Search code examples
iosswiftuilocalnotificationios13uiscenedelegate

Local notifications didReceive in iOS 13


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.


Solution

  • 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.