Receive Nearby API messages on Android when screen off

321 views Asked by At

I want my Android app to recieve nearby messages when screen is off. Ideally also when app is not in foreground.

Is this possible? Which strategy do I use?

1

There are 1 answers

0
ReyAnthonyRenacia On

Subscribe in the background

When your app subscribes to beacon messages in the background, low-power scans are triggered at screen-on events, even when your app is not currently active. You can use these scan notifications to "wake up" your app in response to a particular message. Background subscriptions consumes less power than foreground subscriptions, but have higher latency and lower reliability.

// Subscribe to messages in the background.
private void backgroundSubscribe() {
    Log.i(TAG, "Subscribing for background updates.");
    SubscribeOptions options = new SubscribeOptions.Builder()
            .setStrategy(Strategy.BLE_ONLY)
            .build();
    Nearby.Messages.subscribe(mGoogleApiClient, getPendingIntent(), options);
}

private PendingIntent getPendingIntent() {
    return PendingIntent.getBroadcast(this, 0, new Intent(this, BeaconMessageReceiver.class),
            PendingIntent.FLAG_UPDATE_CURRENT);
}