Determining if UILocalNotification fired with app in foreground or background Determining if UILocalNotification fired with app in foreground or background objective-c objective-c

Determining if UILocalNotification fired with app in foreground or background


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 {    }}


This is what I added to the bodies of these functions...application didFinishLaunchingWithOptions:

NSUserDefaults* userDefaults= [NSUserDefaults standardUserDefaults];UILocalNotification *localNotif =[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];if (localNotif) {  //launched from notification    [userDefaults setBool:YES forKey:@"deactivate in-app notification"];    [userDefaults synchronize];}else{    [userDefaults setBool:NO forKey:@"deactivate in-app notification"];    [userDefaults synchronize];}

In applicationDidEnterBackground:

NSUserDefaults* userDefaults= [NSUserDefaults standardUserDefaults];[userDefaults setBool:YES forKey:@"deactivate in-app notification"];[userDefaults synchronize];

In applicationWillEnterForeground:

NSUserDefaults* userDefaults= [NSUserDefaults standardUserDefaults];[userDefaults setBool:YES forKey:@"deactivate in-app notification"];[userDefaults synchronize];

applicationDidBecomeActive:

NSUserDefaults* userDefaults= [NSUserDefaults standardUserDefaults];[userDefaults setBool:NO forKey:@"deactivate in-app notification"];[userDefaults synchronize];

didReceiveLocalNotification:

NSUserDefaults* userDefaults= [NSUserDefaults standardUserDefaults];if(![userDefaults boolForKey:@"deactivate in-app notification"]){    UIAlertView* alert= [[UIAlertView alloc]initWithTitle:@"My App" message:notif.alertBody delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];    [alert show];    [alert release];    NSUserDefaults* userDefaults= [NSUserDefaults standardUserDefaults];    [userDefaults setBool:NO forKey:@"deactivate in-app notification"];    [userDefaults synchronize];}

Now the two notifications are gone (particularly the one appearing when the app is in background and you open it from the notification alert.


In didReceiveLocalNotification you can check the application state:

[UIApplication shareApplication].applicationState

If it's equal to UIApplicationStateInactive you know that the app was in the background and the user opened it with the local notification alert.

if it's UIApplicationStateActive you may want to display your own alert to the user.