繁体   English   中英

从AppDelegate将远程通知推送到ViewController

[英]Push Remote Notification to ViewController from AppDelegate

我有一个问题想解决。 我有一个的TabBar作为我的rootViewController定义是这样window?.rootViewController = MainTabViewController()我需要推到一个的viewController从与下面的代码片段远程推送通知,但我的应用程序崩溃cannot cast MainTabViewController to UINavigationViewController我知道我做不到由于MainTabViewController未嵌入NavigationViewController中而进行的投射。 解决此问题的最佳方法是什么?

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

你有tabBarControllerrootViewController和你的类型转换成导航控制器,这是你的代码的问题。 您需要将rootViewControllerMainTabViewController ,然后在MainTabViewController上获得选定的选项卡(如果您有导航控制器),然后将ProductListingVC控制器推入该导航控制器,否则需要提供ProductListingVC

暂无
暂无

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

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