当用户单击推送通知时

当用户单击推送通知时

本文介绍了Swiftui:当用户单击推送通知时,如何导航到特定的NavigationLink?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ios推送通知中使用FCM.用户点击推送通知时,如何导航到特定的导航链接.预先感谢您的帮助!

I am using FCM with my ios push notification. How can I navigate to a specific navigation link when the user taps the push notification. Thank you in advance for your help!

我知道我将处理此功能上的推送通知水龙头.

I understand that I will handle the push notification tap on this function.

    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 userNotificationCenterdidReceive response: \(messageID)")
        }
        // Print full message.
        print(userInfo)

        let application = UIApplication.shared

        if(application.applicationState == .active){
          print("user tapped the notification bar when the app is in foreground")
        }
        if(application.applicationState == .inactive)
        {
          print("user tapped the notification bar when the app is in background")
        }

        completionHandler()
    }

我还有一个@EnvironmentObject,它控制哪个tabItem是活动的,哪个NavigationLink是打开的.

I also have an @EnvironmentObject that controls which tabItem is active and which NavigationLink is open.

如何在UNNotificationResponse didReceive函数上访问此EnvironmentObject?

How can I access this EnvironmentObject on my UNNotificationResponse didReceive function?

推荐答案

假设此回调位于AppDelegate中,则可以使AppDelegate成为用作环境对象的共享库的所有者,因此您可以访问它并在应用程序委托中回调并在场景委托中注入到内容视图中.

Assuming this callback is in AppDelegate, you can make AppDelegate owner of your shared object which is used as environment object, so you have access to it and in app delegate callbacks and in scene delegate to inject into content view.

请在应用程序代理访问环境对象中查看我针对类似情况建议的方法

这篇关于Swiftui:当用户单击推送通知时,如何导航到特定的NavigationLink?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 14:02