Basel-525k / geofence_foreground_service

Apache License 2.0
3 stars 2 forks source link

Android listens to location in a Foreground Service #10

Open orkun1675 opened 4 weeks ago

orkun1675 commented 4 weeks ago

I was trying to understand why this plugin needs to show a notication on Android. Because per Android Geofencing documentation the app doesn't need to be running in the foreground to receive geofence events. See: https://developer.android.com/develop/sensors-and-location/location/geofencing#HandleGeofenceTransitions

I saw that the plugin starts a foreground service and requests location updates from the system: https://github.com/Basel-525k/geofence_foreground_service/blob/ff352619eb487dcc304062ec373cd35fdea2d918/android/src/main/kotlin/com/f2fk/geofence_foreground_service/GeofenceForegroundService.kt#L44-L52

Is this neccesary? Wouldn't the geofence be triggered by the system without this foreground location listener?

Basel-525k commented 3 weeks ago

You’re correct that the geofences are registered with the system and that the system triggers them. Ideally, the apps interested in these geofences should respond accordingly. However, if the app is not running—say, if it has been killed—the system will still send geofence signals, but there won’t be any app to handle them.

To ensure proper handling of geofence events, the app’s service needs to be running. If it isn’t, the system-generated geofence signals won't be processed.

This is the way I found working with Flutter at least, if there is a better way to handle this (as to turn the Flutter side on when receiving the event) then it would be a great addition to the plugin!

Note: having the service in the foreground makes it less likely to be killed by the system when the system is in need of resources