收到推送通知时推送视图控制器 iOS
Push View Controller When Push Notification Received iOS
我有一个使用 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];
如果选项卡控制器上的每个选项卡都是导航控制器,试试这个:
UINavigationController *nav = (UINavigationController *) self.tabBarController.selectedViewController;
NewsViewController *dvController8 = [[NewsViewController alloc] initWithNibName:@"NewsViewController" bundle:[NSBundle mainBundle]];
[nav pushViewController:dvController8 animated:YES];
所以基本上首先获取所选控制器的引用,然后在所选视图控制器上创建一个 pushViewController。
编辑,如果应用程序不在后台 运行 如何处理:
如果应用程序不在后台 运行,您必须在 application:didFinishLaunchingWithOptions
上进行
文档说:"if the app is not running when a remote notification arrives, the method launches the app and provides the appropriate information in the launch options dictionary. The app does not call this method to handle that remote notification. Instead, your implementation of the application:willFinishLaunchingWithOptions: or application:didFinishLaunchingWithOptions: method needs to get the remote notification payload data and respond appropriately."
所以我的建议是首先创建一个辅助方法来处理您的通知,如下所示:
- (void)handleNotification:(NSDictionary *)userInfo {
UINavigationController *nav = (UINavigationController *) self.tabBarController.selectedViewController;
NewsViewController *dvController8 = [[NewsViewController alloc] initWithNibName:@"NewsViewController" bundle:[NSBundle mainBundle]];
[nav pushViewController:dvController8 animated:YES];
}
从现在开始,每次您需要处理远程通知时,只需调用 handleNotification 方法即可。
其次将您的 didReceiveRemoteNotification 实现更改为:
[self handleNotification:userInfo];
最后将以下内容添加到您的 didFinishLaunchingWithOptions 实现中:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// your code here
if ([launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]) {
// this means we received a remote notification
[self handleNotification:(NSDictionary *) [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]];
}
return YES;
}
所以在 didFinishLaunchingWithOptions 方法上,您检查 launchOptions 字典是否为 UIApplicationLaunchOptionsRemoteNotificationKey 分配了一些对象。如果是,则意味着该应用程序是从远程通知打开的。分配给该键的值是通知的有效负载。
注意:我没有在 xcode 上测试代码,因此可能会有一些拼写错误,但您应该了解如何实现您需要的内容。
希望对您有所帮助!
我有一个使用 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];
如果选项卡控制器上的每个选项卡都是导航控制器,试试这个:
UINavigationController *nav = (UINavigationController *) self.tabBarController.selectedViewController;
NewsViewController *dvController8 = [[NewsViewController alloc] initWithNibName:@"NewsViewController" bundle:[NSBundle mainBundle]];
[nav pushViewController:dvController8 animated:YES];
所以基本上首先获取所选控制器的引用,然后在所选视图控制器上创建一个 pushViewController。
编辑,如果应用程序不在后台 运行 如何处理:
如果应用程序不在后台 运行,您必须在 application:didFinishLaunchingWithOptions
上进行文档说:"if the app is not running when a remote notification arrives, the method launches the app and provides the appropriate information in the launch options dictionary. The app does not call this method to handle that remote notification. Instead, your implementation of the application:willFinishLaunchingWithOptions: or application:didFinishLaunchingWithOptions: method needs to get the remote notification payload data and respond appropriately."
所以我的建议是首先创建一个辅助方法来处理您的通知,如下所示:
- (void)handleNotification:(NSDictionary *)userInfo {
UINavigationController *nav = (UINavigationController *) self.tabBarController.selectedViewController;
NewsViewController *dvController8 = [[NewsViewController alloc] initWithNibName:@"NewsViewController" bundle:[NSBundle mainBundle]];
[nav pushViewController:dvController8 animated:YES];
}
从现在开始,每次您需要处理远程通知时,只需调用 handleNotification 方法即可。
其次将您的 didReceiveRemoteNotification 实现更改为:
[self handleNotification:userInfo];
最后将以下内容添加到您的 didFinishLaunchingWithOptions 实现中:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// your code here
if ([launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]) {
// this means we received a remote notification
[self handleNotification:(NSDictionary *) [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]];
}
return YES;
}
所以在 didFinishLaunchingWithOptions 方法上,您检查 launchOptions 字典是否为 UIApplicationLaunchOptionsRemoteNotificationKey 分配了一些对象。如果是,则意味着该应用程序是从远程通知打开的。分配给该键的值是通知的有效负载。
注意:我没有在 xcode 上测试代码,因此可能会有一些拼写错误,但您应该了解如何实现您需要的内容。
希望对您有所帮助!