iOS - 当应用程序未运行时 didRecieveRemoteNotification 不起作用

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

推送通知在应用程序运行时或在后台时调用 didReceiveRemoteNotification。 但是,如果用户“杀死”应用程序并且我发送推送通知,当用户点击通知时,应用程序不会调用 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);}

最佳答案

根据 UIApplicationDelegate 协议(protocol)引用:

If the app is not running when a remote notification arrives, the method launches the app and provides the appropriate information in the launch options dictionary. The app does not call this method to handle that remote notification. Instead, your implementation of the application:willFinishLaunchingWithOptions: or application:didFinishLaunchingWithOptions: method needs to get the remote notification payload data and respond appropriately.

当您的应用程序启动时,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 :

关于iOS - 当应用程序未运行时 didRecieveRemoteNotification 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29524111/

相关文章:

ios - 构建 XCTest 时找不到文件错误

ios - 是否有任何 iOS 粒子系统库

ios - 单线程多任务同步

swift - 如何使用 NSImage 在 Xcode Playground 中显示图像

xcode - 制作一个包含姓名和号码的列表,swift xcode

ios - Xcode 错误 : Could not launch app - unable to attach

ios - 在 Xcode 中使用自动化工具检测 HTTP 响应

ios - React Native - 在 Xcode 中创建桥头的正确方法

ios - NSData dataWithContentsOfURL 用于相机胶卷中的 URL

ios - subview Controller 在呈现和关闭模态视图后失去委托(delegate)