cordova-plugin-background-mode: Background mode not working on Android 8.0.0. Working on earlier versions

Test Environment:

  • Android 8.0.0
  • Phone: Galaxy S9+
  • Samsung experience version 9.0
  • Background Mode Version: cordova-plugin-background-mode 0.7.2 “BackgroundMode”
  • Cordova Android Version: 6.4.0
  • npm install output:
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.4 (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.4: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

  • Battery optimization disabled.

Symptoms:

  • Phone sleeps, fails several minutes after working in background
  • Notification is not showing up for backgroundmode
  • Logging the output of isEnabled(), I get an empty result (at least console.log doesn’t show anything).

I’m happy to do any debugging or additional information that might help.

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 8
  • Comments: 136

Commits related to this issue

Most upvoted comments

Thanks a lot @JuneTwooo, @nadimnakade Is working great 😃 and spending much less battery and less plugins. ( with the power Management plugin was more battery consuming .)

I don’t send the local notification, I set the sticky in the default options and working !!! 😃

I set id=-1 because I am also sending some normal notifications not sticky with id>=0 and would replace this sticky one

 const options = {
      id: -1,
      title: 'My App',
      text: 'Running in Background Mode',
      hidden: false,
      silent: false,
      sticky: true,
      resume: false,
      foreground: true,
    };

    await cordova.plugins.backgroundMode.setDefaults(options);
    await cordova.plugins.backgroundMode.enable();

    cordova.plugins.backgroundMode.on('activate', () => {
      cordova.plugins.backgroundMode.disableWebViewOptimizations();
      console.log('background mode activate !!!');
    });

@mpfurnari Hey, Would like to hear more about wake-lock solution in bit detail. I read above thread but not sure how to implement it. Can you help me with it.

I implemented this as an ionic provider. I’ve attempted to clean up my code for general use below, but I haven’t verified this version. My version is very similar…

` import { Injectable } from ‘@angular/core’; import { BackgroundMode } from ‘@ionic-native/background-mode’; import { Platform } from ‘ionic-angular’; import { PowerManagement } from ‘@ionic-native/power-management’;

@Injectable() export class backgroundProvider {

static backgroundMode;
static isAndroid;
static powerManagement;
static platform;
static onPauseSubscription;

constructor(public _backgroundMode: BackgroundMode,
    public _platform: Platform,
    public _powerManagement: PowerManagement) {
    backgroundProvider.backgroundMode = _backgroundMode;
    backgroundProvider.platform = _platform
    backgroundProvider.isAndroid = (backgroundProvider.platform.is('android'));
    backgroundProvider.powerManagement = _powerManagement;
    backgroundProvider.onPauseSubscription = undefined;
}


static paused() {
    // push us to the background in this case so that we're not driving the screen...
    backgroundProvider.backgroundMode.moveToBackground();
}

static enableBackground(forceBackgroundOnPause) {

    console.log("Enabling Background Mode");
    // https://forum.ionicframework.com/t/i-found-a-solution-for-some-regular-background-activity/27012

    backgroundProvider.backgroundMode.setDefaults({
        title: "My App Name",
        text: "Working for you in the background",
        icon: 'res://bgicon.png',
        hidden: true
    });
    if (backgroundProvider.isAndroid) {
        backgroundProvider.backgroundMode.enable();
        if (forceBackgroundOnPause) {
            backgroundProvider.onPauseSubscription = this.backgroundMode.platform.pause.subscribe(() => {
                console.log('paused')
                backgroundProvider.paused(); // when we pause, some apps are better left in the background so as to not keep the screen lit..
            });
        } else {
            backgroundProvider.onPauseSubscription = undefined;
        }

        backgroundProvider.powerManagement.dim(function () {
            console.log('enablebackground: Wakelock acquired');
            backgroundProvider.powerManagement.setReleaseOnPause(false, function () {
                console.log('enablebackground: setReleaseOnPause success');
            }, function () {
                console.log('enablebackground: setReleaseOnPause Failed to set');
            });
        }, function () {
            console.log('enablebackground: Failed to acquire wakelock');
        });
    } else {
        backgroundProvider.backgroundMode.enable();
    }
}

static disableBackground() {
    console.log("Disabling Background Mode");
    if (backgroundProvider.isAndroid) {
        if (backgroundProvider.onPauseSubscription != undefined) {
            this.backgroundMode.onPauseSubscription.unsubscribe();
        }
        this.backgroundMode.bgmode.disable();
        this.backgroundMode.powerManagement.release(function () {
            console.log('disableBackground: Wakelock released');
        }, function () {
            console.log('disableBackground: Failed to release wakelock');
        });
    } else {
        backgroundProvider.backgroundMode.disable();
    }
}

} `

I’ve seen this: https://altbeacon.github.io/android-beacon-library/foreground-service.html

so Android versions 8+ restrict services from running in the background to only 10 minutes after an app leaves the foreground.

… so the alternative is to use a foreground service: A foreground service is differs from regular Android background services in that it shows a persistent notification showing your app icon and configurable text.

… I’m looking for a Cordova Plugin that manage the foreground service if Android 8+ or background service if Android < 8.

@TheMoroccan09 It is working now in my Xiaomi 7.1.1 and not working in my cliente Galaxy S10+ 9 You can check the solutions for your mobile clicking in the corresponding icon here https://dontkillmyapp.com

It only works if device is plugged in power cable. I don’t know why this happens…

APP NAME keeps stopping when background mode is active. APP crashes.

I’ve seen this: https://altbeacon.github.io/android-beacon-library/foreground-service.html so Android versions 8+ restrict services from running in the background to only 10 minutes after an app leaves the foreground. … so the alternative is to use a foreground service: A foreground service is differs from regular Android background services in that it shows a persistent notification showing your app icon and configurable text. … I’m looking for a Cordova Plugin that manage the foreground service if Android 8+ or background service if Android < 8.

Did you found something @dariocavada ?

https://github.com/katzer/cordova-plugin-local-notifications (with sticky: true) like that :

cordova.plugins.notification.local.schedule( { title: ‘my app’, text: ‘my text’, icon: ‘file://icon.png’, smallIcon: ‘res://icon.png’, sticky: true, foreground: true });

@mpfurnari Hey, Would like to hear more about wake-lock solution in bit detail. I read above thread but not sure how to implement it. Can you help me with it.

I implemented this as an ionic provider. I’ve attempted to clean up my code for general use below, but I haven’t verified this version. My version is very similar…

` import { Injectable } from ‘@angular/core’; import { BackgroundMode } from ‘@ionic-native/background-mode’; import { Platform } from ‘ionic-angular’; import { PowerManagement } from ‘@ionic-native/power-management’;

@Injectable() export class backgroundProvider {

static backgroundMode;
static isAndroid;
static powerManagement;
static platform;
static onPauseSubscription;

constructor(public _backgroundMode: BackgroundMode,
    public _platform: Platform,
    public _powerManagement: PowerManagement) {
    backgroundProvider.backgroundMode = _backgroundMode;
    backgroundProvider.platform = _platform
    backgroundProvider.isAndroid = (backgroundProvider.platform.is('android'));
    backgroundProvider.powerManagement = _powerManagement;
    backgroundProvider.onPauseSubscription = undefined;
}


static paused() {
    // push us to the background in this case so that we're not driving the screen...
    backgroundProvider.backgroundMode.moveToBackground();
}

static enableBackground(forceBackgroundOnPause) {

    console.log("Enabling Background Mode");
    // https://forum.ionicframework.com/t/i-found-a-solution-for-some-regular-background-activity/27012

    backgroundProvider.backgroundMode.setDefaults({
        title: "My App Name",
        text: "Working for you in the background",
        icon: 'res://bgicon.png',
        hidden: true
    });
    if (backgroundProvider.isAndroid) {
        backgroundProvider.backgroundMode.enable();
        if (forceBackgroundOnPause) {
            backgroundProvider.onPauseSubscription = this.backgroundMode.platform.pause.subscribe(() => {
                console.log('paused')
                backgroundProvider.paused(); // when we pause, some apps are better left in the background so as to not keep the screen lit..
            });
        } else {
            backgroundProvider.onPauseSubscription = undefined;
        }

        backgroundProvider.powerManagement.dim(function () {
            console.log('enablebackground: Wakelock acquired');
            backgroundProvider.powerManagement.setReleaseOnPause(false, function () {
                console.log('enablebackground: setReleaseOnPause success');
            }, function () {
                console.log('enablebackground: setReleaseOnPause Failed to set');
            });
        }, function () {
            console.log('enablebackground: Failed to acquire wakelock');
        });
    } else {
        backgroundProvider.backgroundMode.enable();
    }
}

static disableBackground() {
    console.log("Disabling Background Mode");
    if (backgroundProvider.isAndroid) {
        if (backgroundProvider.onPauseSubscription != undefined) {
            this.backgroundMode.onPauseSubscription.unsubscribe();
        }
        this.backgroundMode.bgmode.disable();
        this.backgroundMode.powerManagement.release(function () {
            console.log('disableBackground: Wakelock released');
        }, function () {
            console.log('disableBackground: Failed to release wakelock');
        });
    } else {
        backgroundProvider.backgroundMode.disable();
    }
}

} `

I’m still on this solution…

Use this to keep app running when phone goes into (Android 6+) doze/standby mode.

https://github.com/thomas550i/cordova-plugin-doze-Optimize/

Hey Everyone,

I think this fork is working better. https://github.com/tushe/cordova-plugin-background-mode

Finally I was able to make it work for longer time.

I came across this solution mixing power management, partial wake-locks, and background mode: https://forum.ionicframework.com/t/i-found-a-solution-for-some-regular-background-activity/27012

Seems to do the trick for the samsung s9…