didReceiveRemoteNotification:fetchCompletionHandler:从图标打开 vs 推送通知 [英] didReceiveRemoteNotification: fetchCompletionHandler: open from icon vs push notification

查看:32
本文介绍了didReceiveRemoteNotification:fetchCompletionHandler:从图标打开 vs 推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现后台推送通知处理,但我在确定用户是否通过发送的推送通知打开应用程序而不是通过图标打开应用程序时遇到问题.

I'm trying to implement background push notification handling, but I'm having issues with determining whether the user opened the app from the push notification that was sent as opposed to opening it from the icon.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

    //************************************************************
    // I only want this called if the user opened from swiping the push notification. 
    // Otherwise I just want to update the local model
    //************************************************************
    if(applicationState != UIApplicationStateActive) {
        MPOOpenViewController *openVc = [[MPOOpenViewController alloc] init];
        [self.navigationController pushViewController:openVc animated:NO];
    } else {
        ///Update local model
    }

    completionHandler(UIBackgroundFetchResultNewData);
}

使用此代码,无论用户如何打开应用程序,应用程序都会打开 MPOOpenViewController.我怎样才能做到只有在他们通过滑动通知打开应用程序时才会推送视图控制器?

With this code, the app is opening to the MPOOpenViewController regardless of how the user opens the app. How can I make it so that the view controller is only pushed if they open the app from swiping the notification?

使用相同的代码,这在 iOS 6 上工作,但使用新的 iOS 7 方法,它的行为不像我想要的那样.

With the same code, this worked on iOS 6, but with the new iOS 7 method, it doesn't behave how I want it to.

我现在正在尝试在 iOS 7 上运行该应用程序,我们不支持 iOS 7 之前的任何版本.我在 iOS 6 版本的应用程序中使用了完全相同的代码方法(没有完成处理程序),它的行为与我期望的一样.你会滑动通知,这会被调用.如果您从图标打开,则永远不会调用该方法.

I'm trying to run the app on iOS 7 now, and we are not supporting any version prior to iOS 7. I used this same exact code in the iOS 6 version of the method (without the completion handler) and it behaved the way I'd expect it to. You'd swipe the notification and this would get called. If you opened from the icon, the method would never be called.

推荐答案

好的,我想通了.该方法实际上被调用了两次(一次是在接收推送时,一次是在用户与图标或通知交互时).

Ok I figured it out. The method is actually called twice (once when it receives the push, and once when the user interacts with the icon or the notification).

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

    if(application.applicationState == UIApplicationStateInactive) {

        NSLog(@"Inactive");

        //Show the view with the content of the push

        completionHandler(UIBackgroundFetchResultNewData);

    } else if (application.applicationState == UIApplicationStateBackground) {

        NSLog(@"Background");

        //Refresh the local model

        completionHandler(UIBackgroundFetchResultNewData);

    } else {

        NSLog(@"Active");

        //Show an in-app banner

        completionHandler(UIBackgroundFetchResultNewData);

    }
}

感谢 Tim Castelijns 添加以下内容:

Thanks Tim Castelijns for the following addition:

注意:它被调用两次的原因是因为 Payload 有content_available : 1.如果您删除键及其值,那么它只会在点击时运行.这不会解决每个人的问题,因为有些人需要该密钥才能为真

Note: the reason it's called twice is due to the Payload having content_available : 1. If you remove the key and its value, then it will only run upon tapping. This will not solve everyone's problem since some people need that key to be true

这篇关于didReceiveRemoteNotification:fetchCompletionHandler:从图标打开 vs 推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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