点击通知时在 rootViewController 上显示 viewController

Present viewController over rootViewController when a notification is tapped

当应用程序处于终止状态并且我收到推送通知时。如果我查看我希望应用程序正常加载,但它应该在 rootViewController 上显示一个 viewController,我为其添加了一个关闭按钮,每当我点击此按钮时,我只想关闭此 viewController.

在我从通知打开应用程序时调用的方法中,我这样做了:

func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {
    let tabBarController = self.window!.rootViewController as! UITabBarController
    self.window?.rootViewController = tabBarController
    let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    apptVC = storyboard.instantiateViewController(withIdentifier: "NotificationVC") as! NotificationsViewController
    window?.makeKeyAndVisible()
    window?.rootViewController?.present(apptVC, animated: true, completion: nil)
completionHandler()
    }
}

但不幸的是,当我这样做时,应用程序崩溃了。我已经使用 firebase 实现了推送通知。我希望应用程序 运行 通常像它一样,就像在用户收到通知时在启动时在根视图控制器上呈现一个视图控制器一样。

更新
这是我的故事板的截图:

也许你的 rootView Controller 是 nil。尝试在呈现 class 之前设置 rootview_Controller,即使您可能已经在 did finish launnch of appDelegate 中指定了 rootviewController。

  func userNotificationCenter(_ center: UNUserNotificationCenter,
                                    didReceive response: UNNotificationResponse,
                                    withCompletionHandler completionHandler: @escaping () -> Void) {
 viewController = storyboard.instantiateViewControllerWithIdentifier("YourRootVC") as UIViewController
 self.window?.rootViewController = viewController

 window?.rootViewController?.present(yourPusHNotificationVC, animated: true, completion: nil)
    completionHandler()
        }
    }

所以你需要先满足标签栏控制器:

let storyboard = UIStoryboard.init(name: "YourStoryboardName", bundle: nil)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "YourTabBarController") as! UITabBarController

并使用您想要的视图控制器满足所有 UINavigationControllers

let firstNavigationController = storyboard.instantiateViewiController(withIdentifier: "YourFirstNavigationController") as! UINavigationController

let NotificationVC = storyboard.instantiateViewController(withIdentifier: "NotificationVC") as! NotificationsViewController

并将它们全部设为标签栏控制器的 viewControllers:

tabBarController.viewControllers = [firstNavigationController]

哪个导航控制器应该出现在这个视图控制器中?例如:

tabBarController.selectedViewController == firstNavigationController {
    firstNavigationController.present(NotificationVC, animated: true, completion: nil)
}

这是最后一件事:

self.window = UIWindow.init(frame: UIScreen.main.bounds)
self.window?.rootViewController = tabBarController
self.window?.makeKeyAndVisible()

注意这只是推荐,所以我用了一个UNavigationController

所以我创建了一个名为 present() 的函数。

func present() {

let storyboard = UIStoryboard.init(name: "YourStoryboardName", bundle: nil)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "YourTabBarController") as! UITabBarController
let firstNavigationController = storyboard.instantiateViewiController(withIdentifier: "YourFirstNavigationController") as! UINavigationController
let NotificationVC = storyboard.instantiateViewController(withIdentifier: "NotificationVC") as! NotificationsViewController

tabBarController.viewControllers = [firstNavigationController]

tabBarController.selectedViewController == firstNavigationController {
        firstNavigationController.present(NotificationVC, animated: true, completion: nil)
 }

self.window = UIWindow.init(frame: UIScreen.main.bounds)   
self.window?.rootViewController = tabBarController
self.window?.makeKeyAndVisible()


    }
}

不过我觉得没用也没用。每当点击通知时,我们都需要一个动作。我们应该像这样检查一个动作 identifier

extension AppDelegate: UNUserNotificationCenterDelegate {

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

        switch response.actionIdentifier {
        case UNNotificationDefaultActionIdentifier:
            self.present()
            completionHandler()

        default:
            break;
        }
    }
}

希望对您有所帮助