How to make your push notification Open a certain view controller?

后端 未结 4 755
星月不相逢
星月不相逢 2020-12-09 00:43

I looked on SO but I wasn\'t able to find any question that discusses when you receive a push notification how can you then open a specific view controller. For example if y

4条回答
  •  不知归路
    2020-12-09 01:19

    You can detect if the app opened from the notification with this code in app delegate. You will need to set the initial view controller when the application state is UIApplicationStateInactive before the app has become active. You can perform any logic there to decide which view controller should be opened and what content should be shown in that view controller.

    -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
    
        if(application.applicationState == UIApplicationStateActive) {
    
            //app is currently active, can update badges count here
    
        } else if(application.applicationState == UIApplicationStateBackground){
    
            //app is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here
    
        } else if(application.applicationState == UIApplicationStateInactive){
    
            //app is transitioning from background to foreground (user taps notification), do what you need when user taps here
    
            self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
    
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    
            UIViewController *viewController = // determine the initial view controller here and instantiate it with [storyboard instantiateViewControllerWithIdentifier:];
    
            self.window.rootViewController = viewController;
            [self.window makeKeyAndVisible];
    
        }
    
    }
    

提交回复
热议问题