react-native-background-geolocation icon indicating copy to clipboard operation
react-native-background-geolocation copied to clipboard

Can't continue tracking location when user choose 'WhenInUse' option

Open Anastasia-Les opened this issue 1 year ago • 3 comments

Your Environment

  • Plugin version: 4.12.1
  • Platform: Android
  • OS version: 33
  • React Native version (react-native -v): 0.71.6
  • Plugin config
export const useBgGeolocation = (eventCarId: string) => {
  const dispatch = useAppDispatch()

  useEffect(() => {
    const onLocation: Subscription = BackgroundGeolocation.onLocation(
      async (location) => {
        console.log('coordinates', location.coords)
      },
      (e) => console.log('Location error', e),
    )

    BackgroundGeolocation.ready({
      // Geolocation Config
      desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
      distanceFilter: 10,
      // Activity Recognition
      disableMotionActivityUpdates: true,
      stopTimeout: 5,

      backgroundPermissionRationale: {
        title: `${t('bgPermission.title')}`,
        message: `${t('bgPermission.message')}`,
        negativeAction: `${t('bgPermission.negativeAction')}`,
        positiveAction: `${t('bgPermission.positiveAction')}`,
      },
      locationAuthorizationAlert: {
        titleWhenNotEnabled: `${t('authPermission.title')}`,
        titleWhenOff: `${t('authPermission.title')}`,
        instructions: `${t('authPermission.instructions')}`,
        cancelButton: `${t('authPermission.cancel')}`,
        settingsButton: `${t('authPermission.settings')}`,
      },

      notification: {
        layout: 'notification_layout',
        title: `${t('androidServiceNotification.title')}`,
      },

      debug: true, // <-- enable this hear sounds for background-geolocation life-cycle.
      logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,

      stopOnTerminate: true, // <-- Allow the background-service to continue tracking when user closes the app.
      startOnBoot: true, // <-- Auto start tracking when device is powered-up.
      batchSync: false, // <-- [Default: false] Set true to sync locations to server in a single HTTP request.
      autoSync: true, // <-- [Default: true] Set true to sync each location to server as it arrives.
      locationAuthorizationRequest: 'WhenInUse',
    }).catch((e) => {
      console.log('e', e)
    })

    return () => {
      onLocation.remove()
    }
  }, [eventCarId, dispatch])

  useEffect(() => {
    if (!!eventCarId) {
      BackgroundGeolocation.getCurrentPosition({
        persist: true,
        samples: 1,
        extras: {
          getCurrentPosition: true,
        },
      })
        .then((location: Location) => {
          console.log('location', location)
        })
        .catch((error: LocationError) => {
          console.warn('[getCurrentPosition] error: ', error)
        })

      BackgroundGeolocation.start()
      console.log('START')
    } else {
      BackgroundGeolocation.stop()
      console.log('STOP')
    }
  }, [eventCarId, dispatch])
}

Expected Behavior

Continue tracking user's location when option "Allow only while using app" is chosen

Actual Behavior

Does not track user's location when user choose "Allow only while using app", I get only first coordinates

Steps to Reproduce

Context

Debug logs

Logs
PASTE_YOUR_LOGS_HERE

Anastasia-Les avatar Jul 18 '23 18:07 Anastasia-Les

Do you know what are the consequences of WhenInUse vs Always?

christocracy avatar Jul 18 '23 22:07 christocracy

I have just read lots of issues here, but still can't find out what are the implications of using Always vs WhenInUse.

Are there any docs specifying it in context of:

  1. Using start()
  2. Behaving when app is on the front vs. in the background (but not killer via task manager)

rtpm avatar Jan 15 '24 09:01 rtpm

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

github-actions[bot] avatar May 08 '24 01:05 github-actions[bot]

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

github-actions[bot] avatar May 22 '24 01:05 github-actions[bot]