Receive notification from beacon region detection during background monitoring

133 views Asked by At

I am trying to create a basic app in which I create a regionBootstrap for background monitoring of various types of beacons, just like in the reference app.

However, instead of bringing the app to the foreground upon entering a beacon region, I would like to simply display a 'You have entered a beacon region' local notification.

I presume this would need to be coded in the onCreate method within the 'extends Application implements BootStrapNotifier' class. But I also see that the intent for starting the main activity is instantiated within the didEnterRegion method, so is this in fact where I'd need to code the notification?

1

There are 1 answers

0
davidgyoung On

The easiest way to trigger a background notification is to make a custom Application class that implements BootstrapNotifier. You then put the notification code in the didEnterRegion callback method like so:

@Override
public void didEnterRegion(Region arg0) {
    NotificationCompat.Builder builder =
            new NotificationCompat.Builder(this)
                    .setContentTitle("Beacon Reference Application")
                    .setContentText("A beacon is nearby.")
                    .setSmallIcon(R.drawable.ic_launcher);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addNextIntent(new Intent(this, MonitoringActivity.class));
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
    builder.setContentIntent(resultPendingIntent);
    NotificationManager notificationManager =
            (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, builder.build());

}

You can see a full example of this in the reference application for the Android Beacon Library.