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

Location tracking stops after a few minutes when the app is in background

Open kush-mish opened this issue 1 year ago • 9 comments

Your Environment

  • Plugin version: ^18.1.0
  • Platform: Android
  • OS version: Android 10 and 13
  • Device manufacturer / model: Xiaomi Redmi Note 7 pro and Samsung A22
  • React Native version: 11.2.0
  • Plugin config
desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
distanceFilter: 10,
heartbeatInterval: -1,
notification: {
  title: "ABC",
  text: "xyz"
},
enableHeadless: true,
stopOnTerminate: false,
startOnBoot: true,
stopTimeout: 1,
isMoving: true,

Expected Behavior

The foreground process should not end automatically as it is enabled by default and I'm defining the notification in the config.

Actual Behavior

It tracks and updates the location fine for 10-15 minutes and then stops. The notification for the foreground service also disappears, until the app is reopened and the components are loaded. And this happens when the app is not terminated.

Context

I'm building an app that continuously tracks the device's location for up to 18 hours. The way I’m implementing it is, after calling ready() and start() I set up the onLocation listener, where I’m saving the location manually to my database. I have also defined a headless task in the index.js file that saves the location to the database on ‘location’ event. I detach the onLocation listener on the ’terminate’ event.

kush-mish avatar Jul 20 '23 12:07 kush-mish

should not end automatically

Yes, it should. Please read the wiki here “Philosophy of Operation”. This plug-in is designed to track location only when the device is detected to be moving. When stationary, the plug-in turns off location-services to conserve energy.

christocracy avatar Jul 20 '23 13:07 christocracy

How do I detect that the location tracking has been stopped? I defined an onEnabledChange listener, but it doesn't get called when the notification disappears. And do I have to call start() in the onMotionChange listener callback?

kush-mish avatar Jul 20 '23 14:07 kush-mish

See api docs onMotionChange, explained in the Philosophy of Operation.

onEnabledChange is fired ONLY when YOU call .stop() or .start().

christocracy avatar Jul 20 '23 14:07 christocracy

Also explained in the “Philosophy of Operation” is the idea of manually calling .changePace to toggle the plug-in’s tracking state.

also explained there is the Config.stopTimeout, which you’re free to adjust higher as desired.

the plug-in is not .stop()-ing, it’s changing state between stationary/moving.

once you call .start(), the plug-in is enabled forever and will track the device everywhere it moves until you call .stop().

christocracy avatar Jul 20 '23 14:07 christocracy

The plugin changing to the stationary state is fine when the device is still. But there have been cases when it doesn't start tracking even after the device exits this 200-meter geofence. You can see the straight lines in the attached image: image

kush-mish avatar Jul 20 '23 14:07 kush-mish

For device-specific issues, see https://dontkillmyapp.com

christocracy avatar Jul 20 '23 14:07 christocracy

When I increased the value of stopTimeout,, it keeps running(it is running and it has been running longer than before). So I don't think it is the OS that is killing the task? Is there a way, where I can keep invoking the listener occasionally when the device is stationary to keep the whole process alive(though I'm not sure that the foreground task is directly linked with it)?

kush-mish avatar Jul 20 '23 15:07 kush-mish

Are you observing the plug-in logs? See wiki “Debugging” to learn how to observe the logs.

Also see Config.debug to learn how to enable the plug-in debug sound FX.

christocracy avatar Jul 20 '23 15:07 christocracy

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

github-actions[bot] avatar May 07 '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]