question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Background mode not working on Android 8.0.0. Working on earlier versions

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:8
  • Comments:136

github_iconTop GitHub Comments

8reactions
MInesGomescommented, Sep 14, 2019

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 !!!');
    });
4reactions
mpfurnaricommented, Oct 2, 2018

@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();
    }
}

} `

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android 8.0 Behavior Changes
These behavior changes apply to all apps when they run on the Android 8.0 (API level 26) platform, regardless of the API level...
Read more >
Fix an installed Android app that isn't working - Google Support
Try the following steps if an app installed on your phone has any of these problems: Crashing. Won't open. Won't respond. Isn't working...
Read more >
Android 8.0: keeping a background service alive
That's what Google tells us. A process is not forever. If you feel you need your foreground service to stay alive permanently, ...
Read more >
Android Oreo - Wikipedia
Android Oreo is the eighth major release and the 15th version of the Android mobile operating system. It was first released as an...
Read more >
Dark Mode - Apps on Google Play
This app helps to activate the Android night mode on devices that do not provide this option in the system settings. This app...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found