We've had a working Geofencing system in our app that used to work just fine. Recently, I checked out a reported issue and found that all transition events seem to have a -1 transition type which, according to the docs, means that the intent specified in fromIntent(Intent) is not generated for a transition alert.
I don't understand how that can be, though. We didn't change anything in our implementation and apart from that, the pending intent is triggered every time I trigger an enter/exit event, through the emulator's location setting.
Here's our flow:
// we create a geo-fence from an FCM notification payload
override fun createGeofenceFromEvent(geoEventData: GeoEventData) =
Geofence.Builder()
.setRequestId(geoEventData.id)
.setCircularRegion(
geoEventData.latitude,
geoEventData.longitude,
geoEventData.radius
)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER or Geofence.GEOFENCE_TRANSITION_EXIT)
.build()
// then we add it to the client (it's added successfully) like so
override fun addGeofenceToClient(geofence: Geofence) {
App.instance.geofencingClient.addGeofences(
getGeofencingRequest(geofence),
App.instance.geofencePendingIntent
).run {
addOnSuccessListener {
Log.d(tag, "Geofence with id ${geofence.requestId} added to client!")
}
addOnFailureListener {
Log.d(tag, "Failed to add geofence with id ${geofence.requestId} to client!")
it.printStackTrace()
}
}
}
// here's the pending intent declaration
val geofencePendingIntent: PendingIntent by lazy {
val intent = Intent(appContext, GeofenceBroadcastReceiver::class.java)
PendingIntent.getBroadcast(appContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
}
Then in our GeofenceBroadcastReceiver class it always logs -1 now when using:
override fun onReceive(context: Context?, intent: Intent?) {
if (!checkForNotificationUserChoice(context)) return
val geofencingEvent = GeofencingEvent.fromIntent(intent!!)
if (geofencingEvent.hasError()) {
val errorMessage = GeofenceStatusCodes
.getStatusCodeString(geofencingEvent.errorCode)
Log.e(TAG, errorMessage)
return
}
val geofenceTransition = geofencingEvent.geofenceTransition
Log.e(TAG,"$geofenceTransition")
...
}
Any ideas what I'm missing here? I've made sure that I'm testing on an emulator with Google Play Services & APIs enabled, that GPS/Location is enabled and that location updates work, that the “Always” option is enabled for location tracking, etc.
To sum up:
- Geo-fence payloads reach the app successfully through FCM
- Geo-fences are created and added to the client successfully as well
- Enter/exit events triggered by changing the emulator's location also seem to be received by the system correctly
- BUT, the pending intent that handles the transport of the geo-fence event payload seems to “lose” its transition type payload.