0

I am implementing notifications in my app and I can not figure out how to go to a specific view controller when the user presses on the notification. Since iOS 13, my app is using SceneDelegate, but the function: userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) only gets called on in the AppDelegate.

How can I go to a specific view controller when the user taps on the notification when my app uses SceneDelegate? All the resources I found online dealt with iOS 12 or lower versions where you choose the view controller from AppDelegate.

fphelp
  • 1,544
  • 1
  • 15
  • 34
  • You can get your active scenes from the `UIApplication` and pick a scene to show the view controller in – Paulw11 Apr 12 '20 at 09:39

1 Answers1

2

There are several ways one simple way is to do post notification through NotficationCeneter.

NotificationCenter.default.post(name: Notification.Name("SOME_NAME"), object: nil, userInfo: nil)

And you should add an observer for this notification.

 NotificationCenter.default.addObserver(self, selector: #selector(someMethod(notification: )), name: Notification.Name("SOME_NAME"), object: nil)

There are also different options

For example, I create a simple Navigator class which is Singleton and who keeps the top NavigationController and all the knowledge about the current stack of views. And if I need to open some specific screen from the AppDelegate, I'm using something like:

MyNavigator.shared.goToSomeScreen()

Still, everything depends on what is your current code and what is your need, for sure you can find something that fits better for you.

m1sh0
  • 2,236
  • 1
  • 16
  • 21
  • Using notifications worked! The only problem is on iPad where if I have multiple windows open, all the windows go to the specified view controller. Do you happen to know a way where only the scene that's in focus will present the view controller? – fphelp Apr 13 '20 at 07:55
  • 1
    Sorry, not sure, how to do it. But maybe you can check if the scene losing focus to remove the observer and if it becomes on focus to add the observer. Or if you have the navigator idea, the navigator can have the knowledge of what is on focus. – m1sh0 Apr 13 '20 at 08:02