Is there a way to know if the application did become active from a local notification?
I know there is a way to test if the application was launched from a local notification alert; but if it just was sitting out there the background, and received a notification?
I need to run different code, when the app has become active:
Is there a way to do it?
I got the clue to the solution for this from @naveed's tip on checking the state of the application when the didReceiveNotification method is called. No need to check variables etc when the app resumes from the background.
On iOS7 and lower you handle the notifications like this:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
if (application.applicationState == UIApplicationStateInactive ) {
//The application received the notification from an inactive state, i.e. the user tapped the "View" button for the alert.
//If the visible view controller in your view controller stack isn't the one you need then show the right one.
}
if(application.applicationState == UIApplicationStateActive ) {
//The application received a notification in the active state, so you can display an alert view or do something appropriate.
}
}
Update for iOS 8: The following methods are now invoked when the app is opened from the background via a notification.
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler {
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler {
}
If the notifications are received while the app is in the foreground, use the methods:
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *) userInfo {
}
- (void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
}
Note that there is no need to check application state unless you want to support older versions of the OS in your app.
I'm afraid Sylter is incorrect. When an app enters the foreground from the background, either by a direct user action or by a user response to a UILocalNotification
, it does not trigger applicationDidFinishLaunchingWithOptions
. It does, however, call applicationWillEnterForeground
and applicationDidBecomeActive
. This can be verified with a couple of NSLogs
.
So, the problem remains: if an app is entering the foreground from the background, there is no way to discover if the app is entering the foreground in response to a user's response to a UILocalNotification
, or if it is merely entering the foreground. Except...
Once the app has entered the foreground, it will receive the method application:DidReceiveLocalNotification
: if the app entered the foreground in response to a UILocalNotification
.
The problem is that any UI changes made within the application:DidReceiveLocalNotification:
method in response to receiving the UILocalNotification occur after the app has already entered the foreground, creating a disturbing experience for the user.
Has anyone found a solution?
You can check the scenarios of either application is running or not when application received by following.
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
if (app.applicationState == UIApplicationStateInactive ) {
NSLog(@"app not running");
}else if(app.applicationState == UIApplicationStateActive ) {
NSLog(@"app running");
}
// Handle the notificaton when the app is running
NSLog(@"Recieved Notification %@",notif);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With