0

I am registering to receive geofence broadcasts and it worked fine with SDK 25. But with SDK 27 this is now broken.

I understand that I cannot register through manifest file to receive implicit broadcasts, but I am registering a class specific broadcast like this:

Intent geofencingIntent = new Intent("GEOFENCE_BROADCAST");
geofencingIntent.putExtra("ID", geofenceId);
geofencingIntent.setClass(context, GeofenceBroadcastReceiver.class);

PendingIntent geofencingPendingIntent = PendingIntent.getBroadcast(context, 1, geofencingIntent, PendingIntent.FLAG_UPDATE_CURRENT);

final Task<Void> geofenceTask = mGeofencingClient.addGeofences(geofencingRequest, geofencingPendingIntent);

However, I am not receiving this broadcast.

Any advice on how why this "explicit" broadcast is not received and the workaround. It is important that the broadcast is received even if the app is not running or active.

Also I fail to understand the logic behind removing this feature. Yes, it is to save battery by preventing applications wake up unnecessarily. But having the ability to receive broadcasts while the app is not running seems like a pretty important feature to me. And encouraging job scheduling, which can only be set for 15mins intervals minimum seems rather restrictive.

I have checked the following answers and do not believe this is a duplicate: Android 8.0 Oreo AlarmManager with broadcast receiver and implicit broadcast ban

Lifetime of BroadcastReceiver with regard to Android O changes

Thank you.

madu
  • 5,232
  • 14
  • 56
  • 96
  • 1
    I asked a similar question but no answers yet https://stackoverflow.com/questions/50820154/geofence-using-pendingintent-getbroadcast-not-working – Psypher Jun 15 '18 at 16:16

2 Answers2

0

First of all if you haven't registered braodcast at runtime please register it. this will help you with that.

If you register broadcast with the Application context, you receive broadcasts as long as the app is running.

And still if it get failed then through onFailedListener you may check the exception.

mGeofencingClient.addGeofences(getGeofencingRequest(), getGeofencePendingIntent())
        .addOnSuccessListener(this, new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                // Geofences added
                // ...
            }
        })
        .addOnFailureListener(this, new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                // Failed to add geofences
                // ...
            }
        });

Try it with service if you could:

private PendingIntent getGeofencePendingIntent() {
        // Reuse the PendingIntent if we already have it.
        if (mGeofencePendingIntent != null) {
            return mGeofencePendingIntent;
        }
        Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
        // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when
        // calling addGeofences() and removeGeofences().
        mGeofencePendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.
                FLAG_UPDATE_CURRENT);
        return mGeofencePendingIntent;
    }

Also I fail to understand the logic behind removing this feature.

It's very well described in docs

Man
  • 2,720
  • 2
  • 13
  • 21
-2

(code) However, I am not receiving this broadcast.

Any advice on how why this "explicit" broadcast is not received and the workaround. It is important that the broadcast is received even if the app is not running or active.

Also I fail to understand the logic behind removing this feature. Yes, it is to save battery by preventing applications wake up unnecessarily. But having the ability to receive broadcasts while the app is not running seems like a pretty important feature to me. And encouraging job scheduling, which can only be set for 15mins intervals minimum seems rather restrictive