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.

Use this plugin and solved all problem =>

https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background/src/master/

OK, I haven’t tilted at this particular windmill in a while, so I’ll give it another go.

Speaking as a user, I want to be in total, complete, 100% control of my device’s battery life. The amount of control that you, app writer, get, is therefore zero.

Fortunately, manufacturers of mobile device operating systems agree with me, and have therefore implemented rather byzantine mechanisms that effectively treat your apps like pests to be caged and defanged, because so many apps misappropriate resources that don’t belong to them. As a result, you are never going to be able to guarantee that your app will do the periodic phoning home that you so desperately desire. The best you can do is to get involved in the wrong side of an arms race, probably by paying to get specialized plugins like the one @EinfachHans has suggested in this thread.

So let’s talk about marketing and presentation. I am given two apps. App A tells me that it cannot function properly unless it eats up my battery life. App B does everything possible to play nicely with others, but in order to provide (more timely notifications | whatever), it can optionally be configured to do whatever it wants to do in the background, and provides assistance on how I can set up my OS in order to enable that if I wish. However, the core functionality of the app is totally unaffected even if I decide I don’t wish to enable the background functionality.

I’m choosing App B every time.

I get that there are some unusual situations like @danieldugger’s ankle monitor app. These highly-specific situations are also generally more conducive to special-purpose strategies that go beyond the generic IntentService strategy deployed by the plugin @Avirup1993 is recommending.

In the end, though, as app developers, it is imperative that we respect our users. We are guests in their homes, and it reflects badly on the entire development community when we rudely put our dirty feet on the furniture. Don’t treat users’ battery life as an endless free resource to be exploited however you wish.