推送通知收到iOS时推送视图控制器

时间:2015-09-08 19:19:25

标签: ios objective-c uinavigationcontroller push-notification uitabbarcontroller

我有一个使用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];

1 个答案:

答案 0 :(得分:3)

如果选项卡控制器上的每个选项卡都是导航控制器,请尝试以下操作:

UINavigationController *nav = (UINavigationController *) self.tabBarController.selectedViewController;
NewsViewController *dvController8 = [[NewsViewController alloc] initWithNibName:@"NewsViewController" bundle:[NSBundle mainBundle]];
[nav pushViewController:dvController8 animated:YES];

所以基本上首先获取所选控制器的引用,并在选定的视图控制器上创建pushViewController。

编辑,如果应用未在后台运行,如何处理:

如果应用程序没有在后台运行,则必须在应用程序中执行:didFinishLaunchingWithOptions

文档说:“如果远程通知到达时应用程序未运行,该方法将启动应用程序并在启动选项字典中提供相应的信息。该应用程序不会调用此方法来处理该远程通知。 ,你的应用程序的实现:willFinishLaunchingWithOptions:或application:didFinishLaunchingWithOptions:方法需要获取远程通知有效负载数据并做出适当的响应。“

请检查:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/#//apple_ref/occ/intfm/UIApplicationDelegate/application:didReceiveRemoteNotification

所以我的建议是首先创建一个帮助方法来处理你的通知,如下所示:

- (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上测试代码,所以可能会有一些错别字,但你应该知道如何实现你需要的东西。

希望它有所帮助!