为什么didreceivertenotification不是叫didreceivertenotification:fetchCompletionHandler在我的应用程序在前台的时候叫呢?

时间:2022-08-23 11:58:26

If I override

如果我覆盖

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

I successfully have the method called with the app in the foreground when I send a push notification.

当我发送推送通知时,我成功地在前台用app调用了这个方法。

If I override

如果我覆盖

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

I don't get any call to the method when sending a notification with the app in the foreground. Why does the first one work, but the second one doesn't when the app is in the foreground?

当用前台的应用程序发送通知时,我没有收到任何对方法的调用。为什么第一个可以用,第二个不能用呢?

Note that I am only implementing one of these at a time. Not both at the same time.

注意,我每次只实现其中的一个。不是同时进行。

2 个解决方案

#1


15  

You should use the callback version unless you need to support ios<7 (when it was introduced). As you are using Swift I expect that is not the case.

除非需要支持ios<7(引入时),否则应该使用回调版本。当你使用Swift时,我预计情况并非如此。

The older method goes back to ios3 and is deprecated in all but name:

旧的方法可以追溯到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.

实现应用:didReceiveRemoteNotification:fetchCompletionHandler:方法而不是这个方法。如果您的委托实现了这两个方法,应用程序对象将调用应用程序:didReceiveRemoteNotification:fetchCompletionHandler:方法。

The older version will also give different results depending on whether the app is live (foreground or background) or launches from a cold start. In the latter case, it will not get called, and you will need to intercept the launchOptions dict on didFinishLaunchingWithOptions to get at the infoDict:

旧版本还会根据应用程序是实时(前景还是背景)还是冷启动而给出不同的结果。在后一种情况下,它将不会被调用,您将需要拦截关于didFinishLaunchingWithOptions的launchOptions命令,以获取信息敕令:

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.

如果当远程通知到达时应用程序没有运行,该方法将启动应用程序并在启动选项字典中提供适当的信息。应用程序不会调用此方法来处理远程通知。

Use the callback version. It works for all cases. You don't have to use the completion handler/block , in which case the effect is the same (well, more consistent with the newer method).

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

update

更新

sorry, Apple says you do have to call the completion block "as soon as possible" - you have 30 seconds to do so before the OS gives up on you. This might be the source of your warning.

对不起,苹果说你必须尽快调用完成块——在操作系统放弃你之前你有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.

一旦您完成通知处理,您必须调用处理程序参数中的块,否则您的应用程序将被终止。您的应用程序最多有30秒的挂钟时间来处理通知并调用指定的完成处理程序块。在实践中,应该在处理通知之后立即调用处理程序块。

So call the completion block:

因此调用完成块:

        completionHandler(UIBackgroundFetchResult.NoData)

I have been using this method without calling that completion block and haven't experienced any problems, but i will add one now just to be safe. Apple suggests that if you don't, your app won't get foregrounded but I have not seen that in practice.

我一直在使用这个方法而没有调用完成块,也没有遇到任何问题,但是为了安全起见,我现在将添加一个。苹果表示,如果你不这么做,你的应用程序就不会被预先设定,但我在实践中还没有看到这一点。

#2


0  

Did you register at least one UIUserNotificationType with UIApplication:registerUserNotificationSettings: ?

您是否注册了至少一个UIUserNotificationType与UIApplication:registerUserNotificationSettings: ?

If you do not register for badge, sound, or alert then you can only receive silent notifications, which are only delivered with a FetchCompletionHandler.

如果不注册badge、sound或alert,则只能接收静默通知,这些通知只能通过FetchCompletionHandler发送。

#1


15  

You should use the callback version unless you need to support ios<7 (when it was introduced). As you are using Swift I expect that is not the case.

除非需要支持ios<7(引入时),否则应该使用回调版本。当你使用Swift时,我预计情况并非如此。

The older method goes back to ios3 and is deprecated in all but name:

旧的方法可以追溯到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.

实现应用:didReceiveRemoteNotification:fetchCompletionHandler:方法而不是这个方法。如果您的委托实现了这两个方法,应用程序对象将调用应用程序:didReceiveRemoteNotification:fetchCompletionHandler:方法。

The older version will also give different results depending on whether the app is live (foreground or background) or launches from a cold start. In the latter case, it will not get called, and you will need to intercept the launchOptions dict on didFinishLaunchingWithOptions to get at the infoDict:

旧版本还会根据应用程序是实时(前景还是背景)还是冷启动而给出不同的结果。在后一种情况下,它将不会被调用,您将需要拦截关于didFinishLaunchingWithOptions的launchOptions命令,以获取信息敕令:

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.

如果当远程通知到达时应用程序没有运行,该方法将启动应用程序并在启动选项字典中提供适当的信息。应用程序不会调用此方法来处理远程通知。

Use the callback version. It works for all cases. You don't have to use the completion handler/block , in which case the effect is the same (well, more consistent with the newer method).

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

update

更新

sorry, Apple says you do have to call the completion block "as soon as possible" - you have 30 seconds to do so before the OS gives up on you. This might be the source of your warning.

对不起,苹果说你必须尽快调用完成块——在操作系统放弃你之前你有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.

一旦您完成通知处理,您必须调用处理程序参数中的块,否则您的应用程序将被终止。您的应用程序最多有30秒的挂钟时间来处理通知并调用指定的完成处理程序块。在实践中,应该在处理通知之后立即调用处理程序块。

So call the completion block:

因此调用完成块:

        completionHandler(UIBackgroundFetchResult.NoData)

I have been using this method without calling that completion block and haven't experienced any problems, but i will add one now just to be safe. Apple suggests that if you don't, your app won't get foregrounded but I have not seen that in practice.

我一直在使用这个方法而没有调用完成块,也没有遇到任何问题,但是为了安全起见,我现在将添加一个。苹果表示,如果你不这么做,你的应用程序就不会被预先设定,但我在实践中还没有看到这一点。

#2


0  

Did you register at least one UIUserNotificationType with UIApplication:registerUserNotificationSettings: ?

您是否注册了至少一个UIUserNotificationType与UIApplication:registerUserNotificationSettings: ?

If you do not register for badge, sound, or alert then you can only receive silent notifications, which are only delivered with a FetchCompletionHandler.

如果不注册badge、sound或alert,则只能接收静默通知,这些通知只能通过FetchCompletionHandler发送。