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

后端 未结 2 1664
抹茶落季
抹茶落季 2020-12-29 14:53

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
相关标签:
2条回答
  • 2020-12-29 15:41

    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.

    0 讨论(0)
  • 2020-12-29 15:42

    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.

    0 讨论(0)
提交回复
热议问题