SwiftUI:当用户点击推送通知时,我该如何导航到特定的NavigationLink?

8

我正在使用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()
    }

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

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


你能展示一下你尝试过的东西,你的代码吗? - Asperi
@Asperi,我更新了我的答案,谢谢你的回复。 - Paula Ysabelle Medina
1
假设这个回调函数在AppDelegate中,你可以将AppDelegate作为你的共享对象的所有者,该共享对象被用作环境对象,这样你就可以在应用程序委托回调和场景委托中访问它,并注入到内容视图中。以下内容可能会有帮助:App Delegate Accessing Environment Object - Asperi
1个回答

2
假设此回调函数在AppDelegate中,您可以使AppDelegate成为共享对象的所有者,该共享对象用作环境对象,因此您可以在应用程序委托回调和场景委托中访问它,并注入到内容视图中。
请参阅我在App Delegate Accessing Environment Object中提出的类似情况的建议方法。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接