Swiftui:当用户单击推送通知时,如何导航到特定的 NavigationLink?

Pau*_*ina 6 push-notification firebase-cloud-messaging swiftui

我正在将 FCM 与我的 ios 推送通知一起使用。当用户点击推送通知时,如何导航到特定的导航链接。预先感谢您的帮助!

我了解我将处理此功能上的推送通知点击。

    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()
    }
Run Code Online (Sandbox Code Playgroud)

我还有一个@EnvironmentObject控制哪个 tabItem 处于活动状态以及哪个 NavigationLink 处于打开状态。

如何在我的 UNNotificationResponse didReceive 函数上访问这个 EnvironmentObject?

Asp*_*eri 2

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

请参阅我在应用程序委托访问环境对象中针对类似场景提出的方法