推送通知 - 当app从后台关闭时设置rootViewController

时间:2018-02-26 10:20:22

标签: ios swift firebase push-notification

我已经使用firebase实现了推送通知。它的工作原理我想完成,而app也在前台也在后台。但是当应用程序从后台关闭并且通知从消息打开时,我想重置我的rootViewController。所以我在didReceive response:上更改了它,但是当应用程序重新打开时,它会回到旧的rootViewController(已在didFinishLaunchingWithOptions上设置)。

PS。默认的rootViewController是一个luncher动画viewController,它有一些带延迟的动画,然后它将重定向到另一个ViewController,它将被设置为rootViewController

以下是代码段:

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

    let userInfo = response.notification.request.content.userInfo

    let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)

    let state = UIApplication.shared.applicationState

    if state == .active {
        // foreground
        // Will return current viewController which is showing into screen
        let rootVC = getCurrentViewController()
        let reviewVC = mainStoryboard.instantiateViewController(withIdentifier: ReviewViewController.className) as! ReviewViewController
        rootVC?.present(reviewVC, animated: true, completion: nil)
    } else {
        // background
        let swVC = mainStoryboard.instantiateViewController(withIdentifier: "SWRevealViewController") as! SWRevealViewController
        window?.rootViewController = swVC
        window?.makeKeyAndVisible()
        // Will return current viewController which is showing into screen
        let rootVC = self.getCurrentViewController()
        let reviewVC = mainStoryboard.instantiateViewController(withIdentifier: ReviewViewController.className) as! ReviewViewController
        rootVC?.present(reviewVC, animated: true, completion: nil)
    }
    completionHandler()
}

0 个答案:

没有答案
相关问题