Chariotwheel
Chariotwheel

Reputation: 11

Background Mode not quite working, Ionic App sleeps after 5 minutes

I've created an Ionic App with Capacitor that's supposed to send the GPS coordinates to an API every minute. That works generally, but when it comes to working when the screen is locked it gets troublesome.

Now, I've included cordova-background-mode, as well as ForegroundService. Yet, it still stops after 5 minutes. I've also used

this.backgroundMode.disableWebViewOptimizations();
this.backgroundMode.disableBatteryOptimizations(); 

but to no avail.

I even manually deactivated Battery Optimisation in the settings.

What am I overlooking?

Upvotes: 1

Views: 1751

Answers (2)

Feinster
Feinster

Reputation: 59

If anyone needs a solution, this works with android 10 + Capacitor + native backgrounde mode:

if (this.platform.is('android')) {

  this.platform.ready().then(() => {
    this.backgroundMode.enable();
    this.backgroundMode.setEnabled(true);
    this.backgroundMode.on('activate').subscribe(() => {
      this.backgroundMode.disableWebViewOptimizations();
      this.backgroundMode.disableBatteryOptimizations();
    });
    this.backgroundMode.disableBatteryOptimizations();
  });

  const timer = 3 * 60 * 1000; // three minutes

  setInterval(() => {
    this.backgroundMode.wakeUp();
  }, timer);
}

Also add in manifest:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_SERVICE"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />

Upvotes: 1

Callan
Callan

Reputation: 1233

That plugin is broken. I had many failed attempts to make it work but failed.

The only solution I found was to use this one instead. It works even if the screen is locked for hours.

Upvotes: 0

Related Questions