Я делаю приложение для заметок, которое запускает уведомления при вводе геозоны, если я делаю более 1 заметки с одним и тем же местоположением, когда я вхожу только для триггеров геозоны и показывает только 1 уведомление.
Здесь это код: Broadcast Receiver
class GeofenceBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
//if (intent.action == ACTION_GEOFENCE_EVENT) {
val geofencingEvent = GeofencingEvent.fromIntent(intent)
if (geofencingEvent.hasError()) {
val errorMessage = errorMessage(context, geofencingEvent.errorCode)
Log.e(TAG, errorMessage)
return
}
if (geofencingEvent.geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER) {
Log.v(TAG, "context.getString(R.string.geofence_entered)")
val fenceId = when {
geofencingEvent.triggeringGeofences.isNotEmpty() ->
geofencingEvent.triggeringGeofences
else -> {
Log.e(TAG, "No Geofence Trigger Found! Abort mission!")
return
}
}
/*val foundIndex = GeofencingConstants.LANDMARK_DATA.indexOfFirst {
it.id == fenceId
}*/
// Unknown Geofences aren't helpful to us
/*if ( -1 == foundIndex ) {
Log.e(TAG, "Unknown Geofence: Abort Mission")
return
}*/
for (geofence in fenceId){
sendGeofenceEnteredNotification(
context, geofence.requestId, intent.getStringExtra("note")!!
)
}
}
}
// }
}
-------------- Методы добавления геозоны ----------------- ----------
private fun createPendingIntent(context: Context, note: Note) : PendingIntent{
val intent = Intent(context, GeofenceBroadcastReceiver::class.java)
intent.action = "MainActivity.treasureHunt.action.ACTION_GEOFENCE_EVENT"
intent.putExtra("note", note.note)
return PendingIntent.getBroadcast(context, note.date.toInt(), intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
fun addGeofenceForNote(note: Note, context: Context) {
val geofencingClient = LocationServices.getGeofencingClient(context)
val geofence = Geofence.Builder()
.setRequestId(note.id)
.setCircularRegion(note.latitude, note.longitude, GeofencingConstants.GEOFENCE_RADIUS_IN_METERS)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
.build()
// Build the geofence request
val geofencingRequest = GeofencingRequest.Builder()
.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
.addGeofence(geofence)
.build()
geofencingClient.addGeofences(geofencingRequest, createPendingIntent(context, note))?.run {
addOnSuccessListener {
Log.e("Add Geofence", geofence.requestId)
}
addOnFailureListener {
if ((it.message != null)) {
Log.w("TAG", it.message!!)
}
}
}
}
Я сделал 3 заметки с одним и тем же местоположением, а вторая - та, которая запускает радиовещательный приемник.
PS, даже если геозона Я нахожусь в совершенно разных местах, я все еще могу получить только 1 уведомление. Насколько я вижу, все ожидающие намерения и уведомления получают разные идентификаторы.
Ты заранее