I have an app that uses XIB and not Storyboard. The rootViewController is a TabBarController. There are 5 tabs, and each tab contains a NavigationController. What I would like to do, is have the app PUSH a certain view controller whenever the user taps on a received Push Notification. In my didFinishLaunchingWithOptions I have (amongst other setup things):
window.rootViewController = self.tabBarController;
[window makeKeyAndVisible];
In later code:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[PFPush handlePush:userInfo];
if (application.applicationState == UIApplicationStateInactive) {
[self handleRemoteNotificationWithPayload:userInfo];
}
}
-(void)handleRemoteNotificationWithPayload:(NSDictionary *)payload {
NSLog(@"User tapped notification");
NewsViewController *dvController8 = [[NewsViewController alloc] initWithNibName:@"NewsViewController" bundle:[NSBundle mainBundle]];
[self.tabBarController.navigationController pushViewController:dvController8 animated:YES];
}
However, nothing at all happens when I tap the notification, though the NSLog does fire in the console. Suggestions?
BASED off an answer, I now have
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[PFPush handlePush:userInfo];
UINavigationController *nav = (UINavigationController *) self.tabBarController.selectedViewController;
NewsViewController *dvController8 = [[NewsViewController alloc] initWithNibName:@"NewsViewController" bundle:[NSBundle mainBundle]];
[nav pushViewController:dvController8 animated:YES];
}
But this only works if the app is already running suspended in background. If it has crashed or user has forced it close from app switcher, it will just open the app, and not push the controller.
EDIT:
Here is the didFinishLaunchingWithOptions
method:
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
categories:nil];
[[UIApplication sharedApplication]
registerUserNotificationSettings:settings];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];