Determining if UILocalNotification fired with app in foreground or background

Mark Andrews picture Mark Andrews · Aug 24, 2011 · Viewed 13.2k times · Source

When receiving a UILocalNotification

  1. The method application:DidReceiveLocalNotification is called when the app is in Foreground.
  2. When the app is in the Background, it opens the application and then calls application:DidReceiveLocalNotification.

How can I determine which one of these scenarios are taking place? I would like to treat them differently.

If the app was in the Background, then the user indicated they want to look at the event. So in application:DidReceiveLocalNotification I don't want to ask them again, I just want to take them to the event.

But if the application is in the Foreground when the notification fires and calls application:DidReceiveLocalNotification, I want to ask the user if they want to view the event.

In essence I would have:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {

if (appLauncedFromBackground)
  // go to event
else
  // ask if user wants to view event

}

I've thought of writing a bool value to an NSUserDefault inside applicationWillEnterForeground:application and then reading and resetting it inside application:DidReceiveLocalNotification. But I thought if there was a way to test, that would be a much better route.

Thanks, Mark

EDIT ******************************

Thanks xuzhe but that isn't going to work in this situation. Here is a little more detail that may explain why it wont work and maybe help someone in answering this for me:

The UILocalNotification that I am setting here is an event like a calendar event that is scheduled to fire at a user selected time so when scheduling the notification I will have no idea what the user is going to be doing when the notification fires. I am using the userInfo of the notification to store data about the event that is scheduled but setting a value like inBackground as suggested wont work.

I do know it will be one of two situations if the user decides they want to "view" the event. (this all will assume the app is either in the background or foreground and NOT quit).

1 - If it fires while the app is not being used && the app is in the background and not quit, iOS will notify the user that an event has occurred and provide an option for going to the responsible app (which is mine in this case). At that point assuming the user says "yes take me to this cool app" it will open the app from the background and call the application:DidReceiveLocalNotification where I can get all the notification userInfo.

2 - The user, by chance, happens to be in my application when the scheduled event takes place. At that point the application:DidReceiveLocalNotification method is called again where I can then get all the notification userInfo.

But it is at this point that I want to know which of the two scenarios just took place 1 or 2.

Hope this additional detail about my use of the UILocalNotification will help in answering this question. Thanks in advance - mark

END EDIT ****************************

Answer

xuzhe picture xuzhe · Aug 24, 2011

Maybe the easiest way is when your App is in Foreground, do not send a LocalNotification but just take your users to the event.

But if you insist on doing it by using LocalNotification, here is an easy way to detect what's your App's status when UILocalNotification fired.

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    //[super application:application didReceiveLocalNotification:notification]; // In most case, you don't need this line
    UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateInactive) {
        // Application was in the background when notification was delivered.
    } else {

    }
}