收到推送通知时显示视图控制器不起作用,其视图不在窗口层次结构中

Posted

技术标签:

【中文标题】收到推送通知时显示视图控制器不起作用,其视图不在窗口层次结构中【英文标题】:Show viewcontroller on receive of push notification is not working, whose view is not in the window hierarchy 【发布时间】:2015-10-19 13:13:23 【问题描述】:

我的 AppDelegate 在目标 c 中,我想在收到推送通知时显示的视图控制器在 Swift 中。我尝试了很多东西,但我无法在收到推送通知时获得正确的视图控制器。以下是我尝试过的代码 -

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *sessionUserID = [defaults objectForKey:@"USERID"];
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;

NSLog(@"App State Inactive->%d",application.applicationState== UIApplicationStateInactive);
NSLog(@"App State Active->%d",application.applicationState== UIApplicationStateActive);
NSLog(@"App State Active->%d",application.applicationState== UIApplicationStateBackground);

NSLog(@"=====userinfo %@",userInfo);
NSString *msgID  = userInfo[@"aps"][@"alert"];
    if(sessionUserID != nil)
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UIViewController *destCon = [storyboard instantiateViewControllerWithIdentifier:@"notification"]; // Swift viewcontroller
        UIViewController *navController =(UIViewController *) self.window.rootViewController;
        [navController presentViewController:destCon animated:YES completion:nil];

    

以下是日志消息 -

2015-10-19 18:40:43.363 VidurOnMobile[631:94551] App State Inactive->1
2015-10-19 18:40:43.363 VidurOnMobile[631:94551] App State Active->0
2015-10-19 18:40:43.363 VidurOnMobile[631:94551] App State Active->0
2015-10-19 18:40:43.364 VidurOnMobile[631:94551] =====userinfo 
    aps =     
        alert = test;
        badge = 1;
        sound = default;
    ;
    id = 1;

2015-10-19 18:40:43.364 VidurOnMobile[631:94551] Session is still there
2015-10-19 18:40:43.646 VidurOnMobile[631:94551] Warning: Attempt to present <VidurOnMobile.NotificationDetails: 0x156c5810> on <VidurOnMobile.LoginViewController: 0x15553910> whose view is not in the window hierarchy!

请帮忙,我尝试了很多东西,但它不起作用:(

【问题讨论】:

【参考方案1】:

试试这个:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *destCon = [storyboard instantiateViewControllerWithIdentifier:@"notification"]; // Swift viewcontroller
[self.window makeKeyAndVisible];

UIViewController *activeController = [UIApplication sharedApplication].keyWindow.rootViewController;

if ([activeController isKindOfClass:[UINavigationController class]]) 
   activeController = [(UINavigationController*) activeController visibleViewController];


[activeController presentViewController:destCon animated:YES completion:nil];

【讨论】:

2015-10-19 18:57:54.358 VidurOnMobile[653:99800] 警告:尝试在 上显示 ,其视图不在窗口层次结构! 只要检测到当前可见的viewController,实际上就可以从AppDelegate 呈现模态视图控制器。你能试试我编辑的帖子吗?【参考方案2】:

你确定sessionUserID 不为零吗? 然后获取导航控制器的顶部视图控制器并呈现通知视图控制器。试试这个

   UINavigationController *navigationController = (UINavigationController *) self.window.rootViewController;
   UIViewController *topViewController = [[navigationController viewControllers] objectAtIndex:0];
   UIViewController *destCon = [storyboard instantiateViewControllerWithIdentifier:@"notification"]; // Swift viewcontroller
   [topViewController presentModalViewController:destCon animated:YES];

【讨论】:

应用程序因此代码而崩溃 - 以下是日志消息 2015-10-20 14:30:56.485 VidurOnMobile[1155:235398] -[VidurOnMobile.LoginViewController viewControllers]:无法识别的选择器发送到实例 0x166773d0 2015-10-20 14:30:56.488 VidurOnMobile[ 1155:235398] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[VidurOnMobile.LoginViewController viewControllers]:无法识别的选择器发送到实例 0x166773d0”libc++abi.dylib:以 NSException 【参考方案3】:

这解决了我的问题 -

UIViewController *main=[[UIStoryboard storyboardWithName:@"Main" bundle:nil]  instantiateViewControllerWithIdentifier:@"main"];

        UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:main];
        self.window.rootViewController =nil;
        UIViewController *destCon = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"notification"];
        self.window.rootViewController = navigationController;
        [self.window makeKeyAndVisible];
        [self.window.rootViewController presentViewController:destCon animated:YES completion:nil];

【讨论】:

以上是关于收到推送通知时显示视图控制器不起作用,其视图不在窗口层次结构中的主要内容,如果未能解决你的问题,请参考以下文章

收到推送通知时显示模式

推送视图时显示白屏

尝试呈现其视图不在窗口层次结构中的 UIAlertController (Swift 3/Xcode 8)

尝试在点击单元格时显示其视图不在窗口层次结构中

用户收到推送通知后更新表视图中的数据

Xamarin/Mvvmcross:收到 iOS 推送通知时打开不同的视图控制器