Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS / XCode: how to know that app has been launched with a click on notification or on springboard app icon?

I would like to know if there is a way to know if an app (which can be closed or open in background) has been launched with a click on:

  • a notification (in the notification center) ?
  • or the app icon on the springboard ?

Thanks !!

like image 239
Regis_AG Avatar asked Oct 17 '12 15:10

Regis_AG


2 Answers

put this code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];

    if (notification) {
        // launched from notification
    } else {
        // from the springboard
    }
}

in your UIApplicationDelegate.

like image 75
Cfr Avatar answered Nov 02 '22 23:11

Cfr


From Apple Docs on Scheduling, Registering, and Handling Notifications :

iOS Note: In iOS, you can determine whether an application is launched as a result of the user tapping the action button or whether the notification was delivered to the already-running application by examining the application state. In the delegate’s implementation of the application:didReceiveRemoteNotification: or application:didReceiveLocalNotification: method, get the value of the applicationState property and evaluate it. If the value is UIApplicationStateInactive, the user tapped the action button; if the value is UIApplicationStateActive, the application was frontmost when it received the notification.

like image 26
Black Frog Avatar answered Nov 03 '22 00:11

Black Frog