从AppDelegate将远程通知推送到ViewController

时间:2017-12-07 11:30:13

标签: ios swift firebase push-notification

我想解决一个问题。 我有一个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)
        }

1 个答案:

答案 0 :(得分:0)

class A { private String fieldOne; private String fieldTwo; private Integer fieldThree; private Integer fieldFour; } tabBarController,并且您将其输入导航控制器,这是代码中的问题。您需要将rootViewController转换为rootViewController,然后在MainTabViewController上获取所选标签,如果您有导航控制器,则将MainTabViewController控制器推送到该导航控制器,否则您需要提交ProductListingVC