Skip to content
Alann Maulana edited this page Dec 6, 2016 · 2 revisions

One of the most common use cases when we are monitoring for beacons in background mode is to trigger notification to a phone. This notification can be a sales or marketing message or an alert that a nearby service is available. Tapping on the notification launches your app and allows the user to see more information about the subject of the notification.

Let’s add a notification to show up whenever user enters the range of our monitored beacon. Here is a helper method to add to your AppDelegate class within application:didFinishLaunchingWithOptions: method:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // register user notification to shared application
    UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:setting];

   // initialize Cubeacon SDK, setup scanning etc...
}

- (void)showNotificationWithMessage:(NSString *)message {
    UILocalNotification *notification = [UILocalNotification new];
    notification.alertBody = message;
    notification.soundName = UILocalNotificationDefaultSoundName;
    [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}

For example, we will show a check in timestamp when a user entering and exiting a beacon region:

- (void)didEnterRegion:(CBRegion *)region {
    [self showNotificationWithMessage:@"Check In Successfully! You are already checked in at 7.35 o'clock, such a great morning. We hope today will be a great day for you!"];
}

- (void)didExitRegion:(CBRegion *)region {
    [self showNotificationWithMessage:@"Checked Out! Your checked out time is 17.00. See you tomorrow!"];
}

NOTE: We use a static string here for notification message. For more dynamic message, you can call some REST API to get notification parameters based on a beacon region. We recommended you to using Mesosfer Backend as a Service to handle your beacon's storyline.

Clone this wiki locally