Open Specific View when Opening App from Notification

微笑、不失礼 提交于 2019-11-27 17:32:24

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];

}

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.

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];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!