Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On remote push notification redirect to correct ViewController

I'm struggling to get my iOS app to behave as intended when opened via a remote push notification (using Swift). What I want is, when the app is opened via tapping of a push notification, it should jump directly to a certain ViewController, but still maintain the navigation stack. And to complicate it further, the destination view controller depends on the push message.

Example: My app is closed and I get a push notification: "You got a new message". I click the notification and the app opens and shows the new message, instead of the regular initial view controller. If my app is open and I get a push notification, nothing happens.

like image 754
Nikolaj Simonsen Avatar asked Jul 08 '15 14:07

Nikolaj Simonsen


2 Answers

In general, here are the methods to respond to a notification. Through these methods, you need to implement code that will present the appropriate view to your stack based on the notification.

To respond to notifications when the app is running in the foreground OR background implement the application:didReceiveRemoteNotification:fetchCompletionHandler: method. If you enabled the remote notifications background mode, the system launches your app (or wakes it from the suspended state) and puts it in the background state when a remote notification arrives. However, the system does not automatically launch your app if the user has force-quit it.

To respond to notifications when your app is running in the foreground NOT background implement the application:didReceiveRemoteNotification: method

To respond to notifications when your app is NOT running implement the application:willFinishLaunchingWithOptions: OR application:didFinishLaunchingWithOptions: method

like image 157
rmp Avatar answered Oct 19 '22 11:10

rmp


So what I ended up doing was:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
        if application.applicationState == .Inactive || application.applicationState == .Background {
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let navigationController = self.window?.rootViewController as? UINavigationController
            let destinationController = storyboard.instantiateViewControllerWithIdentifier("dashboard") as? DashboardViewController
            navigationController?.pushViewController(destinationController!, animated: false)
            let destinationController2 = storyboard.instantiateViewControllerWithIdentifier("notificationsSettings") as? AppSettingsTableViewController
            navigationController?.pushViewController(destinationController2!, animated: false)
        }
    }

So in didReceiveRemoteNotification I check which state the app comes from, and then I navigate to the viewController I want to present to the user. The reason I don't just go directly to the ViewController is because I want the navigation stack to be "intact" so the user can navigate back via the navigationController.

like image 36
Nikolaj Simonsen Avatar answered Oct 19 '22 10:10

Nikolaj Simonsen