ios – 为什么didReceiveRemoteNotification不被调用,但didReceiveRemoteNotification:fetchCompletionHandler当我的应用程序在前台调用?

如果我覆盖
override func application(application: UIApplication,didReceiveRemoteNotification userInfo: [NSObject : AnyObject],fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    println("hey)
}

当我发送推送通知时,我成功地使用该应用在前台调用的方法.

如果我覆盖

override func application(application: UIApplication,didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    println("hey")
}

在前台发送通知应用程序时,我没有收到任何方法的调用.为什么第一个工作,但第二个没有应用程序在前台?

请注意,我只是一次执行其中之一.不是同时两个.

解决方法

您应该使用回调版本,除非您需要支持ios< 7(引入时).当你使用Swift时,我预计不是这样. 旧的方法可以追溯到ios3,并且除了名称之外都被弃用:

Implement the application:didReceiveRemoteNotification:fetchCompletionHandler: method instead of this one whenever possible. If your delegate implements both methods,the app object calls the application:didReceiveRemoteNotification:fetchCompletionHandler: method.

旧版本还将根据应用程序是否有效(前景或背景)或从冷启动启动而提供不同的结果.在后一种情况下,它不会被调用,您将需要截取didOfficeLaunchingWithOptions的launchOptions dict来获取infoDict:

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.

使用回调版本.适用于所有情况.您不必使用完成处理程序/块,在这种情况下,效果是相同的(更符合较新的方法).

更新

对不起,苹果说你必须尽快打电话给完成块“ – 在操作系统放弃你之前你有30秒钟的时间.这可能是您的警告的根源.

As soon as you finish processing the notification,you must call the block in the handler parameter or your app will be terminated. Your app has up to 30 seconds of wall-clock time to process the notification and call the specified completion handler block. In practice,you should call the handler block as soon as you are done processing the notification.

所以调用完成块:

completionHandler(UIBackgroundFetchResult.NoData)

我一直在使用这种方法,而不用调用那个完成块,没有遇到任何问题,但是我现在只需要加一个来安全.苹果建议,如果你不这样做,你的应用程序将不会出现,但实际上我没有看到.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...