flutterlocation icon indicating copy to clipboard operation
flutterlocation copied to clipboard

Android background location should work with WhileInUse permissions

Open Wackymax opened this issue 3 years ago • 10 comments

Describe the bug Currently the library forces the users to enable "always" location permissions in order to enable the background location mode.

Expected behavior Android does allow an application that started requesting location updates in the foreground, or though other exceptions listed here, to access the location with only the WhileInUse permissions. Can we add a setting to the enableBackgroundMode method that can bypass the permission check and just goes ahead creating the foreground service without forcing the user to enable "always" permissions?

From the Android documentation located here

The system considers your app to be using foreground location if a feature of your app accesses the device's current location in one of the following situations:

  • Your app is running a foreground service. When a foreground service is running, the system raises user awareness by showing a persistent notification. Your app retains access when it's placed in the background, such as when the user presses the Home button on their device or turns their device's display off.

Wackymax avatar Jul 01 '21 19:07 Wackymax

We have this need too. The good news is that this doesn't need huge changes. In fact, I am currently using this hack: https://github.com/willbryant/flutterlocation/commit/3851e050797b385d16bffedfce7c6cb43abd6064

As you can see all it does is replace the call to locationService.requestBackgroundPermissions with a call to locationService.enableBackgroundMode. This works, and we're using it in production.

Happy to help get this into a better shape so it can be upstreamed. Clearly we need to add an extra option to the API. "background mode" in the plugin currently means "at all times", and you and I want it to mean just "while in use" mode. (Note that iOS and Android use the terms differently as well.) We can either add a parameter to enableBackgroundMode, or we can add another method.

On a related note, our app repeatedly failed Apple's App Store review because we did not request the NSLocationAlwaysUsageDescription. This is ironic because we don't need this permission, since we don't want to run in "always" mode. But because the library references the APIs to support "always" mode, Apple insisted that we include NSLocationAlwaysUsageDescription. They accepted our app once we added that, even though it never gets used.

So actually, my absolute ideal would be to split off the "always" mode support into another plugin (which this plugin could provide a hook for). Then we could actually just leave the base API as it is now and change its behavior to be just "while in use" mode.

This is fairly invasive so not sure that the maintainer would be keen. @Lyokone any thoughts?

willbryant avatar Jul 06 '21 01:07 willbryant

(Edited)

Hi Will,

Thanks for that! Will see if I can use that commit for the time being while we wait for a more permanent fix.

On Tue, 06 Jul 2021, 03:51 Will Bryant, @.***> wrote:

We have this need too. The good news is that this doesn't need huge changes. In fact, I am currently using this hack: 3851e05 https://github.com/Lyokone/flutterlocation/commit/3851e050797b385d16bffedfce7c6cb43abd6064

As you can see all it does is replace the call to locationService.requestBackgroundPermissions with a call to locationService.enableBackgroundMode. This works, and we're using it in production.

Happy to help get this into a better shape so it can be upstreamed. Clearly we need to add an extra option to the API. "background mode" in the plugin currently means "at all times", and you and I want it to mean just "while in use" mode. (Note that iOS and Android use the terms differently as well.) We can either add a parameter to enableBackgroundMode, or we can add another method.

On a related note, our app repeatedly failed Apple's App Store review because we did not request the NSLocationAlwaysUsageDescription. This is ironic because we don't need this permission, since we don't want to run in "always" mode. But because the library references the APIs to support "always" mode, Apple insisted that we include NSLocationAlwaysUsageDescription. They accepted our app once we added that, even though it never gets used.

So actually, my absolute ideal would be to split off the "always" mode support into another plugin (which this plugin could provide a hook for). Then we could actually just leave the base API as it is now and change its behavior to be just "while in use" mode.

This is fairly invasive so not sure that the maintainer would be keen. @Lyokone https://github.com/Lyokone any thoughts?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Lyokone/flutterlocation/issues/600#issuecomment-874403228, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACYF27MJWKMTFLETSU7RLXLTWJOTFANCNFSM47VLADSQ .

Wackymax avatar Jul 06 '21 04:07 Wackymax

+1 for this, I need the "while in use" for Android as well :)

colbygarland avatar Aug 20 '21 21:08 colbygarland

This is nice feature to have. Can I help anyhow? Reviewing or so. To make the merge happen. p.s. @Lyokone , great job on the library itself!

AlexBacich avatar Sep 02 '21 15:09 AlexBacich

This would be great to fix. We're now using a fork of this repo with the hack of @willbryant in it but would rather not.

TheoLassonder avatar Oct 02 '21 04:10 TheoLassonder

https://github.com/Lyokone/flutterlocation/pull/692

If this PR becomes Merge, i will add the isForeground option in enableBackgroundMode in the location package and change Background permission in Android to be selectively acceptable according to that option.

note11g avatar Feb 11 '22 04:02 note11g

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Apr 16 '22 17:04 stale[bot]

We're still all keen, just waiting for feedback from @Lyokone on which option is acceptable.

willbryant avatar Apr 16 '22 23:04 willbryant

any progress?

choim4389 avatar Oct 04 '22 03:10 choim4389

Still interested in this...

DenisSvinkovskij avatar Apr 16 '24 06:04 DenisSvinkovskij