gpt4 book ai didi

ios - 为什么没有调用 didReceiveRemoteNotification 但 didReceiveRemoteNotification :fetchCompletionHandler called when my app is in the foreground?

转载 作者:IT王子 更新时间:2023-10-29 05:23:39 24 4
gpt4 key购买 nike

如果我覆盖

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,并且在除 name 之外的所有方法中都被弃用:

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.

旧版本还会根据应用是实时(前台或后台)还是从冷启动启动而给出不同的结果。在后一种情况下,它不会被调用,您需要拦截 didFinishLaunchingWithOptions 上的 launchOptions 字典以获取 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.

使用回调版本。它适用于所有情况。 您不必使用完成处理程序/ block ,在这种情况下效果是相同的(好吧,与更新的方法更一致)。

更新

抱歉,Apple 说您确实必须“尽快”调用完成 block - 在操作系统放弃您之前,您有 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.

所以调用完成 block :

        completionHandler(UIBackgroundFetchResult.NoData)

我一直在使用此方法,没有调用该完成 block 并且没有遇到任何问题,但为了安全起见,我现在将添加一个。 Apple 建议如果您不这样做,您的应用程序将不会被置于前台,但我在实践中没有看到这种情况。

关于ios - 为什么没有调用 didReceiveRemoteNotification 但 didReceiveRemoteNotification :fetchCompletionHandler called when my app is in the foreground?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29929438/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com