I have run into an issue here. I need to update the location of the device to my firebase database every 5 seconds untill the user closes the app.
I know it will eat up the battery, it is a necessary feature for the app.
The setInterval function is not working inside the BackgroundMode enable function. When it is put outside, it works normally(When the app is running) when when the app is not on screen(hidden), the update function stops.
Here is what i have tried:
if (this.platform.is('android') || this.platform.is("ios")) {
const options = { title: 'GEOLOCATION', text: 'Background geolocation update', hidden: false, silent: true };
this.backgroundMode.setDefaults(options);``
this.backgroundMode.enable();
this.backgroundMode.on('enable').subscribe(() => {
console.log('background mode activated !!!');
// start tracking
// Periodic update after particular time intrvel
this.positionTracking = setInterval(() => {
this.geolocation.getCurrentPosition().then((resp) => {
console.log(resp);
this.driverService.updatePosition(uid,resp.coords.latitude, resp.coords.longitude);
}, err => {
console.log(err);
});
}, 5000);
});
}
Thank you in advance for the support.