How do i find out what state the application is in as the result of a notification?
I have a UILocalNotification set up, and as far as i can see it i have 5 different scenarios:
- The app is not running, the user chooses to view the notification, so it launches the app.
- The app is not running, the user chooses to close the notification, then opens the app at a later date.
- The app is running in the background, the user chooses to view the notification, so it brings the app to the foreground.
- The app is running in the background, the user chooses to close the notification, then opens the app bringing it to the foreground at a later date.
- The app is running in the foreground.
How do i deal with t开发者_C百科hese 5 different scenarios?
Put your code into
application:didFinishLaunchingWithOptions:
. In the actionsNSDictionary
you will find the information about the notification.You can again check in
application:didFinishLaunchingWithOptions:
if the local notification is still active and take appropriate action.Put your code into
applicationWillEnterForeground:
Again the same spot, just check if there are active local notifications.
Here you can check in
application:didReceiveLocalNotification:
and either notify the user or not.
Not exactly sure what you're after, but the following might answer your question.
From the documentation:
When the system delivers a local notification, several things can happen, depending on the application state and the type of notification. If the application is not frontmost and visible, the system displays the alert message, badges the application, and plays a sound—whatever is specified in the notification. If the notification is an alert and the user taps the action button (or, if the device is locked, drags open the action slider), the application is launched. In the application:didFinishLaunchingWithOptions: method the application delegate can obtain the UILocalNotification object from the passed-in options dictionary by using the UIApplicationLaunchOptionsLocalNotificationKey key. The delegate can inspect the properties of the notification and, if the notification includes custom data in its userInfo dictionary, it can access that data and process it accordingly. On the other hand, if the local notification only badges the application icon, and the user in response launches the application, the application:didFinishLaunchingWithOptions: method is invoked, but no UILocalNotification object is included in the options dictionary.
If the application is foremost and visible when the system delivers the notification, no alert is shown, no icon is badged, and no sound is played. However, the application:didReceiveLocalNotification: is called if the application delegate implements it. The UILocalNotification instance is passed into this method, and the delegate can check its properties or access any custom data from the userInfo dictionary.
精彩评论