7

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?

  • Would you show what did you try, your code? – Asperi Mar 25 '20 at 04:37
  • @Asperi, I updated my answer, thank you for your response – Paula Ysabelle Medina Mar 25 '20 at 05:32
  • 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. The following might be helpful [App Delegate Accessing Environment Object](https://stackoverflow.com/a/59380382/12299030) – Asperi Mar 25 '20 at 05:47

1 Answers1

2

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

Asperi
  • 228,894
  • 20
  • 464
  • 690