My app allows remote push notifications to a user. How do I enable it to be opened in a specific view controller when the user taps on the push notification? I want the app to open and navigate to a specific view controller depending on the push notification received.
3 Answers
To do this you need to set an identifier
for each ViewController
that your app may be opened with, and then check the payload
in the launchOptions
argument of application:didFinishLaunchingWithOptions:
in your AppDelegate
Here are the steps to doing this:
In your
PFPush
, usesetData
to add a key to your payload with the identifier:notification.setData(["alert":"your notification string", "identifier":"firstController"])
Set the
identifier
on eachViewController
by selecting it and changing the following values
- Make your Push Notification send the storyboard ID in its
payload
with the keyidentifier
- Check for the ID in application:didFinishLaunchingWithOptions: by adding the following at the end of the function:
if let payload = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary, identifier = payload["identifier"] as? String {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier(identifier)
window?.rootViewController = vc
}
-
I'm using PFPush and at the moment, my Push Notification only contain a string of text– mechdonNov 14, 2015 at 8:57
-
@mechdon send the data along with the push using the method
setData
on your push notification with a dictionary["identifier":"firstController"]
or whatever your identifier should be Nov 14, 2015 at 9:01 -
@mechdon how were you planning to specify the
ViewController
to open though? Nov 14, 2015 at 9:02 -
I looked up the PFPush Class Reference. It says if I use setMessage, this will overwrite any data specified in setData. I need to set message in addition to the identifier.– mechdonNov 14, 2015 at 9:38
-
@mechdon add a key in your dictionary called
alert
and set its value to the alert you want to send like so:["alert":"your notification string", "identifier":"firstController"]
Nov 14, 2015 at 9:41
In the AppDelegate, you will get a delegate callback "didFinishLoading" or "didReceivePushNotification" methods (based on your app is in background or foreground). In that method get the top most view controller's instance, then create the specific view controller that you want to show and present/push from top most view controller.
-
3Satyam, thanks for your answer which is also correct. But I gave kabiroberai's answer the tick, because his is the most complete solution to my question.– mechdonNov 14, 2015 at 9:53
-
@Satyam, How come we know to which controller we need to navigate based on the push notification? Please clarify this. May 30, 2018 at 5:45
-
@NarasimhaNallamsetty, your push notification payload must have necessary information. May be your server guys have to add more info to the payload. Visit the Apple website. Based on the extra information, you can make out which view controller do you have to navigate.– SatyamMay 30, 2018 at 6:11
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (notification)
{
[self application:application didReceiveRemoteNotification:(NSDictionary*)notification];
}