[Ionic-4] How to exclude my ionic app from the android Battery Optimization

Hi all, I’m a newbie here on the ionic/android developing world, so first, let me thanks anyone will answer me!

I’ve built a very simple Ionic 4 app that play some audio stream. It works nice, however the audio stops when the app goes in background, after more or less 3 minutes. This is due the Android Battery Optimization built in the system. (if you manually exlude the app from those optimizations, the app works forever…)

So my question: is there a way to exclude my app from those battery optimizations during the install process and/or at the first launch of my app?

I’ve been playing with the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS attribute into the AndroidManifest.xml but so far, didn’t get it work.

Here the versioning of my environment:

Ionic:

   ionic (Ionic CLI)             : 4.12.0 
   Ionic Framework               : @ionic/angular 4.2.0
   @angular-devkit/build-angular : 0.13.8
   @angular-devkit/schematics    : 7.2.4
   @angular/cli                  : 7.3.8
   @ionic/angular-toolkit        : 1.4.1

Cordova:

   cordova (Cordova CLI) : 9.0.0 (cordova-lib@9.0.1)
   Cordova Platforms     : android 8.0.0
   Cordova Plugins       : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 3.1.2, (and 8 other plugins)

System:

   Android SDK Tools : 26.1.1 
   NodeJS            : v8.9.3 
   npm               : 5.6.0
   OS                : Windows 10

Thanks again!

Maybe…? (not tested)

Hi @bilow, thanks for the answer.

I did try that native plugin, however it still seems to ignore it.
Here what I’ve tried:

  1. Installed that plugin as per instruction here (https://ionicframework.com/docs/native/power-management)

  2. Also tried your version (https://github.com/boltex/cordova-plugin-powermanagement)

  3. The statements in my code look like:

[...]
this.powerManagement.setReleaseOnPause(false);
this.powerManagement.dim();
[...]

Any other way to try?
Thanks again everyone.

Hi there, just an update for whoever is following this thread.

I’ve also tried the steps described here (I found a solution for some regular background activity!) but my app is still going off after a while.

Here my last setup, code in the “app.component.ts”:

[...]
this.backgroundMode.enable();
this.powerManagement.dim();
this.powerManagement.setReleaseOnPause(false);
[...]

My AndroidManifest.xml contains also

[...]
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
[...]

Thanks again.

Hi, me again… Just to share with you guys the solution I found so far.

There is a nice cordova plugin that fires a popup to the user asking him to “allow” the app working in background.
(https://github.com/thomas550i/cordova-plugin-doze-Optimize/blob/master/www/DozeOptimize.js)

It looks like is working now… :slight_smile:

Thanks again, and please shout if you guys think there’s a better solution/nicer code to do the same.

Happy code :wink: :computer:

P.S. Of course, I did remove from my project all the references to the background and powermanagement plugin, and also left the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS into the AndroidManifest.xml file.

How to use this plugin, can you give me an idea of ​​how to implement? thank you time

Did it really helped you on China based phones ? like OnePlus, Oppo, Xiomi etc ?