marco-shock-wave
marco-shock-wave

Reputation: 11

How to open a specific page of the app when I click on a push notification

I'm developing an IOS app that receives push notifications. Is it possible to open a specific page of my app when I receive it and click on the notification?

Upvotes: 1

Views: 2978

Answers (3)

darkbreed
darkbreed

Reputation: 151

Our app does this, and to be quite honest, its a pain in the ass. But here is how we went about it:

  1. Have a root view controller that handles all incoming notifications (usually via some kind of data source or manager class)
  2. Create a model object to represent your notification and make sure this has a type property.
  3. Create a typedef enum to store the different kinds of notifications your app can expect to receive.
  4. The root view controller should implement a method with a switch statement that switches on the type of notification being handled.
  5. The root view controller can then load up the correct view controller for your notification, so you pass the notification object to the view controller if you need to use any information held within it.

This is a basic overview, and you will need to adjust it to suit your navigation stack depending on how complex it is. It may be that you need to pass that notification object down a view levels in your view hierarchy (as we have to do).

Depending on how your application behaves, you may also need to consider how your application handles returning from the background to the foreground i.e. what state is it in when the app receives its notification and reset your navigation stack where appropriate so that your navigation stack reacts correctly.

Upvotes: 1

Hussain Chhatriwala
Hussain Chhatriwala

Reputation: 849

You will receive control in the "didReceiveRemoteNotification" whenever user tap on the notification.

 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let navigationController = storyboard.instantiateViewControllerWithIdentifier("DetailViewController") as! DetailViewController
    self.window?.rootViewController?.presentViewController(navigationController, animated: true, completion: {}) 
 }

Here you can also detect if the application is in active state or inactive and perform the presentation accordingly.

So get rootViewController and present the screen you need to show. Also if you need to push the controller in already presented navigation stack, then you can fetch the navigation controller from window, then push the specific controller over it. The decision to show the controller you need to present can be taken from the userInfo received.

This way you can open any specific controller you want.

Upvotes: -1

Saheb Roy
Saheb Roy

Reputation: 5957

Yes it is possible and its part of a "thing" called Deep Linking. Basically the idea is to architect your project where each view controller would be mapped with a URL-Scheme (yeah that string thing). So that in applicationDidFinish you know which viewcontroller represents which URL-Scheme. Now send the data in the push notification along wit the URL-Scheme of the viewController, you want the data to be shown. Write code in the method to parse and push that particular controller. This is the basic rough idea of how it would/should work, I might have missed out a few details, but here is a good article and tutorial on it, if you wanna go through.

Hope this helps

Upvotes: 1

Related Questions