【发布时间】:2015-09-08 19:19:25
【问题描述】:
我有一个使用 XIB 而不是 Storyboard 的应用。 rootViewController 是一个 TabBarController。有 5 个选项卡,每个选项卡都包含一个 NavigationController。我想做的是让应用程序在用户点击收到的推送通知时推送某个视图控制器。在我的 didFinishLaunchingWithOptions 中(除其他设置外):
window.rootViewController = self.tabBarController;
[window makeKeyAndVisible];
在后面的代码中:
- (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];
}
但是,当我点击通知时,什么都没有发生,尽管 NSLog 确实在控制台中触发。有什么建议吗?
根据答案,我现在有
- (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];
}
但这只有在应用程序已经在后台暂停运行时才有效。如果它已经崩溃或用户从应用切换器强制关闭它,它只会打开应用,而不是推动控制器。
编辑:
这里是didFinishLaunchingWithOptions 方法:
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
categories:nil];
[[UIApplication sharedApplication]
registerUserNotificationSettings:settings];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
【问题讨论】:
标签: ios objective-c uinavigationcontroller push-notification uitabbarcontroller