katzer / cordova-plugin-background-mode

Keep app running in background
Apache License 2.0
1.38k stars 1k forks source link

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

Open mpfurnari opened 5 years ago

mpfurnari commented 5 years ago

Test Environment:


* 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. 
victorhugoweb commented 5 years ago

same here Dont works in android 8

luisvt commented 5 years ago

after 5 minutes of running the app in the background in android the app stops

mpfurnari commented 5 years ago

after 5 minutes of running the app in the background in android the app stops

That's what I'm seeing as well. I'm also noticing that the notification isn't showing up as described in the plugin. I've tried a few of the forks that fix the notification issue for 8.0... they fix the notification issue, but I was still seeing what you are seeing.

iverveinc commented 5 years ago

Same problem with me too. It works for few mins after going to background and then stops until I go back to foreground. And I guess this issue is coming with Samsung devices more often. Working slightly better on Nexus and other devices.

luisvt commented 5 years ago

what I found in my testing was that after disabling webview optimizations, the code worked correctly:

this.backgroundMode.on('activate', () => this.backgroundMode.disableWebviewOptimizations());
mpfurnari commented 5 years ago

what I found in my testing was that after disabling webview optimizations, the code worked correctly:

this.backgroundMode.on('activate', () => this.backgroundMode.disableWebviewOptimizations());

What device and OS are you working with? I'm testing on a Galaxy S9+, 8.0. I'll give this a try tonight.

luisvt commented 5 years ago

Nexus 6p, android 8.1.0

victorhugoweb commented 5 years ago

not working after +- 5 minutes Android 8.0

iverveinc commented 5 years ago

Same here I tried several times by Disabling Web View Optimisation.Its working better in Nexus but have a problem with Samsung. I guess Its because Samsung has custom OS which is customised Android OS.

mpfurnari commented 5 years ago

what I found in my testing was that after disabling webview optimizations, the code worked correctly:

this.backgroundMode.on('activate', () => this.backgroundMode.disableWebviewOptimizations());

I tried this with my Samsung s9+, still fails to stay running. I send a ping every 30 seconds to the phone. At just under 4 minutes, the phone fails to reply to the ping. The ping timeout is set to about 40 seconds, so that means the phone app is stalling about 3 min 30 sec.

iverveinc commented 5 years ago

@katzer Does anyone managing this plugin anymore? This issue is really critical :(

brunobartels commented 5 years ago

Same problem with 8.0

Moto G, Sansung, Azuz etc., to andrond 7 Works well. LG stop more frequently. It only closes if you stress the system too much and call the camera, for example. It is still possible to circumvent predicting the user, and optimizing the maximum memory and CPU resource during persistent use, because it counts more in the decision of the system to encessr or not the application.

Moto X 8.0 With promises reported. In fact the service apparently is not really started, because neither the notification in my case works.

mpfurnari commented 5 years ago

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...

victorhugoweb commented 5 years ago

@mpfurnari Do this trick works on all android?

mpfurnari commented 5 years ago

@mpfurnari Do this trick works on all android?

Not being completely familiar with the underlying design of this backgroundmode plugin, I'm not certain that I'd call the wake-lock solution a trick. But, to answer your question, yes this works on Android. Specifically, I've tested this on a Samsung S9+ running Android 8.0. I'm only using this for Android. The backgroundmode plugin seems to work as-is for iOS.

victorhugoweb commented 5 years ago

i need keep open a websocket connection on background

mpfurnari commented 5 years ago

i need keep open a websocket connection on background

That's exactly what's I'm doing...

mpfurnari commented 5 years ago

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...

I've been running for hours with this solution... tested through phone calls, idle times, on a charger and off a charger. It seems to work. I'll be shipping with this solution. As I mentioned in my last comment, I'm setting up a WebSocket and need this to keep the other end informed that I'm still alive...

brunobartels commented 5 years ago

The mpfurnari solution has momentarily answered, even if it is not yet the ideal form, as I'm afraid the nescessity of it keeps the screen on and the application in the foreground :/ . The truth is that there is only one really correct and stable way to do this: with java through service. I'm trying to integrate a real service into android (outside the webview environment) natively developed in java via plugin. However all behavior and methods must be done directly in the plugin, in java. It is not possible in this case to create a universal method because there is no java / javascript interaction for this case. What seems to be a possible solution (still in testing) is to have the application call the service and receive data from it and vice versa. If the application is paused or closed, the service will continue. If the application reopens, it collects the updated data gutted by the service.

I'm working together with a java developer for this. If you succeed, step here.

Hugs!

mpfurnari commented 5 years ago

The mpfurnari solution has momentarily answered, even if it is not yet the ideal form, as I'm afraid the nescessity of it keeps the screen on and the application in the foreground :/ . The truth is that there is only one really correct and stable way to do this: with java through service. I'm trying to integrate a real service into android (outside the webview environment) natively developed in java via plugin. However all behavior and methods must be done directly in the plugin, in java. It is not possible in this case to create a universal method because there is no java / javascript interaction for this case. What seems to be a possible solution (still in testing) is to have the application call the service and receive data from it and vice versa. If the application is paused or closed, the service will continue. If the application reopens, it collects the updated data gutted by the service.

I'm working together with a java developer for this. If you succeed, step here.

Hugs!

Agreed this solution is not ideal in that it requires 2 plugins to make it work. However, you might want to try the solution before making comments. This does not keep the screen on... it stays off as it should. It also does not keep the application in the "foreground"; In fact, the application doesn't come to the top unless called upon. The cost of this (in my solution and measured in battery consumption) appears to be close to the solution without the partial wake-lock. That said, I welcome a solution which provides an equivalent service layer that can easily be customized for a cordova app. Until then, I'm good with the partial wake-lock and the backgroundmode solution.

brunobartels commented 5 years ago

In my case, even with the two plugins, activating the window.powerManagement.dim mode, but putting the app in the background and stressing the system, the android paused the application (moto g5). If the app stays in the foreground, keeping the "dim" state on, it really does not stop. But I did not find a relation of the "dim" state with a supposed response of the system in killing or not the application when in the background. If the system needs resources, it will kill! Even because, from what I noticed, when you put the application in the background, this state is paused, only resuming if it is to the foreground again. But I pray that my ignorance is wrong.

I launched the solution for beta testers and I will monitor whether the report of problems with "normal" use decreases. I'm very fond of it, believe me!

I've been trying to get around this for over a year. As much as Cordova's own documentation does not recommend this and imposes this limitation, we try to stubbornly. I have combined all kinds of plugin and behavior. But I am more and more convinced that there really is nothing that can be done other than trying to develop a service through Plugin and with the methods directly in Java. Google Maps itself, for example, google native app, by not using a service, if put in background mode and stressing Android with the use of links and camera, it is dead, only maintain the state of form really stable in " acquire "(foreground and with the screen attached). Why would mine be any different?

Unfortunately, because of the limitations of the platform and the way it works, I do not see today how this so-called background services plugin can be developed and integrated with the webview JS, where it installs and submits to the javascript methods as a plugin any . It would be a kind of plugin itself, where your service is really built within the JAVA environment.

Maybe in a future version of Cordova? Do not know!

I am guided by this content: https://www.red-folder.com/blog/phonegap-android-background-service

mpfurnari commented 5 years ago

Agreed, if the app is kept in the foreground with this solution, it doesn't behave as desired when the phone is locked etc. My workaround for this will be to force the app into the background. It works for my application, but maybe not for everyone.

I haven't tried stressing the system, but I could see this causing issues with even just basic performance. Again, for my application, I'm not so concerned about excessive stress scenario. (My typical use case is with the phone otherwise idled.)

I'm very open to move the necessary functions to a service layer. Specifically, I'm trying to run an MQTT connection (over websockets for now, but not required.) Ultimately, though, my service layer needs to interrupt the user with notifications and vibrations.... I imagine my needs are not so different than many others developing system solutions that are not just basic phone apps.

I'd be very interested in what you uncover!

For now, though, this will have to do....

khayargoli commented 5 years ago

Is this about using API 26 as target sdk in android code or running any targeted sdk code on android 8.0 or above devices?

mpfurnari commented 5 years ago

Is this about using API 26 as target sdk in android code or running any targeted sdk code on android 8.0 or above devices?

As far as I know, the issue is specific to Android 8.0 running on Samsung. @luisvt reported a similar problem but different solution (above) running on a Nexus 6p 8.1.

mpfurnari commented 5 years ago

Following up on the undesired lock-screen behavior, I've added the following to my initialization: this.onPauseSubscription = this.platform.pause.subscribe(() => { this.backgroundmode.moveToBackground(); }); and added this to my cleanup: this.onPauseSubscription.unsubscribe();

aminholmes commented 5 years ago

@mpfurnari , thanks so much for posting your fix. I have just implemented it in our test env and it appears to be working! This if the first time we've been able to get our app to last past 5 min since this error started happening with Android devices. I am going to test overnight and let you all know how it goes. I'm encouraged so far.

mpfurnari commented 5 years ago

@aminholmes Hey Amin, Glad to hear that it's working for you! Matt

khayargoli commented 5 years ago

I was running on android P on oneplus 6. I have enabled the notification and it is being displayed on android 6.0 devices but not on P.

iverveinc commented 5 years ago

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.

mpfurnari commented 5 years ago

I was running on android P on oneplus 6. I have enabled the notification and it is being displayed on android 6.0 devices but not on P.

I believe the tushe fork provided by @iverveinc corrects the notification issue. Still, with this fork, I was unable to run for long on a Samsung S9+ Android 8.0 with only the backgroundmode plugin. Failing that on tushe's fork, I tried the wake-lock solution with katzer's version of backgroundmode and I was able to run.

If someone can confirm that they are getting tushe's fork to work on a Samsung S9, I'll dig deeper...

iverveinc commented 5 years ago

@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.

gerard-d-kelly commented 5 years ago

@mpfurnari Noticing similar behaviour with Android 8.* on OnePlus 5T, Huawei and Sony models. I have implemented the wake-lock workaround, but still seeing the same type of behaviour (5 mins of background work). Going to spend some time today ensuring I have implemented the fix correctly, will update if I get anywhere.

mpfurnari commented 5 years ago

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

} `

Danthemanwenttojapan commented 5 years ago

@mpfurnari I was wondering what problems you were having regarding when the phone is locked, which prompted you to force the application to the background, as I have not seen any problems with a Samsung Galaxy s8. I am also using a websocket that must stay open.

Danthemanwenttojapan commented 5 years ago

@gerard-d-kelly Did it work for you at the end? Try with setting the cordova.plugins.backgroundMode.disableWebViewOptimizations(); in the onactivate clause.

mpfurnari commented 5 years ago

@mpfurnari I was wondering what problems you were having regarding when the phone is locked, which prompted you to force the application to the background, as I have not seen any problems with a Samsung Galaxy s8. I am also using a websocket that must stay open.

@Danthemanwenttojapan The phone screen would stay on, and the app would be in focus even though the screen was "locked". Forcing the app to the background eliminated this problem.

gerard-d-kelly commented 5 years ago

@Danthemanwenttojapan I spent 3-4 days trying different solutions including the disableWebViewOptimizations() and still couldn't get it working correctly. Android 8 seems to be very aggressive towards background processes even when writing natively. I have moved on to writing a native app that will run the "always on" background tasks I need and use my angular 6 as a front end embedded in a webview using cordova. Like I said, Android 8 is very agressive and I think this is the best solution in my case. Moving forward using another cross-platform angular to mobile framework might be better, one which allows easier access to native APIs and doesn't use webviews.

Danthemanwenttojapan commented 5 years ago

@mpfurnari That's interesting, I'm not getting the same phenomena with with a Cordova implementation (Not ionic), with Android 8, galaxy s8. I used all the solutions including backgroundMode.disableWebViewOptimizations();

Danthemanwenttojapan commented 5 years ago

@gerard-d-kelly It's working for me with all the solutions here together on a Android 8 with galaxy s8 (I must maintain a websocket). The plugin (I didn't look too carefully at the code), creates a foreground (or background) service, which should work. I don't have access to all the phones you mentioned, so was wondering if some Android implementation pauses foreground services. Try disabling doze mode for your app (Under battery management), and let me know what happened (If that is ok). I don't think this should be necessary for a foreground service.

nagthgr8 commented 5 years ago

i need keep open a websocket connection on background

Is this work-around to keep the App alive in background?

nagthgr8 commented 5 years ago

@gerard-d-kelly It's working for me with all the solutions here together on a Android 8 with galaxy s8 (I must maintain a websocket). The plugin (I didn't look too carefully at the code), creates a foreground (or background) service, which should work. I don't have access to all the phones you mentioned, so was wondering if some Android implementation pauses foreground services. Try disabling doze mode for your app (Under battery management), and let me know what happened (If that is ok). I don't think this should be necessary for a foreground service.

I have done this still App got close.

nagthgr8 commented 5 years ago

Did anyone tried this solution https://github.com/Red-Folder/bgs-core/wiki/Build-your-own-Background-Service

nagthgr8 commented 5 years ago

In silent mode the plugin will not display a notification - which is not the default. Be aware that Android recommends adding a notification otherwise the OS may pause the app.

cordova.plugins.backgroundMode.setDefaults({ silent: true });

nagthgr8 commented 5 years ago

decided to develop the service in native code as I have come to the conclusion the plugins for such type of requirement will not work. The App will close period, background service is something need, s to be out proc and not in-proc, the plugins here I guess works as in-proc and when the App closes the service will close bhoom over :( more here

gondp commented 5 years ago

Is there any solution for this? For what functions on Android 8+

n1705771 commented 5 years ago

I have the same issue with background mode. The app stops after 5 minutes if not power plug in. But it works well when I plug power cable in my android phone.

How to make phone always think that power cable is plugged even though it is not?

tajindersinghnamdhari commented 5 years ago

Try below, solved the crashing issue for me on Android 8+ https://github.com/katzer/cordova-plugin-background-mode/issues/393#issuecomment-449635095

Tested working on 8.0 on device, while 8.1 & 9.0 on emulator.

devi255 commented 5 years ago

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.

Work on android@7.1.4 thx...

n1705771 commented 5 years ago

@devi255 It doesn't work for me. still stop in 5 minutes.

thx

ziyaddin commented 5 years ago

@n1705771 are you sure that you are using the latest git version of the plugin?