簡體   English   中英

當應用程序處於后台狀態時,單擊通知時導航欄消失

[英]Navigation bar disappear when click on notification when app is in background state

用戶單擊通知時需要導航到通知控制器。因此,需要從AppDelegate類重定向它們

這是代碼:

func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo
        // Print message ID.
        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID: \(messageID)")

              if  let cu = self.window?.rootViewController as? UITabBarController
              {

            print(cu)
                    let nav: UINavigationController = UINavigationController()

                    self.window?.rootViewController = nav

                    let str = UIStoryboard.init(name: "Main", bundle: nil)

                    let rr = str.instantiateViewController(withIdentifier: "NotificationListViewController")

                    nav.setViewControllers([cu,rr], animated: true)

                }
        }



        // Print full message.
        print(userInfo)

        completionHandler()
    }
}

上面的方法是有效的,意味着只有第一次導航到通知頁面時,才是第二次嘗試去儀表板頁面。

if條件第二次失敗(獲取nil值)。

如果有人知道,請幫助我

您在第一次收到通知時將UINavigationController分配給rootViewController: let nav: UINavigationController = UINavigationController() self.window?.rootViewController = nav因此,當您收到第二個通知時,rootViewController不再是UITabbarController,而是UINavigationController的實例,因此鑄造let cu = self.window?.rootViewController as? UITabBarController let cu = self.window?.rootViewController as? UITabBarController將失敗。

您不應該創建新的UINavigationController。 相反,您應該推送視圖控制器

if let cu = self.window?.rootViewController as? UITabBarController {
   let str = UIStoryboard.init(name: "Main", bundle: nil)
   let rr = str.instantiateViewController(withIdentifier: "NotificationListViewController")
   cu.pushViewController(rr, animated: true)
}

或轉到您的UITabBarController的適當選項卡:

if let cu = self.window?.rootViewController as? UITabBarController {
    let notificationsTabIndex = 1 //use proper tab number    
    cu.selectedIndex = notificationsTabIndex
}

暫無
暫無

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

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