How to go directly to a specific View Controller if Notification has been opened
<p>I have an <code>UITabBarController</code> which presents in one tab a table of chats. If the user clicks on one row, a view controller will be displayed that presents the latest messages.</p>

<p>Now I am introducing Push notifications. The desired behavior would be to automatically go to the chat room if the user opens up the Push notification. Although there's plenty information available on how to handle these use cases, I do not succeed in implementing it. </p>

<p>Here's my code so far:</p>

<pre><code>   func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -&gt; Bool {
        // Override point for customization after application launch.

        if (!isUserLoggedIn()) {
            // show login vc
        } else {
            let protectedController = mainStoryboard.instantiateViewController(withIdentifier: "TabBarController") as! UITabBarController

            if let notificationPayload = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? NSDictionary {
                if let chatId = notificationPayload["chatId"] as? String {
                    print(chatId)
                    // show chat VC
                }
            }

            window!.rootViewController = protectedController
            window!.makeKeyAndVisible()
        }


        let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
            // Enable or disable features based on authorization.
        }
        application.registerForRemoteNotifications()

        return true
    }
</code></pre>

<p>The print statement will never be called but that may be due to the fact that the application is completely closed before opening the notification.</p>