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

Posted

技术标签:

【中文标题】当用户点击推送通知时导航到特定的视图控制器【英文标题】:Navigating to specific view controller when user taps on push notification 【发布时间】:2016-02-22 05:08:43 【问题描述】:

当推送通知到达时,当应用程序处于终止状态时,我需要通过点击推送通知启动应用程序时从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];
    




这是正确的吗? 还是我需要进行任何更改?

【问题讨论】:

我认为您需要在ApplicationDidfinishLaunching 中编写代码才能在您的应用被杀死时获取通知详细信息。 Open Specific View when Opening App from Notification的可能重复 @SibaPrasadHota:我已经从应用程序调用了这个方法并完成了启动 【参考方案1】:

将您的逻辑写入didFinishLaunchingWithOptions,您将在其中获取通知数据。

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

launchOptions?["UIApplicationLaunchOptionsRemoteNotificationKey"]

如果你得到这个值,那么你插入导航到特定UIViewController 的逻辑,否则什么都没有。

【讨论】:

以上是关于当用户点击推送通知时导航到特定的视图控制器的主要内容,如果未能解决你的问题,请参考以下文章