iOS - didRecieveRemoteNotification在应用未运行时无法正常工作

时间:2015-04-08 19:52:12

标签: ios objective-c xcode ios8 apple-push-notifications

推送通知在应用运行时或在后台运行时调用didReceiveRemoteNotification。 但是如果用户杀死了#34;应用和我发送推送通知,当用户点按通知时,应用不会拨打didReceiveRemoteNotification

这就是我所拥有的:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)dict{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:dict[@"title"]
                                                message:dict[@"description"]
                                               delegate:self
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];

[alert show];

NSLog(@"didReceiveRemoteNotification: %@", dict);}

1 个答案:

答案 0 :(得分:3)

根据UIApplicationDelegate协议参考:

  

如果远程通知到达时应用程序未运行,则   方法启动应用程序并提供相应的信息   启动选项字典。该应用程序不会调用此方法   处理该远程通知。相反,你的实施   application:willFinishLaunchingWithOptions:或   application:didFinishLaunchingWithOptions:方法需要得到   远程通知有效载荷数据并做出适当的响应。

当您的应用启动applicationDidFinishLaunching将包含在应用程序终止时收到的任何远程通知的数据。当您的应用程序完成启动时,请使用以下代码。

if ([launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey]) 
{
   [self application:application didReceiveRemoteNotification:launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]];
}

来源https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/#//apple_ref/occ/intfm/UIApplicationDelegate/application:didReceiveRemoteNotification