简体   繁体   中英

How to navigate to another view controller in push notifications

我正在做一个应用程序,因为我有一些视图控制器,例如A-> B,A-> C,A-> D和B-> E,C-> F,D-> G和E-> H, F-> I和G-> J。因此我在E视图控制器中,每当收到通知时,我都必须移至G视图控制器。如果我在除G之外的任何其他视图控制器中,则需要移至G视图控制器因此,请告诉我该怎么做。

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 (ie 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 ie:

[[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..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM