Power Management plugin not working ionic 2

Hi, I’m using the plugin ionic native power-management with ionic v2, my intention is to not doze the app after 5 minutes in background (user is either looking at other app or just minimized). Here is my code:

app.components.ts

import { BackgroundMode, BackgroundModeConfiguration } from ‘@ionic-native/background-mode’;
import { PowerManagement } from ‘@ionic-native/power-management’;
import { AndroidPermissions } from ‘@ionic-native/android-permissions’;
import { Cordova } from ‘@ionic-native/core’;

/other libs/plugins/

@Component({
templateUrl: ‘app.html’
})
export class GCEApp {

constructor(
private backgroundMode: BackgroundMode,
private powerManagement: PowerManagement,
private androidPermissions: AndroidPermissions) {
}

public ngOnInit(): void {

this.translateService.setDefaultLang('pt-BR');

this.platform.ready().then(() => {
    
    this.hideSplashScreen();

    this.androidPermissions.requestPermissions([this.androidPermissions.PERMISSION.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS]);

    this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS).then(
        result => console.log('Has permission REQUEST_IGNORE_BATTERY_OPTIMIZATIONS -> ',result.hasPermission),
        err => this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS));

    this.backgroundMode.enable();
    this.powerManagement.dim()
        .then((ok) => {
            console.log('PartialWakeLock enabled!');
            console.log(ok);
        })
        .catch((err) => {
            console.log('PartialWakeLock error!');
            console.log(err);
        });

    this.powerManagement.setReleaseOnPause(false)
        .then(ok => {
			console.log('setReleaseOnPause = false successfully');
			console.log(ok);
        }, function(err) {
			console.log('ERR setReleaseOnPause = false');
			console.log(err);
        });

});

}

The code above logs that I have permission to disable battery optimizations and that I acquired the partial wakelock with powerManagement.dim(), but after 5 minutes the app stops and only resumes if the user looks at the app again.

Testing with a Moto G2, Android v5.0.2.

Can somebody help? Thanks in advance!

Solved the issue using this plugin instead of the ionic-native power management: https://github.com/boltex/cordova-plugin-powermanagement

With this I was able to keep running the app even after the screen faded.

2 Likes