当ios应用关闭时,Firebase通知点击崩溃

问题描述 投票:0回答:1

当我点击通知时,我希望它打开应用程序的通知viewController。所有代码都在AppDelegate.m中

我有一个函数来找到topMostController

- (UIViewController *)topMostController {
    UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (topController.presentedViewController) {
        topController = topController.presentedViewController;
    }
    return topController;
}

然后还有另一个显示通知viewController的函数

    UINavigationController *navigationController = (UINavigationController *)[self topMostController];

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryBoard" bundle:nil];

    MyViewController *myController = [storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];

    [navigationController pushViewController:myController animated:YES];

如果应用程序打开,它工作正常。但是,当应用程序关闭时,如果我单击通知,它将崩溃。错误是

Fatal Exception: NSInvalidArgumentException
-[ViewController pushViewController:animated:]: unrecognized selector sent to instance

我认为没有topMostController或者navigationController在这种情况下是无效的。我需要使用UINavigationController,因为我想要一个“后退”按钮返回页面(主页面)而不是关闭应用程序。在这种情况下,如何使topMostController有效?我应该在didFinishLaunchingWithOptions函数中创建一些东西吗?谢谢。

objective-c firebase push-notification firebase-notifications navigationcontroller
1个回答
0
投票

你需要分配根视图控制器

   self.navigationController = RootNavigationControllerViewController(rootViewController: HomeViewController.init())



    self.window?.rootViewController =  self.navigationController
    self.window?.makeKeyAndVisible()
© www.soinside.com 2019 - 2024. All rights reserved.