Launching a specific viewController in response to a remote push notification

13,823

Solution 1

It's possible to do what you describe, but I would not recommend it.

Frist, place a disconnected view controller with the view that you want in the storyboard, give the view controller an identifier, something like "My Push Notification View"

In didFinishLaunchingWithOptions:, You can get to the rootViewController from the app delegate. This controller will be the navigation controller. Using navigation controller, you can push a new view controller on top of the stack. To create the new view controller, you instantiate the view controller with the identifier "My Push Notification View".

UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
UIViewController *notificationController = [navController.storyboard instantiateViewControllerWithIdentifier:@"My Push Notification View"];

[navController pushViewController:notificationController animated:YES];

I think you will want to use something like -presentViewController:animated:completion: to show a modal view instead of interrupting the navigation stack.

UIViewController *rootController = (UIViewController *)self.window.rootViewController;
UIViewController *notificationController = [rootController.storyboard instantiateViewControllerWithIdentifier:@"My Push Notification View"];

[rootController presentViewController:notificationController animated:YES completion:NULL];

Solution 2

Try This i used in one of my application, User a variable in app delegate as a global

 ex: BOOL gotNotifcation;

-(void)application:(UIApplication*)app didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    NotificationsViewController *notificationobject = [[NotificationsViewController alloc]init];
    [self.navigationController pushViewController:notificationobject animated:YES];
    gotNotifcation = YES;   
}

In NotificationsViewController for back button action if it is customized button

-(void)gotoback
{
    AppDelegate *delegate =(AppDelegate *)[UIApplication sharedApplication].delegate;

    if(delegate.gotNotifcation)
    {
        delegate.gotNotifcation = NO;
        MessageListController *feed = [[MessageListController alloc] init];
        [self.navigationController pushViewController:feed animated:NO];
    }
    else
    {
        [self.navigationController popViewControllerAnimated:NO];
    }
}
Share:
13,823
conorgriffin
Author by

conorgriffin

I am an experienced engineer working for Workday in Dublin, Ireland. I have a background in development and systems engineering and love working with technology.

Updated on June 27, 2022

Comments

  • conorgriffin
    conorgriffin about 2 years

    I'm creating an app who's storyboard flows like the image below:

    storyboard

    When a user logs in from "Sysalert View Controller" they are brought into "Message List View Controller" where I do an NSURLConnection to load some JSON into the table. When a user taps a row in the table, they are brought into "Message Details" which shows more detailed information for that message.

    When a user launches the app from a push notification, regardless of the state of the app prior to launching, I want the app to load the "Message List" data from my server and then show them the message that's just been pushed to the device.

    I know I need to use didFinishLaunchingWithOptions to tell the app to react to the push notification but how do I setup the view hierarchy so that the "Message List" view controller loads its data and then pushes the "Message Details" view controller onto the stack for the appropriate message?

    Essentially this kind of mimics the behaviour of the Messages or Mail apps. Where opening with a notification brings you to a view controller for that message but you are still able to move back in the hierarchy as if you had launched the app from the initial viewController and traversed through the viewControllers in turn.