Open Specific View when Opening App from Notification

前端 未结 3 1113
梦如初夏
梦如初夏 2020-12-04 12:28

I have just added push notifications to my app. I\'m wanting to have so that when a user opens the app from a notification, it will open a specific view controller and not

相关标签:
3条回答
  • 2020-12-04 13:12

    If the app is opened from a notification, either of the two methods from your app delegate will be called

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
    

    OR

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    

    In case of the latter, the launchOptions will let you know if the app was launched due to a remote notification or some other reason (see Launch Option Keys here)

    Put in a check in these methods so that the specific viewcontroller will be opened if the app is launched from a notification.

    0 讨论(0)
  • 2020-12-04 13:33

    You can do some thing like this

      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    
       // If application is launched due to  notification,present another view controller.
    UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    
    if (notification)
    {
        NotificationViewController *viewController = [[NotificationViewController alloc]initWithNibName:NSStringFromClass([NotificationViewController class]) bundle:nil];
        [self.window.rootViewController presentModalViewController:viewController animated:NO];
        [viewController release];
    }
    
    return YES;
    }
    
     -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
     {
    
    NotificationViewController *viewController = [[NotificationViewController alloc]initWithNibName:NSStringFromClass([NotificationViewController class]) bundle:nil];
    
    
    [self.window.rootViewController presentModalViewController:viewController animated:NO];
    [viewController release];
    }
    
    0 讨论(0)
  • 2020-12-04 13:35

    You could post a notification yourself when you receive a remote notification and by registering the viewcontroller to this notification, you could open a particular viewController once notification is received.

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    
       [[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];
    
    }
    

    In your FirstViewController.m register for listening to this notification.

    -(void)viewDidLoad{  
    
          [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushNotificationReceived) name:@"pushNotification" object:nil];
    
    }
    

    Inside the method you could open particular viewController

    -(void)pushNotificationReceived{
    
     [self presentViewController:self.secondViewController animated:YES completion:nil];
    }
    

    Finally un-register current viewController from notification in dealloc method

    -(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    }
    
    0 讨论(0)
提交回复
热议问题