Skip to content
Alann Maulana edited this page Nov 19, 2016 · 1 revision

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 custom application class:

public void showNotification(String title, String message) {
    Intent notifyIntent = new Intent(this, MonitoringActivity.class);
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivities(this, 1234,
            new Intent[] { notifyIntent }, PendingIntent.FLAG_UPDATE_CURRENT);
    Notification notification = new Notification.Builder(this)
            .setSmallIcon(android.R.drawable.ic_dialog_info)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .build();
    notification.defaults |= Notification.DEFAULT_SOUND;
    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(5678, notification);
}

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

@Override
public void didEnterRegion(CBRegion region) {
    showNotification(
                "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!");
}

@Override
public void didExitRegion(CBRegion region) {
    showNotification(
                "Checked Out",
                "Your checked out time is 17.00. "
                        + "See you tomorrow!");
}

NOTE: We use a static string here for notification title and 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