jack kevin
jack kevin

Reputation: 26

How to navigate to another view controller in push notifications

I am doing one application.In that i have some view controllers as like A->B,A->C,A->D and B->E,C->F,D->G and E->H,F->I and G->J.So i am in E view controller,whenever notification comes,i have to move to G view controller.And if i am in any view controller except G,i need to move to G view controller.So please tell me how to do it.

Upvotes: 0

Views: 977

Answers (1)

Kex
Kex

Reputation: 8579

You need to first set some code in your AppDelegate.m to respond to pushes when the app is open:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    //If opening app from notification
    if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground  )
    {
        //restore push
        [[NSNotificationCenter defaultCenter] postNotificationName:@"appRestorePush" object:nil];

    }
    //If app is already open
    else {


        [[NSNotificationCenter defaultCenter] postNotificationName:@"appOpenPush" object:nil];

    }


}

Here we fire a NSNotification if the app is open from a push (i.e. you slide on it from the lock screen) and there is also a different notification for if the app is already open. You don't have to use two different types of NSNotification but it might be useful to you. I'm not really sure what your view controller setup is but assuming you are using a UINavigation controller for everything in the root controller you just set it up to listen in the ViewDidLoad i.e:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appOpenPush) name:@"appOpenPush" object:nil];

and in the method you call something like this:

-(void)appOpenPush {

   NSLog(@"got a push while app was open");
   //Get the view controller

   UIViewController *lastViewController = [[self.navigationController viewControllers] lastObject];
    if([lastViewController isKindOfClass:[MyViewController class]]) {

     //do your segue here

   }

   else if//////do this for all your cases...

}

Here we are checking what type of class the view controller is and then choosing the appropriate segue.

Hope you have done a little work at least and understand what I wrote..

Upvotes: 1

Related Questions