簡體   English   中英

點擊通知時,通過rootViewController顯示viewController

[英]Present viewController over rootViewController when a notification is tapped

當應用程序處於終止狀態時,我會收到推送通知。 如果我查看該圖片,我希望該應用正常加載,但應該在viewController上方顯示一個viewControllerrootViewController我添加了一個關閉按鈕,並且每當我點擊此按鈕時,我只想關閉此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實現了推送通知。 我希望應用程序像它一樣正常運行,就像當用戶來自通知時在啟動時在根視圖控制器上顯示一個視圖控制器一樣。

UPDATE
這是我的故事板的屏幕截圖: 在此處輸入圖片說明

因此,您首先需要使標簽欄控制器滿意。

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;
        }
    }
}

希望能幫助到你

也許您的rootView控制器為零。 即使您可能已經在appDelegate的完成發布中指定了rootviewController,也可以在呈現類之前嘗試設置rootview_Controller。

  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()
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM