当应用程序被终止状态时,点击推送通知后无法导航到 ViewController

Posted

技术标签:

【中文标题】当应用程序被终止状态时,点击推送通知后无法导航到 ViewController【英文标题】:Unable to navigate to ViewController after tapping the push notification when app is killed state 【发布时间】:2018-07-18 10:50:04 【问题描述】:

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

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)
    

【问题讨论】:

一旦应用进入killed状态,就会调用AppDelegate的applicationWillTerminate函数,将Bool值和chatID存储在NSUserDefault中,当调用函数applicationDidBecomeActive时加载所需的视图控制器。希望这会对你有所帮助。 @PrakashTripathi 我在通知有效负载中获得了一个 sender_id,我将其存储在 NSUserDefaults 中,同时接收远程通知并导航到 ChatMessageVC 并从 NSUserDefaults 获取 sender_id 并从 firebase 加载聊天。但是在这里我无法在应用程序未运行时点击通知后导航到 ChatMessageVC 是的,你无法导航到 chatView 控制器,因为应用程序进入终止状态后,视图控制器的堆栈被清除,所以在这种情况下,使用 URL 加载视图控制器 @PrakashTripathi 我做的和你分享的 URL 说的一样。我正在从 rootViewController(导航控制器)导航到 chatMessageVC 你添加了 "aps" : "content-available" : 1 ,在推送通知和调试控制台中是否在终止状态下调用了 didReceiveNotificationResponse 函数。 【参考方案1】:

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

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.

【讨论】:

我正在使用 FCM 进行推送通知。我在 ChatMessageVC 上获得了 contact_id 和我需要的所有详细信息,我面临的问题是在点击通知后,我导航到 contactPage 而不是 chatMessageVC

以上是关于当应用程序被终止状态时,点击推送通知后无法导航到 ViewController的主要内容,如果未能解决你的问题,请参考以下文章

在 iOS 10 下使用 FCM 在前台单击推送通知后无法导航到特定的 viewController

当应用程序被杀死时,无法在推送通知单击时导航到特定屏幕(Flutter android)

当应用程序被杀死时,在推送通知后执行操作:react-native,IOS

当应用程序处于终止状态而没有用户交互时如何清除本地通知。

当应用程序在后台或终止时,iOS 设备不会推送通知。导航到下一个屏幕在 Flutter 的 IOS 设备中也不起作用?

当应用程序未激活(已终止)时,推送通知无法正常工作