Панель навигации исчезает при нажатии на уведомление, когда приложение находится в фоновом режиме - PullRequest
0 голосов
/ 13 марта 2019

Мне нужно перейти к контроллеру уведомлений, когда пользователь нажимает на уведомление. Поэтому мне нужно перенаправить их из класса 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 не выполняется во второй раз (получение нулевого значения).

Помогите мне, если кто-то знает

1 Ответ

0 голосов
/ 13 марта 2019

Вы назначаете UINavigationController для rootViewController при первом получении уведомления: let nav: UINavigationController = UINavigationController() self.window?.rootViewController = nav Поэтому, когда вы получаете второе уведомление, yout rootViewController больше не UITabbarController, а скорее экземпляр UINavigationController, поэтому приведение 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
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...