S.J
S.J

Reputation: 3071

How to determine background app come to foreground in action of tapping local notification

Apple doc Getting the User’s Attention While in the Background says

Notifications are a way for an app that is suspended, is in the background, or is not running to get the user’s attention.

My app is waked by iOS because of region monitoring and is in the background and post a local notification. User tap the notification and app will be in foreground.

How to determine app comes in foreground because of notification tapped by user?

Which delegate method will contains the notification information.

didFinishLaunchingWithOption or didReceiveLocalNotification

Upvotes: 0

Views: 488

Answers (3)

balkaran singh
balkaran singh

Reputation: 2786

you can determine this by use of application.applicationState

 if(application.applicationState == UIApplicationStateInactive)
     {
           NSLog(@" Inactive");
      // when you tapping on notification

     }
    else if (application.applicationState == UIApplicationStateBackground)
    {
           NSLog(@" background");
    }
    else
    {
          NSLog(@" Active");

    }

Upvotes: 0

Moin Shirazi
Moin Shirazi

Reputation: 4425

you can detect what's your App's status when UILocalNotification fired and if - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification is called, this makes sure that local notification is received.

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateInactive) {
        // Application was in the background when notification was delivered.
    } else {

    }
}

Upvotes: 1

Nitin Gohel
Nitin Gohel

Reputation: 49730

If you your app running in background and you are tapped on LocalNotification Banner then you will get called following method:

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

after iOS 8:

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler

if app is not running in background you will get notification at:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
if ([launchOptions valueForKey:@"UIApplicationLaunchOptionsLocalNotificationKey"]) {
 // here you will get
}

Upvotes: 1

Related Questions