远程通知方法调用两次 [英] Remote notification method called twice

查看:102
本文介绍了远程通知方法调用两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序委托中实现了 application:didReceiveRemoteNotification:fetchCompletionHandler:以响应推送通知。

I have implemented application:didReceiveRemoteNotification:fetchCompletionHandler: in my application delegate to respond to push notifications.

当应用程序在后台时收到通知时,会立即调用此方法,并在完成后获取新数据并执行完成块。全部按照文档。但是,如果我点击通知警报,则会再次调用此方法,从而导致另一个网络调用和UI更新。我希望每次推送通知都会调用一次此方法,而不是在收到时再调用一次。再次采取行动。

When a notification is received while the app is in the background, this method is called immediately and I fetch new data and execute the completion block when done. All as per the documentation. However, if I tap the notification alert this method gets called again, resulting in another network call and a UI update. I would have expected this method to be called once for each push notification, not once on receipt and again on action.

其他人如何实施此方法?

How have others implemented this method?

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    [self loadSomeResource:^(NSData *data,NSError *error){
        if (error) {
            completionHandler(UIBackgroundFetchResultFailed);
        }
        else if (data){
            completionHandler(UIBackgroundFetchResultNewData);
        }
        else {
            completionHandler(UIBackgroundFetchResultNoData);
        }
    }];
}


推荐答案

以下是收到推送通知时应用程序中注意到的事项:didReceiveRemoteNotification:fetchCompletionHandler:方法:
1。当应用程序未启动时(即,当应用程序既不在后台也不在前台时),该方法被调用一次, applicationState UIApplicationStateInactive 。 2。当应用程序位于前台时,该方法将被调用一次,而 applicationState 将是 UIApplicationStateActive 。当应用程序处于后台时,该方法会被调用两次,一次是在您收到推送通知时,另一次是您点击该通知时。当您收到推送通知时, applicationState 将是 UIApplicationStateBackground ,当您点击该通知时, applicationState UIApplicationStateInactive

applicationState UIApplicationStateBackground 时,我们可以忽略它,因此我们只能处理所有这三种情况的推送通知一次。

Here are the things to be noticed in application:didReceiveRemoteNotification:fetchCompletionHandler: method when you receive a push notification:
1. When the app is not launched (i.e, when the app is neither in background nor in foreground), the method is called once and applicationState will be UIApplicationStateInactive.
2. When the app is in foreground, the method is called once and applicationState will be UIApplicationStateActive.
3. When the app is in background, the method is called twice, once when you receive the push notification, and other time when you tap on that notification. When you receive the push notification, applicationState will be UIApplicationStateBackground and when you tap on that notification, applicationState will be UIApplicationStateInactive.

We can ignore it when the applicationState will be UIApplicationStateBackground and hence we can handle the push notification only once for all the three scenarios.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    if (application.applicationState == UIApplicationStateBackground) {
        completionHandler(UIBackgroundFetchResultNoData);
        return;
    }

    // Do whatever you need here and call completionHandler with appropriate UIBackgroundFetchResult
}

这篇关于远程通知方法调用两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆