Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if app did become active from a UILocalNotification

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:

  1. From a local notification.
  2. Just has become active :)

Is there a way to do it?

like image 902
wh1t3cat1k Avatar asked Nov 09 '10 17:11

wh1t3cat1k


3 Answers

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.

like image 189
Michael Gaylord Avatar answered Nov 04 '22 18:11

Michael Gaylord


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?

like image 30
jaredsinclair Avatar answered Nov 04 '22 17:11

jaredsinclair


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);
}
like image 10
naveed Avatar answered Nov 04 '22 18:11

naveed