Management of preventSuspend: true in iOS
Hello there, this is not a bug, just a question to know what is the best approach to a problem.
I'm making an app that needs to scan the position of workers, I need to geolocate them for the whole day (8 hours of work + 1 hour of lunch) every 3 minutes without run out of battery.
For iOS I need to set preventSuspend: true to receive heartbeats while the app is in background.
My question is: since from the docs it's recommended to not keep this value always true, what is a good strategy to change this value? I'd like to know when (and maybe an example of how) to do that.
Environment
- Plugin version: 2.8.3
- Platform: iOS
- React Native version (
react-native -v): 0.42.3
If you're concerned about battery-life, you shouldn't use preventSuspend.
You're still going to get iOS background-fetch events about every 15min. You can use that callback to execute a getCurrentPosition if you wish. This plugin is a dependency of background-geolocation and is already installed. You simply need to implement a listener as documented in its README there.
// Disable preventSuspend
BackgroundGeolocation.setConfig({
preventSuspend: false
});
// Enable preventSuspend
BackgroundGeolocation.setConfig({
preventSuspend: true
});
Ok, but if I' m not wrong preventSuspend is necessary to receive heartbeats when I'm in background in ios, right?
So, if I absolutely want to use preventSuspend: true the correct behaviour should be change it according to some strategy (for example stop it during the night). Can you suggest me a good strategy or two based on your experience?
I'm not thinking about my own application, it's about understanding how to use heartbeats in background without draining the battery more than necessary.
Implement a background-fetch listener for iOS. This plugin is already installed as a dependency of background-geolocation -- it's already available at window.BackgroundFetch. Simply set up a listener as documented on that repo.
In your fetch callback, get the current time and make a decision to disable or enable preventSuspend
Ok, so your suggestion is to check the date/time and based on that disable the preventSuspend when I don't need heartbeats. With preventSuspend: false I will not have heartbeats when the device is stationary, but I will still have the location events when it's moving.
To do this kind of check I will probably need something that run in background, for example the transistorsoft/react-native-background-fetch or ocetnik/react-native-background-timer.
Something like this:
function runPeriodicallyEvenInBackground() {
if( iHaveMyReasons() ){
// Disable preventSuspend
BackgroundGeolocation.setConfig({
preventSuspend: false
});
} else {
// Enable preventSuspend
BackgroundGeolocation.setConfig({
preventSuspend: true
});
}
}
Is everything correct? Disable based on time, no heartbeats, only location when moving?
Also, what kind of strategy I can use to enable/disable? For example I can think about: stop it at night and in the weekend.
Talking about time, I saw that schedule can do something similar, can we use it with preventSuspend?
Sorry, I sent you the wrong link to background-fetch (I sent you the Cordova one -- not the React Native version).
react-native-background-fetch (which is created by me), is what you need. It is already installed as a dependency of react-native-background-geolocation. You simply need to implement a listener as documented in its README.
You cannot use background-timers on iOS. Why not? Watch this
In that video above, also see the bit at the end
@christocracy is it accurate to say that if we do not enable preventSuspend, the server will still get location messages if the user is moving (i.e, moves past their geofence.)? Or is it once iOS has suspended an app, it can never transmit location data again?
We also have similar requirements, we want to track data across throughout the entire day. We don't necessarily need location data if the user isn't moving past their geofence tho.
Will iOS still re-launch the app with preventSuspend: false if the user moves? I guess what is the purpose of preventSuspend?
Will iOS still re-launch the app with preventSuspend: false if the user moves? I
Yes. preventSuspend is not required by the plugin core Philosophy of Operation. preventSuspend is a very specialized mode that should generally be avoided in 99% of use-cases.