如何发出推送通知打开某个视图控制器? [英] How to make your push notification Open a certain view controller?

查看:76
本文介绍了如何发出推送通知打开某个视图控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了SO,但是当您收到推送通知时,我找不到任何讨论的问题,然后您如何才能打开特定的视图控制器. 例如,如果您正在创建类似WhatsApp的应用程序,并且收到两个不同的推送通知,即来自两个不同用户的消息,您将如何将其从应用程序委托定向到相应的viewController?

I looked on SO but I wasn't able to find any question that discusses when you receive a push notification how can you then open a specific view controller. For example if you are creating an app like WhatsApp and you receive two different push notifications ie messages from two different users how would you direct from the app delegate to the respective viewController?

据我在userinfo词典中知道appDelegate给您的,您可以给一个特定的viewController一个ID,但是我不知道如何向一个特定的视图控制器致敬,以便您可以再次定向到那个viewController. 请在答案中包含一个代码段

As far as I know in the userinfo dictionary that the appDelegate gives you you can give an ID to a specific viewController but I don't know how to give any a tribute to a specific view controller so that then you could again direct to that viewController. Kindly include a code snippet with your answer

**** Swift或Objective-C答案都是可以接受的****

**** Swift or Objective-C answers are both acceptable ****

推荐答案

您可以使用应用程序委托中的此代码检测是否从通知中打开了应用程序.在应用程序变为活动状态之前,当应用程序状态为UIApplicationStateInactive时,您将需要设置初始视图控制器.您可以在此处执行任何逻辑,以决定应打开哪个视图控制器以及应在该视图控制器中显示哪些内容.

You can detect if the app opened from the notification with this code in app delegate. You will need to set the initial view controller when the application state is UIApplicationStateInactive before the app has become active. You can perform any logic there to decide which view controller should be opened and what content should be shown in that view controller.

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

    if(application.applicationState == UIApplicationStateActive) {

        //app is currently active, can update badges count here

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

        //app is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here

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

        //app is transitioning from background to foreground (user taps notification), do what you need when user taps here

        self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];

        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

        UIViewController *viewController = // determine the initial view controller here and instantiate it with [storyboard instantiateViewControllerWithIdentifier:<storyboard id>];

        self.window.rootViewController = viewController;
        [self.window makeKeyAndVisible];

    }

}

这篇关于如何发出推送通知打开某个视图控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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