POST location updates even when screen is off / other application is in the foreground? (ionic angular)

The project I’m working on is supposed to POST location updates every given amount of time, let’s say per minute.

The problem is the application should send the location updates even when it’s in the background, phone is sleeping, other app is in the foreground…

Based on some other, older questions here I came to the conclusion that these are the plugins I should use in combination:

If I understood, the background-geolocation doesn’t provide event-listeners for the background location events (only for the foreground ones) because it’s supposed to only collect locations while the phone is sleeping and POST-ing them the next time the phone wakes up and the application is focused?
I see in Android Studio that the location updates are logged even when I turn / lock the screen of my phone, yet the PostTask is not running.
I tried to find an event listener for those location updates but I think there isn’t any.

I tried to use the experimental headless task function from the background-location plugin but it’s not working… Tried to turn background mode on too, it didn’t work eather.

I tried to use powermanagement. The simple aquire is working so at least I can stop the phone from turning the screen off when the application is focused yet the “dim” function which should let the phone darken the screen just crashes the application…but even if it would work if I would lock the screen the POSTing would stop I think.

I tried to apply a 15 sec timer inside the background.configure method. Getting and posting locations every 15 sec is working…until the screen is locked. Same goes for the built in post task.

It works for some secs afterwards but eventually the OS just puts the application in the background and only the “silent” location cacheing continues without POSTing.

The ionic project uses capacitor by default so I have to install the cordova plugins using the
“ionic-native form” when installing.
Eg.

npm install @mauron85/cordova-plugin-background-geolocation
npm install @ionic-native/background-geolocation
ionic cap sync

I don’t know if this matters or not.

The phone I test the app is a Xiaomi with Android 10.


initializeApp() {
    this.platform.ready().then(() => {
      this.statusBar.styleDefault();
      this.splashScreen.hide();

      this.backgroundMode.enable();

      this.powerManagement.acquire()
      .then(res => {
        console.log('Wakelock acquired!');
      })
      .catch(err => {
        console.log('Failed to acquire wakelock: ', err);
      });

      this.powerManagement.setReleaseOnPause(false).then(
        _ => {
          console.log('setReleaseOnPause successfully');
        },
        err => {
          console.log('Failed to set');
        }
      );


	// Crashes Android app after some secs...
      /*this.powerManagement.dim().then(
        _ => {
          console.log('Wakelock acquired');
        },
        err => {
          console.log('Failed to acquire wakelock: ', err);
        }
      );*/

      this.initAndStartGeoloc();
    });
}


private initAndStartGeoloc(): any {
    const config: BackgroundGeolocationConfig = {
	// Have to use exact value cause it won't find / import the const
      locationProvider: 1, // BackgroundGeolocationProvider.ANDROID_DISTANCE_FILTER_PROVIDER,
      
	  desiredAccuracy: 10,
      stationaryRadius: 20,
      distanceFilter: 30,
      
	  interval: 4900,
      fastestInterval: 4900,
      
	  startForeground: true,
      stopOnStillActivity: false,
      debug: false, //  enable this hear sounds for background-geolocation life-cycle.
      stopOnTerminate: false, // enable this to clear background location settings when the app terminates

      syncThreshold: 1,

      url: this.locService.URL,
      httpHeaders: {
         'Access-Control-Allow-Origin': '*'
      },
      // customize post properties
       postTemplate: {
        latitude: '@latitude',
        longitude: '@longitude',
        timeInMs: '@time'
       }
    };
	
	this.backgroundGeolocation.configure(config)
		  .then(() => {

			timer(1000, 15000).subscribe(_ => {
			  this.backgroundGeolocation.getCurrentLocation().then(res => {
				// Check if valid then POST
			  });
			  // Same for stationary if only that's available...
			});
			
			this.backgroundGeolocation.headlessTask(function (event) {
			  if (event.name === 'location' ||
				  event.name === 'stationary') {
				// POST location
			  }
			  return 'Processing event: ' + event.name; // will be logged
			});
			
			// Event listeners for BackgroundGeolocationEvents.location, ...etc
			
	});

	this.backgroundGeolocation.start();
}

I have the same problem too. One minute you think it’s working, the next minute it doesn’t work. If I keep my app in the foreground, it works great.
Other times, if I run it in the background, with say Google Maps doing some actual mapping, then my app stops working, such as not posting the geo json data. Then, I’ll get a big jump on the screen of the locations.
Then, other times, the app recovers to where I had left it, other times, the whole thing has just reopened and is now running like it’s the first time it’s been run.
It’s the weirdest thing, but it’s still very hit and miss and rather frustrating.
I’m running an Huawei thing.