Why cordova background geolocation doesn't work as my expection?

I use cordova background geolocaiton plugin in my ionic 3 application.

    ionic cordova plugin add cordova-plugin-mauron85-background-geolocation@alpha
    npm install --save @ionic-native/background-geolocation@4

I expect the plugin work in background, once the device is moved over 40 meters, the plugin will trigger this.backgroundGeolocation.on(BackgroundGeolocationEvents.location) once, My code is like:

startBackgroundGeolocation() {
    const config: BackgroundGeolocationConfig = {
      desiredAccuracy: 10,
      stationaryRadius: 20,
      distanceFilter: 40,
      debug: true, //  enable this hear sounds for background-geolocation life-cycle.
      stopOnTerminate: false // enable this to clear background location settings when the app terminates
    };

    this.backgroundGeolocation.configure(config).then(() => {
      this.backgroundGeolocation
        .on(BackgroundGeolocationEvents.location)
        .subscribe((location: BackgroundGeolocationResponse) => {
          console.log("!!!!!!!!!!!!!!!!!!!!!!!!!! background geolocation !!!!!!!!!!!!!!!!!!!");
          console.log(location);
        });
    });
    this.backgroundGeolocation.start()
  }

But after backgroundGeolocation start, I find the behavior is not like my expectation.

  1. this.backgroundGeolocation.on(BackgroundGeolocationEvents.location) is triggered about ever 30 seconds without any device movement
  2. this.backgroundGeolocation.on(BackgroundGeolocationEvents.location) has been triggered many times, and the number of times increases one every ~30 seconds

for example, when it is first triggered, the log shows:

!!!!!!!!!!!!!!!!!!!!!!!!!! background geolocation !!!!!!!!!!!!!!!!!!!

in about 30 seconds, it is second triggered, log shows:

!!!!!!!!!!!!!!!!!!!!!!!!!! background geolocation !!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!! background geolocation !!!!!!!!!!!!!!!!!!!

in next about 30 seconds, it is third triggered, log shows:

!!!!!!!!!!!!!!!!!!!!!!!!!! background geolocation !!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!! background geolocation !!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!! background geolocation !!!!!!!!!!!!!!!!!!!

and so on.

I want .on(position) only be triggered when movement over 40 meters, and when it is triggered, just triggered once.

!!!!!!!!!!!!!!!!!!!!!!!!!! background geolocation !!!!!!!!!!!!!!!!!!!

I think there must be something wrong with my configuration setting, but I could not find the problem in the setting.

Could you please help if you familiar with cordova-background-geolocation plugin?

Thanks

The first thing I would do is put a console.log right at the point when you make the subscription, because the symptoms you describe sound consistent with what would happen if that subscription was being made more than once.

@rapropos Thank you very much! Yes. 2nd issue is deed caused by subscription was being made more than once.

I have modified the code as below, add setBackgroundGeolocationConfig() in constructor, and only call function startBackgroundGeolocation() when I need to use background geolocation, then second issue is fixed.

setBackgroundGeolocationConfig(){
    const config: BackgroundGeolocationConfig = {
      desiredAccuracy: 10,
      stationaryRadius: 20,
      distanceFilter: 40,
      debug: false, //  enable this hear sounds for background-geolocation life-cycle.
      stopOnTerminate: false // enable this to clear background location settings when the app terminates
    };

    this.backgroundGeolocation.configure(config).then(() => {
      this.backgroundGeolocation
        .on(BackgroundGeolocationEvents.location)
        .subscribe((location: BackgroundGeolocationResponse) => {
          console.log("!!!!!!!!!!!!!!!!!!!!!!!!!! background geolocation !!!!!!!!!!!!!!!!!!!");
          console.log(location);
        });
    });
  }

  startBackgroundGeolocation() {
    // start recording location
    this.backgroundGeolocation.start()
  }

1st issue still exists, I want .on(position) only be triggered when movement over 40 meters, but it is triggered about every 30 seconds. What is wrong in configuration parameters?

Hi,
I am trying exactly this but it does not work in the background at all. Using Huawei P-smart. I have no battery saver set. What could I be doing wrong?