ios 接收远程推送通知时打开视图控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13416745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:13:33  来源:igfitidea点击:

Open view controller when receiving remote Push Notification

objective-ciosapple-push-notifications

提问by theomen

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:

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

How can I show a modal view in response to a notification as a new window? (no parent vc)

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

Open a specific tab/view when user receives a push notification

当用户收到推送通知时打开特定的选项卡/视图

So here is my code:

所以这是我的代码:

- (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 is the view controller set as initial in the storyboard.

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

Many thanks.

非常感谢。

回答by Paramasivan Samuttiram

Please try the following code:

请尝试以下代码:

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

回答by Alex Moleiro

My code differs a bit from the answers I have seen. The fact is that the only code that works form me, is the following:

我的代码与我看到的答案略有不同。事实是,唯一对我有用的代码如下:

    UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;

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

    IniciarSliderViewController *controller = (IniciarSliderViewController*)[mainStoryboard instantiateViewControllerWithIdentifier: @"MenuSlider"];

   [navigationController pushViewController:controller animated:YES];

1.- Instantiate de navigationController. Usually the rootviewcontroller in the vast majority of the cases, but not in all

1.- 实例化导航控制器。通常在绝大多数情况下是 rootviewcontroller,但不是在所有情况下

2.- Instantiate the storyboard. Usuarlly tagged as MainStoryboard

2.- 实例化故事板。通常被标记为 MainStoryboard

3.- Instantiate your specific view controller. You must adapt for your special case

3.- 实例化您的特定视图控制器。你必须适应你的特殊情况

4.- Push as you should do because you've set all you need

4.-按你应该做的方式推送,因为你已经设置了所有你需要的