Swiftui:当用户点击推送通知时,如何导航到特定的 NavigationLink? [英] Swiftui: How can I navigate to a specific NavigationLink when user clicks on push notification?

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

问题描述

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

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 成为你的共享对象的所有者,它被用作环境对象,这样你就可以访问它并在 app 委托中回调和场景委托注入内容视图.

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.

请参阅App Delegate Accessing Environment Object

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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆