transistorsoft / react-native-background-geolocation

Sophisticated, battery-conscious background-geolocation with motion-detection
http://shop.transistorsoft.com/pages/react-native-background-geolocation
MIT License
2.66k stars 426 forks source link

Enter/exit geofence is not detected with location permission "Only while app is in use". (Android issue) #2175

Closed jakub-oone closed 1 month ago

jakub-oone commented 1 month ago

Your Environment

Expected Behavior

Polygon/circle geofence entry/exit is detected and visible on the map in the BGGeolocation demo app. with location permission "Only when the app is in use

Actual Behavior

Polygon/circle geofence entry/exit is not detected and is not visible on the map in the BGGeolocation demo app. with location permission "Only when the app is in use

Steps to Reproduce

  1. run BGGeolocation app - latest build from Google Play
  2. Advanced App
  3. switch on demo app
  4. allow just location permission "Only when the app is in use"
  5. Long-press map to add polygone/circle geofences
  6. No geofence is visible on the map.
  7. Walk into geofence zone
  8. no geofence is triggered

Context

Debug logs

Logs ``` PASTE_YOUR_LOGS_HERE ```
christocracy commented 1 month ago

Enter/exit geofence is not detected with location permission "Only while app is in use"

That’s correct. Both the iOS and Android geofencing APIs require Always permission.

see Android Geofencing docs

ACCESS_BACKGROUND_LOCATION means Always authorization.

IMG_2330

christocracy commented 1 month ago

If you observe the plug-in logs, you’ll find a ⚠️ message after adding geofences with WhenInUse permission.

jakub-oone commented 1 month ago

OK, but I forgot to mention that we are using GEOFENCE + LOCATION mode in the foreground of the app. iOS works (triggers all geofences) OK with "WhenInUse" permission, so why not Android? Thx.

christocracy commented 1 month ago

Consult the Android Geofencing api docs I linked above. The key word is “must”:

IMG_2331

Buthrakaur commented 1 month ago

Hello @christocracy , you're right that the Geofencing API on Android can't be used without always location permission but the library still has an access to location services (if while in use permission is granted of course) which were at least previously used to trigger the onGeofence event on both platforms. This seems to stop working in recent library versions though. We successfully used this behavior in the previous library versions e.g. "react-native-background-geolocation": "4.13.3" to make geofence-like functionality work on foreground with while in use location permission on both platforms. After we upgraded to newer version with polygon geofence support (the paid addon) this functionality stopped working on Android (on iOS not).

On the other hand the foreground geofence-like functionality with while in use location permission works just ok on iOS (even in the demo app) so this looks like a bug on Android which was introduced in some recent library version. Could you please look into this once again?

christocracy commented 1 month ago

This seems to stop working in recent library versions though

This is false. You were likely using an older targetSdkVersion. Now that targetSdkVersion >= 33 is required, your app must adhere to these restrictions.

while in use location permission works just ok on iOS

There is no correlation of behaviour between operating systems. They are completely different, created by different companies and implemented with completely different languages.

christocracy commented 1 month ago

The Android api docs are clear “To use geofencing, your app must request the following

Buthrakaur commented 4 weeks ago

Hello @christocracy , thanks for explanation. You're right - the geofencing didn't work even with the previous library version on foreground with only "while in use" permission on Android. We were confused because of specific behavior of the operating system when user grants "always" permission for a while but revokes the permission to "while in use" later. It looks like the Android Geofencing API keeps working on foreground with "while in use" permission if the geofences were added while the app had "always" permission. That's really strange and I can't also understand this Android limitation (geofencing not using on foreground with "while in use" permission) compared to iOS which supports this but that's "as designed" by Google/Android.

We would be super-happy if you would align this behavior on iOS/Android and "simulate" the geofencing on Android on foreground with "while in use" permission using pure location services (we actually thought the library is doing this) but understand you many not be willing to do so. We will need to "simulate" this on our side for now as our app use-cases just requires geo-triggering work on foreground with "while in use" permission only.

christocracy commented 4 weeks ago

If geofencing is crucial to your app, why don’t you instruct your users why it’s important to grant “Always” permission, explaining to them that a certain feature will no longer work if they don’t? If the user insists on granting “WhenInUse”, then perhaps they’re not interested in that feature.

Buthrakaur commented 4 weeks ago

It's quite hard to get "always" permission from normal users because of privacy - we're building SmartGuide travel guide app so that's not a B2B kind of app with limited user base where explaining this would be much easier. It's also technically feasible to perform location-based triggering on foreground with "while in use" permission even though it's not efficient compared to geofencing - general public doesn't even know there's some geofencing API and they just think about GPS.. So we try to explain and get "always" permission but still need to make our core functionality available with "while in use". We already received some 1* reviews because we even try to get the "always" location so we can't push more.. mobile app developer life is not easy :)

christocracy commented 4 weeks ago

Map components often have geometry methods for determining if a location exists within a circle.