BackgroundMode not working as expected

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.

Use this Plugin: https://github.com/transistorsoft/cordova-background-geolocation-lt

It costs, but works like it should!

2 Likes

So, BackgroundMode plugin doesn’t work in this case?

I use the plugin written by Chris for an alternative to the traditional ankle monitor for the criminally accused. It is awesome and worth every penny you pay.