Pushing notifications from iBeacon when the app is in the background or killed

One of the greatest features of iBeacon is that apps can be running in the background or even be killed (removed from the app switcher on iOS) and keep Monitoring for beacons.

Region Monitoring allows an app to determine when it enters and exits the range of a beacon region. To get a better grasp of how regions work, take a look at another article in our Knowledge Base: What is a beacon region?

Background Monitoring on iOS and Android

To make Monitoring work in the background on iOS, your app needs to request ‘always’ authorization for accessing Location Services. Learn about handling Location Services.

Remember that exiting a region has a built-in 30-second delay, to avoid false positives. During those 30 seconds, the app cannot receive any signal from the beacons in particular region to trigger an exit event. Also, when the phone is locked, Monitoring is less responsive, so both enter and exit events might launch with a delay. That’s because iOS reduces Bluetooth scanning frequency when the device is locked, to preserve the battery.

After an exit/enter event, the app is woken up and remains active for 10 seconds.

Background Monitoring on Android is described in a separate article:

How Monitoring in the background works on Android?

Monitoring when the app is killed (iOS only)

Even if the app is not running, location events (related to the beacons in this case) are handled the same way as any other app launching events. Every time a phone enters or exits a region while the app is terminated, it will be automatically launched.

application:didFinishLaunchingWithOptions: method (of AppDelegate class) is called with UIApplicationLaunchOptionsLocationKey key existing in launchOptions parameter.

When you verify this key exists (so location was the reason that your app was launched) you should create new instance of ESTBeaconManager class, set delegate to AppDelegate object (or any other object that is working as ESTBeaconManagerDelegate and was created before this event occurred) and start monitoring.

Region you are passing to the startMonitoringForRegion: method is not important, as ESTBeaconManager delegate will receive the most recent region information. You can just pick any of the ones your app registered in iOS. After Monitoring is revoked, app will automatically receive most recent entered/exited region event in beaconManager:didEnterRegion: or beaconManager:didExitRegion: method.

Sample code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{ 
  if([launchOptions objectForKey:@"UIApplicationLaunchOptionsLocationKey"])
  {
    self.beaconManager = [ESTBeaconManager new];
    self.beaconManager.delegate = self;
    // don't forget the NSLocationAlwaysUsageDescription in your Info.plist
    [self.beaconManager requestAlwaysAuthorization];
    [self.beaconManager startMonitoringForRegion:[[ESTBeaconRegion alloc]
                                                  initWithProximityUUID:ESTIMOTE_PROXIMITY_UUID
                                                  identifier:@"AppRegion"]];
  }
  return YES;
}

-(void)beaconManager:(ESTBeaconManager *)manager didEnterRegion:(ESTBeaconRegion *)region
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = @“Enter region”;
notification.soundName = UILocalNotificationDefaultSoundName;

[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}

-(void)beaconManager:(ESTBeaconManager *)manager didExitRegion:(ESTBeaconRegion *)region
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = @“Exit region”;
notification.soundName = UILocalNotificationDefaultSoundName;

[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}

Just like with background Monitoring, after an exit/enter event is triggered, the app is woken up and remains active for 10 seconds.

Ranging

Remember that Ranging, the method used for approximating distance from individual beacons, should be used in the foreground (when the app is active on screen). For more information, read the following articles: