Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 658
Paula Ysabelle Medina Avatar asked Mar 25 '20 00:03

Paula Ysabelle Medina


1 Answers

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

like image 155
Asperi Avatar answered Oct 24 '22 10:10

Asperi