接收远程推送通知时打开视图控制器 [英] Open view controller when receiving remote Push Notification

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

问题描述

我正在使用 storyboard ,我想在用户收到远程推送通知时始终打开相同的视图,即使应用程序在后台或打开.我需要呈现的视图是故事板中设置的初始视图控制器之后的四个视图.我读了这篇文章:

I'm using storyboard , and I want to open always the same view when user receives remote push notifications, even the app is in background or opened. The view I need to present is four views after the initial view controller set in the storyboard. I read this posts:

如何将模态视图作为新窗口显示以响应通知?(没有父 vc)

打开特定标签/视图时用户收到推送通知

这是我的代码:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

    UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
    notificacionViewController *menu = [navController.storyboard instantiateViewControllerWithIdentifier:@"notificacion"];

    // First item in array is bottom of stack, last item is top.
    navController.viewControllers = [NSArray arrayWithObjects:menu,nil];

    [self.window makeKeyAndVisible];


}

但是当我收到通知时,应用程序崩溃并出现此错误:

But when I receive notification, the app crashes with this error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[locationViewController setViewControllers:]: unrecognized selector sent to instance 0x42ccd0'

locationViewController 是在故事板中设置为初始值的视图控制器.

locationViewController is the view controller set as initial in the storyboard.

非常感谢.

推荐答案

请尝试以下代码:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
    NotificationViewController *notificationViewController = [[NotificationViewController alloc] init];
    [navController.visibleViewController.navigationController pushViewController:notificationViewController];    
}

这篇关于接收远程推送通知时打开视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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