应用被终止状态下,点击推送通知后无法导航至ViewController

时间:2018-07-18 10:50:05

标签: ios swift3 push-notification

当应用程序处于活动状态或后台时,点击通知后,我能够导航到ChatMessageVC,但是当应用程序处于终止状态时,单击通知会将我带到ContactListVC而不是chatMessageVC。任何帮助都是非常可取的。

enter image description here

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
 if let option = launchOptions {
         let info = option[UIApplicationLaunchOptionsKey.remoteNotification]
                if (info != nil) {
                    self.application(application, didReceiveRemoteNotification: info as! [AnyHashable : Any])
                }
         }
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
    if state == .background {
        let navigation = storyboard.instantiateViewController(withIdentifier: "navigationContactPage")
        let navigationController = UINavigationController.init(rootViewController: navigation)
        self.window?.rootViewController = navigationController
        self.window?.makeKeyAndVisible()
        let viewController = storyboard.instantiateViewController(withIdentifier: "chatMessageVC") as! ChatMessagesVC

        navigationController.pushViewController(viewController, animated: true)
    }
}

1 个答案:

答案 0 :(得分:0)

如果您在应用终止时点击通知,则此方法将在您的应用内部调用。

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { let userInfo = response.notification.request.content.userInfo //Ask your backend developer to send chat id in notification user info. Some how you are succesfully navigating to contacts vc. In the same way open the chatVC using this chat id from contacts vc. }

相关问题