Search code examples
iosobjective-cuilocalnotification

IOS/Objective-C: Direct users to specific screen of app on clicking on notification


In the default - (void)applicationDidBecomeActive:(UIApplication *)application { method in appDelegate, Apple includes the comment:

      //This may also be a good place to direct someone
 to a certain screen if the app was in background but they just clicked on a notification

I am trying to do this for local notifications, but can't figure out how to direct the user to a certain screen. The docs say that you can check for an URL but even so, I don't know how to specify an NSUrl for a screen within an app.

This seems like a common use case, we click on notifications all the time that take us to specific parts of app, but not finding any good resources.

Thanks in advance for any suggestions.


Solution

  • You have to use didReceiveLocalNotification or didReceiveNotificationResponse delegate method in your AppDelegate.m file based on which Notification you have implemented

    For UILocalNotification:

    -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
    {
            // Your redirection code goes here like shown below
            MyViewController *controller = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController"];
            [self.window.rootViewController.navigationController pushViewController:controller animated:YES];
    }
    

    For UNNotification

    - (void)userNotificationCenter:(UNUserNotificationCenter *)center
           didReceiveNotificationResponse:(UNNotificationResponse *)response
           withCompletionHandler:(void (^)(void))completionHandler {
    
              // Your redirection code goes here like shown below
                MyViewController *controller = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController"];
                [self.window.rootViewController.navigationController pushViewController:controller animated:YES];
     }
    

    For more reference on UNNotification or handling Local Notification, please refer https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/SchedulingandHandlingLocalNotifications.html