'Geofencing api not working in android with pending intent
I'm trying to implement simple geofence app but its not getting triggered at all.
here is the code of what I've done so far -
class GeoFencingHelper(context: Context, private val task: Task, private val pendingIntent: PendingIntent){
private val geofencingClient = LocationServices.getGeofencingClient(context)
init {
Timber.e(task.id.toString())
}
@SuppressLint("MissingPermission")
fun initiateGeoFencing() {
val geoReq = createGeoFenceList()
geofencingClient.addGeofences(geoReq, pendingIntent).apply {
addOnSuccessListener {
Timber.e("added geofence!")
}
addOnFailureListener {
Timber.e("geofence failed!")
}
}
}
private fun createGeoFenceList(): GeofencingRequest {
val geofenceList = arrayListOf<Geofence>()
geofenceList.add(Geofence.Builder().apply {
setRequestId(task.id.toString())
setCircularRegion(
task.location.latitude,
task.location.longitude,
task.range.toFloat()
)
// Set the expiration duration of the geofence. This geofence gets automatically
// removed after this period of time.
setExpirationDuration(Geofence.NEVER_EXPIRE)
setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER or Geofence.GEOFENCE_TRANSITION_EXIT)
// Create the geofence.
}.build())
return getGeofencingRequest(geofenceList)
}
private fun getGeofencingRequest(
fenceList: List<Geofence>,
): GeofencingRequest {
return GeofencingRequest.Builder().apply {
setInitialTrigger(
if(task.reminderCondition == ReminderCondition.ON_ENTRY)
GeofencingRequest.INITIAL_TRIGGER_ENTER
else
GeofencingRequest.INITIAL_TRIGGER_EXIT)
addGeofences(fenceList)
}.build()}
I'm adding geofencing here
fun createGeoFence(context: Context, task: Task){
val geofencePendingIntent: PendingIntent by lazy {
task.range = 150.0
val intent = Intent(context, ReminderNotificationBroadcastReceiver::class.java)
intent.putExtra(TASK_ID, task.id)
if(Build.VERSION.SDK_INT > 30){
PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
}else{
PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
}
val geoFencingHelper = GeoFencingHelper(context, task, geofencePendingIntent)
geoFencingHelper.initiateGeoFencing()}
here is the broadcast receiver to show notification to user when geofence event gets triggered.
class ReminderNotificationBroadcastReceiver @Inject constructor(private val dao: TaskDao): BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val builder = NotificationCompat.Builder(context, "2222")
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("Reminder \"\".")
.setContentText("Reminder \".")
.setOngoing(true)
.setColor(Color.BLUE)
with(NotificationManagerCompat.from(context)) {
notify(2222, builder.build())
}}}
things I've done so far -
- disabled battery optimization.
- background location permission is allowed all the time.
Issue -
I used mock location apps like lockito and gps emulator to test the app. but the geofence event is not getting triggered, I also tried to build and test sample project provided by android codelab, but I'm facing same issue in that app as well.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
