如何检索和处理远程推送通知?

在应用没在运行时获取远程通知,我找到的方法:

UILocalNotification *localNotification  = [launchOptions 
                   objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]

在方法:

 - (BOOL)application:(UIApplication *)application 
               didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

并没有得到信息。

后来我采用alertView显示信息,也没显示。

为什么?请高手帮忙解决

如果你的应用没有在运行,那么你应该在 application:didFinishLaunchingWithOptions: 方法中检查远程通知的参数。你可以通过以下代码来获取远程通知的内容:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSDictionary *remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (remoteNotification) {
        // 远程通知被收到了
        // 在这里处理通知内容
    }

    return YES;
}

如果你使用的是 UIAlertView 来显示通知信息,请注意 UIAlertView 已经被废弃,建议使用 UIAlertController 代替。

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
[self presentViewController:alert animated:YES completion:nil];