[Feature] Android secondary position stream for foreground notification updates#1406
[Feature] Android secondary position stream for foreground notification updates#1406androidseb wants to merge 7 commits into
Conversation
geolocator_android/android/src/main/java/com/baseflow/geolocator/GeolocatorPlugin.java geolocator_android/android/src/main/java/com/baseflow/geolocator/StreamHandlerImpl.java
|
Hi @androidseb I was actually looking to achieve the same thing using geolocator and posted #1569 recently. I hadn't seen this PR until now. A design question: what do you think about being able to modify the stream parameters ( Has this PR worked well for your? Were you able to test it IRL? And could you say more re: iOS implementation. How do you update the |
|
Hi @orkun1675, I'll respond to your questions below. Note that those answers are assuming you only have the "while using the app" location permission and not the "always" location permission.
I think it would be convenient if it worked, but from my understanding, this will not work unless the app is running in the foreground: the Android SDK APIs used to subscribe to location updates only allow you to cancel your subscription, not modify it - if you want to modify your subscription settings (e.g. intervalDuration), you need to cancel your subscription and re-subscribe with the new settings, and unless your app has the "always" location permission, the Android system will deny your request to create a new subscription from the background, even if your foreground service for another subscription is already running.
Yes, it works perfectly well for my use case, I use it for the tracks recording feature of my Map Marker app here (not open source):
The iOS implementation does not require those two position streams, because on iOS, it is technically possible to modify the existing subscription while the app is running in the background, or at least shortly after the app has entered background. |
|
Thanks a lot for the pointers @androidseb !
I don't think this is neccesary. Calling
Great point. Right now our app DestiWake requires the "Location Always" permission. I haven't tested this approach without that permission. We hope to remove "Location Always" in the future; I figure I'll have to revisit this then. |
Changes summary
These changes aim to make it possible to implement a GPS track recording app with optimal battery usage in the background while also having a highly responsive UI while the app is in the foreground, without having to declare the
ACCESS_BACKGROUND_LOCATIONpermission, essentially fulfilling the following requirements on Android:ACCESS_BACKGROUND_LOCATIONpermission at allWithout these changes, a limitation of this library on Android is that you can only have a single location stream active at a given time. And a new location stream cannot start when the app is in the background unless you have declared and granted access to the dangerous
ACCESS_BACKGROUND_LOCATIONpermission. This limitation does not affect iOS since you can still start a location stream for a little while after the app has been put in the background.If you want to change the location stream parameters (e.g.
distanceFilter,intervalDuration, etc.), you have to cancel your current stream subscription and start a new one. Ideally, an app recording location in the background sets parameters to reduce the location updates frequency to preserve battery, but keeps a high frequency update while the app is in the foreground.These changes allow to have two separate streams (started while the app is in the foreground) with different parameters, and cancel them individually:
foregroundNotificationConfigis null, then the regular stream is startedforegroundNotificationConfigis set, then the foreground service stream is startedWhen the app is put in the background, the high frequency UI stream can be canceled while the lower frequency foreground service stream remains active.
How to use this in your code today
If you want to test / use this code before it has been merged and published, you can use the following dependency overrides in your
pubspec.yaml.Draft disclaimer
This is currently a draft, and the pre-launch checklist below is not complete, because I'm not sure if this is a direction the maintainers of this library would want to take. If so, please let me know, I'll put in the work to complete the checklist and polish this PR.
Pre-launch Checklist
pubspec.yamlwith an appropriate new version according to the pub versioning philosophy, or this PR is does not need version changes.CHANGELOG.mdto add a description of the change.///).main.dart format .and committed any changes.flutter analyzeand fixed any errors.