当用户点击推送通知时导航到特定的视图控制器

时间:2016-02-22 05:08:44

标签: ios objective-c xcode

当推送通知到达时,当应用程序处于终止状态时,我需要通过点按推送通知从appdelegate导航到特定的视图控制器?

//处理推送通知的辅助方法

- (void)handleNotification:(NSDictionary *)userInfo {
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    NSString * token = [defaults objectForKey:@"token"];

    UINavigationController *navigationController = (UINavigationController *) self.window.rootViewController;
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    if (token!=NULL) {
        // Show the dashboard
        [navigationController pushViewController:[storyboard instantiateViewControllerWithIdentifier:@"listviewcontroller"] animated:NO];
    } else {
        // Login
        [navigationController pushViewController:[storyboard instantiateViewControllerWithIdentifier:@"viewcontroller"] animated:NO];
    }



}

这是正确的吗? 或者我需要做出任何改变吗?

1 个答案:

答案 0 :(得分:0)

didFinishLaunchingWithOptions中写下您的逻辑,您将获得通知数据。

当用户点击任何通知时,您将获得特定密钥:

launchOptions?["UIApplicationLaunchOptionsRemoteNotificationKey"]

如果你得到这个值,那么你就可以插入导航到特定UIViewController的逻辑。