Reputation: 890
In my app I have multiple geofences and I want them to have unique pending intents, with different extra data. But what is happenning is that for all my geofences pending intents that are being triggered are one that was added for last geofence and not one that was assigned to the specific one the user just enetered.
So for example when I would have 2 geofences and for first one I would add extra string "AAA" to pending intent, then add second geofence with extra "BBB" and enter first geofence I would get notification with "BBB" instead of correct "AAA" What am I doing wrong? Here is my code where I add single new geofence:
public void addGeofencing(final MyObject myObject){
Geofence geofence = (new Geofence.Builder()
.setRequestId(myObject.getId())
.setCircularRegion(
myObject.getLat(),
myObject.getLon(),
RADIUS_IN_METERS
)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
Geofence.GEOFENCE_TRANSITION_EXIT)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.build());
client.addGeofences(getGeofencingRequest(geofence),getGeofencePendingIntent(myObject.getExtraString))
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
System.out.println("GEOFENCE WORKED");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
System.out.println("GEOFENCE FAILED");
}
});
}
private GeofencingRequest getGeofencingRequest(Geofence geofence) {
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
builder.addGeofence(geofence);
return builder.build();
}
private PendingIntent getGeofencePendingIntent(String extraString) {
Intent intent = new Intent(context, GeofenceTransitionsIntentService.class);
intent.putExtra("extra",extraString);
return PendingIntent.getService(context, 0, intent, FLAG_UPDATE_CURRENT);
}
Upvotes: 1
Views: 540
Reputation: 3809
I see you are using a generic function to build your PendingIntents
and
return PendingIntent.getService(context, 0, intent, FLAG_UPDATE_CURRENT);
specifies the ID 0
to whatever PendingIntent
you are building.
Since you are using the flag FLAG_UPDATE_CURRENT
, you are just overriding the previously built PendingIntent
( AAA
).
Extras is the only thing that changes between the two Intents
but they are not taken into consideration: see how Android compares PendingIntents.
Answer: use a different requestCode
for each PendingIntent
.
Upvotes: 3