Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"WhenInUse" config option being ignored on android #1762

Closed
deanhet opened this issue Jul 26, 2023 · 5 comments
Closed

"WhenInUse" config option being ignored on android #1762

deanhet opened this issue Jul 26, 2023 · 5 comments
Labels

Comments

@deanhet
Copy link

deanhet commented Jul 26, 2023

I'm hoping this amounts to a simply missing something on our side. We recently changed our app permissions from always on in the background, to only while in use. However, when we pass locationAuthorizationRequest: 'WhenInUse' in the config, the app will always open an OS permission prompt to change the permissions to "Allow all the time".

I have read the docs and every related issue I think might be causing issue. I've reset our config just in case a broken one was persisting and we've checked we're not requesting anything before .ready.

Your Environment

  • Plugin version: 4.12.1
  • Platform: Android
  • OS version: API 33
  • Device manufacturer / model: Pixel 5
  • React Native version (react-native -v): 0.72.3
  • Plugin config
{
        backgroundPermissionRationale: {
        //   message: 'Example message.',
          title:   'Example title'
        },
        // Application config
        // debug:             __DEV__, // <-- enable this hear sounds for background-geolocation life-cycle. On iOS you will need to manually enable this permission in XCode.
        // Geolocation Config,
        desiredAccuracy:              BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
        distanceFilter:               10,
        heartbeatInterval:            900,
        locationAuthorizationRequest: 'WhenInUse',
        logLevel:                     BackgroundGeolocation.LOG_LEVEL_VERBOSE,
        preventSuspend:               true, // <-- Prevent the OS from suspending the app, could kill battery. Keep an eye on it.
        startOnBoot:                  false, // <-- Auto start tracking when device is powered-up.
        stopOnTerminate:              true, // <-- Should the background-service continue tracking when user closes the app.
        // Activity Recognition
        stopTimeout:                  5
      }

Expected Behavior

When running BackgroundGeolocation.requestPermission() and setting the app location permissions to while in app, when ready is called then there should be no further prompt if locationAuthorizationRequest: 'WhenInUse' is set. Alternatively if it needs to prompt again, it will only ask for in app usage.

Actual Behavior

Screenshot 2023-07-26 at 2 44 29 pm

This prompt will show up every time until the user allows.

Steps to Reproduce

  1. We have a location client class.
  async initialize() {
    // Subscribe to events.
    const onLocation = BackgroundGeolocation.onLocation((location) => this.onLocation(location))
    const onHeartbeat = BackgroundGeolocation.onHeartbeat((event) => this.onHeartbeat(event))
    const onMotionChange = BackgroundGeolocation.onMotionChange(this.onMotionChange)
    const onActivityChange = BackgroundGeolocation.onActivityChange(this.onActivityChange)
    const onProviderChange = BackgroundGeolocation.onProviderChange(this.onProviderChange)

    this.subscriptions = [
      ...this.subscriptions,
      onHeartbeat,
      onLocation,
      onMotionChange,
      onActivityChange,
      onProviderChange
    ]

    // Ready the plugin.
    try {
      const state = await BackgroundGeolocation.ready({
        backgroundPermissionRationale: {
        //   message: 'Example message.',
          title:   'Example title'
        },
        // Application config
        // debug:             __DEV__, // <-- enable this hear sounds for background-geolocation life-cycle. On iOS you will need to manually enable this permission in XCode.
        // Geolocation Config,
        desiredAccuracy:              BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
        distanceFilter:               10,
        heartbeatInterval:            900,
        locationAuthorizationRequest: 'WhenInUse',
        logLevel:                     BackgroundGeolocation.LOG_LEVEL_VERBOSE,
        preventSuspend:               true, // <-- Prevent the OS from suspending the app, could kill battery. Keep an eye on it.
        startOnBoot:                  false, // <-- Auto start tracking when device is powered-up.
        stopOnTerminate:              true, // <-- Should the background-service continue tracking when user closes the app.
        // Activity Recognition
        stopTimeout:                  5
      })

      BackgroundGeolocation.getCurrentPosition(this.currentPositionRequest)

      await BackgroundGeolocation.start(undefined, (error) => {
        console.log('[BackgroundGeolocation] Failed to start service.', error)
      })

      if (!state?.enabled) {
        console.log('[BackgroundGeolocation] Failed to start service.')
      }
    } catch (error) {
      console.log('[BackgroundGeolocation] Failed to start service.', error)
    }
  }

  reset() {
    this.subscriptions.forEach((subscription) => subscription.remove())
  }
}
  1. We call locationClient.initialize() when the app mounts. The prompt shows every time until the user allows all the time.

Context

Ideally the user shouldn't ever need to allow permissions for all the time.

Debug logs

Logs are from app open until the permission prompt comes up

Logs ```

07-26 14:56:04.762 11126 11126 I TSLocationManager: [c.t.l.s.TSScheduleManager oneShot]
07-26 14:56:04.762 11126 11126 I TSLocationManager: ⏰ Scheduled OneShot: TERMINATE_EVENT in 10000ms (jobID: -1708771588)
07-26 14:56:04.789 11126 15193 D TSLocationManager: [c.t.l.a.BackgroundGeolocation b]
07-26 14:56:04.789 11126 15193 D TSLocationManager: 🔴 Cleared callbacks
07-26 14:56:09.679 11126 15312 D TSLocationManager: [c.t.l.g.TSGeofenceManager$f run]
07-26 14:56:09.679 11126 15312 D TSLocationManager: ╔═════════════════════════════════════════════
07-26 14:56:09.679 11126 15312 D TSLocationManager: ║ TSGeofenceManager monitoring 0/0
07-26 14:56:09.679 11126 15312 D TSLocationManager: ╠═════════════════════════════════════════════
07-26 14:56:09.679 11126 15312 D TSLocationManager: ╚═════════════════════════════════════════════
07-26 14:56:09.698 11126 15394 I TSLocationManager: [c.t.l.service.HeartbeatService start]
07-26 14:56:09.698 11126 15394 I TSLocationManager: 🎾 Start heartbeat (900s)
07-26 14:56:09.702 11126 15394 D TSLocationManager: [c.t.locationmanager.util.c g]
07-26 14:56:09.702 11126 15394 D TSLocationManager: ℹ️ LocationAuthorization: Permission granted
07-26 14:56:09.703 11126 15312 I TSLocationManager: [c.t.l.l.TSLocationManager onSingleLocationResult]
07-26 14:56:09.703 11126 15312 I TSLocationManager: 🔵 Acquired current position
07-26 14:56:09.704 11126 15312 D TSLocationManager: [c.t.l.l.TSLocationManager calculateMedianAccuracy] Median accuracy: 5.0
07-26 14:56:09.874 11126 15394 D TSLocationManager: [c.t.locationmanager.util.c g]
07-26 14:56:09.874 11126 15394 D TSLocationManager: ℹ️ LocationAuthorization: Permission granted
07-26 14:56:09.874 11126 15312 I TSLocationManager: [c.t.locationmanager.util.c h]
07-26 14:56:09.874 11126 15312 I TSLocationManager: 🔵 LocationAuthorization: Requesting permission
07-26 14:56:09.874 11126 15213 I TSLocationManager: - Enable: true → true, trackingMode: 1
07-26 14:56:09.874 11126 11126 I TSLocationManager: [c.t.locationmanager.util.c$j onPermissionGranted]
07-26 14:56:09.874 11126 11126 I TSLocationManager: ✅ LocationAuthorization: Permission granted
07-26 14:56:09.875 11126 15213 D TSLocationManager: [c.t.l.http.HttpService startMonitoringConnectivityChanges]
07-26 14:56:09.875 11126 15213 D TSLocationManager: 🎾 Start monitoring connectivity changes
07-26 14:56:09.875 11126 15213 D TSLocationManager: [c.t.l.device.DeviceSettings startMonitoringPowerSaveChanges]
07-26 14:56:09.875 11126 15213 D TSLocationManager: 🎾 Start monitoring powersave changes
07-26 14:56:09.875 11126 15213 I TSLocationManager: [c.t.locationmanager.util.c h]
07-26 14:56:09.875 11126 15213 I TSLocationManager: 🔵 LocationAuthorization: Requesting permission
07-26 14:56:09.878 11126 15213 I TSLocationManager: [c.t.l.service.HeartbeatService start]
07-26 14:56:09.878 11126 15213 I TSLocationManager: 🎾 Start heartbeat (900s)
07-26 14:56:09.907 11126 11126 D TSLocationManager: [c.t.l.service.AbstractService start]
07-26 14:56:09.907 11126 11126 D TSLocationManager: 🎾 LocationRequestService [eventCount: 1]
07-26 14:56:09.908 11126 15213 I TSLocationManager: [c.t.l.l.SingleLocationRequest startUpdatingLocation]
07-26 14:56:09.908 11126 15213 I TSLocationManager: 🔵 [SingleLocationRequest start, action: 2, requestId: 34]
07-26 14:56:09.909 11126 11126 I TSLocationManager: [c.t.locationmanager.util.c$j onPermissionGranted]
07-26 14:56:09.909 11126 11126 I TSLocationManager: ✅ LocationAuthorization: Permission granted
07-26 14:56:09.912 11126 15213 D TSLocationManager: [c.t.l.service.AbstractService finish] ⚙️︎ finish LocationRequestService [eventCount: 0, sticky: true]
07-26 14:56:09.976 11126 11126 D TSLocationManager: [c.t.l.service.AbstractService start]
07-26 14:56:09.976 11126 11126 D TSLocationManager: 🎾 LocationRequestService [eventCount: 1]
07-26 14:56:09.976 11126 15213 I TSLocationManager: [c.t.l.l.SingleLocationRequest startUpdatingLocation]
07-26 14:56:09.976 11126 15213 I TSLocationManager: 🔵 [SingleLocationRequest start, action: 1, requestId: 35]
07-26 14:56:09.977 11126 15213 I TSLocationManager: [c.t.l.s.ActivityRecognitionService start]
07-26 14:56:09.977 11126 15213 I TSLocationManager: 🎾 Start motion-activity updates
07-26 14:56:09.978 11126 15213 D TSLocationManager: [c.t.l.service.AbstractService finish] ⚙️︎ finish LocationRequestService [eventCount: 0, sticky: true]
07-26 14:56:10.007 11126 11126 I TSLocationManager: [c.t.l.s.TSScheduleManager oneShot]
07-26 14:56:10.007 11126 11126 I TSLocationManager: ⏰ Oneshot TERMINATE_EVENT is already pending
07-26 14:56:10.036 11126 11126 D TSLocationManager: [c.t.l.a.TSLocationManagerActivity execute] android.permission.ACCESS_BACKGROUND_LOCATION
07-26 14:56:10.133 11126 11126 D TSLocationManager: [c.t.l.a.TSLocationManagerActivity execute] locationsettings
07-26 14:56:10.134 11126 11126 D TSLocationManager: [c.t.l.adapter.TSConfig translateDesiredAccuracy] translateDesiredAccuracy (true): -1
07-26 14:56:10.155 11126 11126 D TSLocationManager: [c.t.l.a.TSLocationManagerActivity stop] eventCount: 1
07-26 14:56:10.858 11126 11126 D TSLocationManager: [c.t.l.service.AbstractService start]
07-26 14:56:10.858 11126 11126 D TSLocationManager: 🎾 LocationRequestService [eventCount: 1]
07-26 14:56:10.860 11126 11126 D TSLocationManager: [c.t.l.service.AbstractService start]
07-26 14:56:10.860 11126 11126 D TSLocationManager: 🎾 LocationRequestService [eventCount: 2]
07-26 14:56:10.860 11126 15213 I TSLocationManager: [c.t.l.s.LocationRequestService handleLocationResult]
07-26 14:56:10.860 11126 15213 I TSLocationManager: ╔═════════════════════════════════════════════
07-26 14:56:10.860 11126 15213 I TSLocationManager: ║ getCurrentPosition LocationResult: 34
07-26 14:56:10.860 11126 15213 I TSLocationManager: ╠═════════════════════════════════════════════
07-26 14:56:10.860 11126 15213 I TSLocationManager: ╟─ 📍 Location[fused 37.421998,-122.084000 hAcc=45.103 et=+7h17m48s80ms alt=5.0 vAcc=0.76070404], age: 858ms, time: 1690379770000
07-26 14:56:10.860 11126 15312 I TSLocationManager: [c.t.l.s.LocationRequestService handleLocationResult]
07-26 14:56:10.860 11126 15312 I TSLocationManager: ╔═════════════════════════════════════════════
07-26 14:56:10.860 11126 15312 I TSLocationManager: ║ motionchange LocationResult: 35
07-26 14:56:10.860 11126 15312 I TSLocationManager: ╠═════════════════════════════════════════════
07-26 14:56:10.860 11126 15312 I TSLocationManager: ╟─ 📍 Location[fused 37.421998,-122.084000 hAcc=45.103 et=+7h17m48s80ms alt=5.0 vAcc=0.76070404], age: 860ms, time: 1690379770000
07-26 14:56:10.861 11126 15213 I TSLocationManager: [c.t.l.l.TSLocationManager onSingleLocationResult]
07-26 14:56:10.861 11126 15213 I TSLocationManager: 🔵 Acquired current position
07-26 14:56:10.861 11126 15213 D TSLocationManager: [c.t.l.l.TSLocationManager calculateMedianAccuracy] Median accuracy: 5.0
07-26 14:56:10.868 11126 11126 D TSLocationManager: [c.t.l.service.AbstractService finish] ⚙️︎ finish LocationRequestService [eventCount: 0, sticky: false]
07-26 14:56:10.875 11126 15312 D TSLocationManager: [c.t.l.l.TSLocationManager calculateMedianAccuracy] Median accuracy: 5.0
07-26 14:56:10.875 11126 15213 D TSLocationManager: [c.t.l.s.LocationRequestService handleLocationResult] SingleLocationRequest 34 isFinished? true
07-26 14:56:10.876 11126 15213 D TSLocationManager: [c.t.l.service.AbstractService finish] ⚙️︎ finish LocationRequestService [eventCount: 0, sticky: false]
07-26 14:56:10.876 11126 15312 D TSLocationManager: [c.t.l.s.LocationRequestService handleLocationResult] SingleLocationRequest 35 isFinished? false
07-26 14:56:10.876 11126 15312 D TSLocationManager: [c.t.l.service.AbstractService finish] ⚙️︎ finish LocationRequestService [eventCount: 0, sticky: false]
07-26 14:56:10.877 11126 15311 I TSLocationManager: [c.t.l.data.sqlite.b persist]
07-26 14:56:10.877 11126 15311 I TSLocationManager: ✅ INSERT: b521fcfe-c77e-4f54-9bd9-b48b0c1b4240
07-26 14:56:10.882 11126 11126 D TSLocationManager: [c.t.l.service.AbstractService onDestroy]
07-26 14:56:10.882 11126 11126 D TSLocationManager: 🔴 LocationRequestService stopped


</details>

Many thanks!
@christocracy
Copy link
Member

You are either:

  1. Configuring the plugin with reset: false
  2. Calling a method which requests location access BEFORE calling .ready(config) (eg: .getCurrentPosition())

@christocracy
Copy link
Member

  1. Calling .requestPermission() BEFORE calling .ready(config).

The default value of loctionAuthorizationRequest is Always.

// NO
BackgroundGeolocation.requestPermission()
.
.
.
await BackgroundGeolocation.ready(config) // <-- Your Config is NOT applied until this completes!!!

// ONLY HERE has your Config been applied.
BackgroundGeolocation.requestPermission()

Calling .ready(config) DOES NOT automatically request permissions. ONLY calling .start() causes the plugin to request permissions.

@deanhet
Copy link
Author

deanhet commented Jul 26, 2023

Thank for the speedy response and clarifying!

  1. My full config is posted as above, we don't include a reset.
  2. I thought that might be the case, so (for debugging) the only other function we call that includes getCurrentPosition, I have returned early on. We're still seeing it.
  3. I have now removed the early requestPermission and we see intended behaviour!
Screenshot 2023-07-26 at 3 21 52 pm

However, when I reload the bundle from the terminal (or just a live reload is triggered). The initial prompt asking "allow all the time" comes up again. Manually force closing the app and opening it again doesn't seem to trigger any prompt though.

This may be what was causing our confusion with it always showing.

Copy link

github-actions bot commented May 7, 2024

This issue is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale label May 7, 2024
Copy link

This issue was closed because it has been inactive for 14 days since being marked as stale.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants