How to get APNS push notification in background in iOS6?

孤街浪徒 提交于 2019-12-08 01:34:57

问题


I am implementing APNS in my iOS application. I am getting apple push notifications in foreground, but when my app goes in background or inactive mode, my app does not receive any push notification.

My code which I tried is as follows:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

    UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateActive) {
        NSLog(@"active");
        NSDictionary *apsInfo = [userInfo valueForKey:@"aps"];

        NSString *fv=[[apsInfo valueForKey:@"alert"] componentsJoinedByString:@""];
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Active" message:fv delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
    }else if (state == UIApplicationStateBackground){
        NSLog(@"background");
        NSDictionary *apsInfo = [userInfo valueForKey:@"aps"];

        NSString *fv=[[apsInfo valueForKey:@"alert"] componentsJoinedByString:@""];
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Background" message:fv delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
    }
    }else{
        NSLog(@"Inactive");
         NSDictionary *apsInfo = [userInfo valueForKey:@"aps"];
         NSString *fv=[[apsInfo valueForKey:@"alert"] componentsJoinedByString:@""];
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Inactive" message:fv delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
    }
}

Please tell me where I am going wrong or missing something.


回答1:


You are not supposed to display the notification programatically when the app is in the background or inactive. iOS displays the alert text of the aps dictionary automatically when the app is in one of these states, and your code is called only after the notification is displayed and tapped by the user to open your app.

If the user doesn't open your app by tapping the notification, your code will never be called. In addition, the didReceiveRemoteNotification method is only called if the app is active or running in the background. If it is not running, a different method is called - application:didFinishLaunchingWithOptions.



来源:https://stackoverflow.com/questions/20487890/how-to-get-apns-push-notification-in-background-in-ios6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!