Open view controller when receiving remote Push Notification

吃可爱长大的小学妹 提交于 2019-11-30 16:58:41

问题


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:

How can I show a modal view in response to a notification as a new window? (no parent 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.

Many thanks.


回答1:


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];    
}



回答2:


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

2.- Instantiate the storyboard. Usuarlly tagged as MainStoryboard

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

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



来源:https://stackoverflow.com/questions/13416745/open-view-controller-when-receiving-remote-push-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!