react-native-background-geolocation
react-native-background-geolocation copied to clipboard
Can't continue tracking location when user choose 'WhenInUse' option
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
Do you know what are the consequences of WhenInUse vs Always?
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:
- Using start()
- Behaving when app is on the front vs. in the background (but not killer via task manager)
This issue is stale because it has been open for 30 days with no activity.
This issue was closed because it has been inactive for 14 days since being marked as stale.