Open app in specific view when user taps on push notification with iOS Swift

28,307

Solution 1

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:

  1. In your PFPush, use setData to add a key to your payload with the identifier: notification.setData(["alert":"your notification string", "identifier":"firstController"])

  2. Set the identifier on each ViewController by selecting it and changing the following values

Setting the Storyboard ID

  1. Make your Push Notification send the storyboard ID in its payload with the key identifier
  1. 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
}

Solution 2

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.

Solution 3

 UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (notification)
    {
        [self application:application didReceiveRemoteNotification:(NSDictionary*)notification];
    }
Share:
28,307
mechdon
Author by

mechdon

Updated on March 08, 2020

Comments

  • mechdon
    mechdon about 4 years

    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.

  • mechdon
    mechdon over 8 years
    I'm using PFPush and at the moment, my Push Notification only contain a string of text
  • kabiroberai
    kabiroberai over 8 years
    @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
  • kabiroberai
    kabiroberai over 8 years
    @mechdon how were you planning to specify the ViewController to open though?
  • mechdon
    mechdon over 8 years
    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.
  • kabiroberai
    kabiroberai over 8 years
    @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"]
  • kabiroberai
    kabiroberai over 8 years
    @mechdon I have just updated my answer. I hope it works now :)
  • mechdon
    mechdon over 8 years
    Thanks very much kabiroberai, your answers have benefitted me a lot. Yeah, it's working!
  • kabiroberai
    kabiroberai over 8 years
    @mechdon no problem, I'm glad that I could help :)
  • mechdon
    mechdon over 8 years
    Satyam, 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.
  • Javi Rando
    Javi Rando about 8 years
    I am new with Swift and I don't understand the first part where you use setData in PFPush (first point) and I also don't get the third one. Would you mind showing some more code. Thanks in advance!
  • kabiroberai
    kabiroberai about 8 years
    @JaviRando I'm a bit rusty on Parse now, as Facebook has announced plans to discontinue it on their own servers, but basically the setData function allows you to send any data in your push notification along with the actual alert. This data is known as the payload. For example, I could make a push with the payload ["alert":"Open StackOverflow", "url":"http://stackoverflow.com"]. I would then fetch the url key and possibly open the URL in Safari. In the third step I mean to say that you need to send out the push after making it, for example by using notification.sendPushInBackground()
  • Javi Rando
    Javi Rando about 8 years
    I got it but I still don't know in which part of my code it would be. Could you add an example payload?
  • Dashrath
    Dashrath over 7 years
    I added this code in didFinishLaunchingWithOptions BUT when app is already open or active in background then this code is not being executed ever. Any ideas on how we can get it working when app is active in background ?
  • kabiroberai
    kabiroberai over 7 years
    @Dashrath if you want this to work in the foreground as well, you have to add the code in step 4 to application:didReceiveRemoteNotification:fetchCompletionHand‌​ler: instead. Instead of payload, you can then access the identifier via the userInfo dictionary (let identifier = userInfo["identifier"] as? String)
  • Narasimha Nallamsetty
    Narasimha Nallamsetty almost 6 years
    How do we get identifier as part of notification?. My doubt is how can backend developers send ViewController identifier? Please clarify.
  • Narasimha Nallamsetty
    Narasimha Nallamsetty almost 6 years
    @Satyam, How come we know to which controller we need to navigate based on the push notification? Please clarify this.
  • Satyam
    Satyam almost 6 years
    @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.