简体   繁体   English

从AppDelegate将远程通知推送到ViewController

[英]Push Remote Notification to ViewController from AppDelegate

I have a problem am trying to solve. 我有一个问题想解决。 I have a tabbar as my rootViewController defined like this window?.rootViewController = MainTabViewController() I need to push to a viewController From a remote push notification with the snippet below, but my app crash with cannot cast MainTabViewController to UINavigationViewController I know I cannot do that cast since MainTabViewController is not embedded in a NavigationViewController. 我有一个的TabBar作为我的rootViewController定义是这样window?.rootViewController = MainTabViewController()我需要推到一个的viewController从与下面的代码片段远程推送通知,但我的应用程序崩溃cannot cast MainTabViewController to UINavigationViewController我知道我做不到由于MainTabViewController未嵌入NavigationViewController中而进行的投射。 What is the best way to approach this? 解决此问题的最佳方法是什么?

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

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

    if let type = userInfo["type"] as? String, let data = userInfo["data"] as? String, let title = userInfo["title"] as? String {
        let params = ["type": type, "data": data, "title": title]
        Analytics.AddEvent(title: "Notification Open", params: params)

        let backItem = UIBarButtonItem()
        backItem.title = ""
        let navVC = window?.rootViewController as! UINavigationController
        NSLog("Controller Type: \(navVC.description)")
        navVC.navigationController?.navigationBar.tintColor = .black
        navVC.navigationItem.backBarButtonItem = backItem
        if type == "category_product"{
            //Show product list
            var params = [String: String]()
            params["CategoryName"] = title
            params["CategoryId"] = data
            Analytics.AddEvent(title: "Category Opened", params: params)

            let vc = ProductListingVC()
            vc.categoryName = title
            vc.category_id = data
            vc.hidesBottomBarWhenPushed = false
            navVC.pushViewController(vc, animated: true)
        }

You have tabBarController as rootViewController and you are type casting it into navigation controller, this is the issue in your code. 你有tabBarControllerrootViewController和你的类型转换成导航控制器,这是你的代码的问题。 You need to cast your rootViewController to MainTabViewController and then get the selected tab on your MainTabViewController if there you have navigation controller then push the ProductListingVC controller to that navigation controller else you need to present ProductListingVC . 您需要将rootViewControllerMainTabViewController ,然后在MainTabViewController上获得选定的选项卡(如果您有导航控制器),然后将ProductListingVC控制器推入该导航控制器,否则需要提供ProductListingVC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM