Reputation: 11
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
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:
type
property.typedef enum
to store the different kinds of notifications your app can expect to receive.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
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
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