Swiftui: How can I navigate to a specific NavigationLink when user clicks on push notification?

久未见 提交于 2020-05-07 06:04:27

问题


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()
    }

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

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


回答1:


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.

Please see my proposed approach for similar scenario in App Delegate Accessing Environment Object



来源:https://stackoverflow.com/questions/60841491/swiftui-how-can-i-navigate-to-a-specific-navigationlink-when-user-clicks-on-pus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!