Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open view controller when receiving remote Push Notification

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.

like image 410
theomen Avatar asked Nov 16 '12 12:11

theomen


2 Answers

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];    
}
like image 80
Paramasivan Samuttiram Avatar answered Sep 22 '22 10:09

Paramasivan Samuttiram


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

like image 44
Alex Moleiro Avatar answered Sep 21 '22 10:09

Alex Moleiro