Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Background Mode is running for only 5 min in Android 9 #430

Open
HimanshuBiz4 opened this issue Feb 26, 2019 · 33 comments
Open

Background Mode is running for only 5 min in Android 9 #430

HimanshuBiz4 opened this issue Feb 26, 2019 · 33 comments

Comments

@HimanshuBiz4
Copy link

This plugin is working for Android 8+ and older but not properly working for Android 9.
When I am connecting Sensor to my application in android 9 after 5 min in background application not getting data and again start sending data after reopening the application.

I think the plugin is not updated for android 9 power management updates.
https://developer.android.com/about/versions/pie/android-9.0-changes-all

Android Version - 9
Ionic Version - 9

@ziyaddin
Copy link

Are you sure that you are using the latest git version of the plugin and you have enabled the plugin inside platform.ready block?

@HimanshuBiz4
Copy link
Author

Yes I have enabled that inside platform.ready block and i have latest git version.
I am facing this issue only for android 9.

@KinG-InFeT
Copy link

+1

@MyoungboKim
Copy link

MyoungboKim commented Mar 19, 2019

+1
I'm having the same problem.
Do you have any suggestions for handling this dilemma?

@tutiihofer
Copy link

+1.

Anybody have any solution?

@rafaelgalle
Copy link

+1
I'm having the same problem.

@tutiihofer
Copy link

I founded the solution at #434

@rafaelgalle
Copy link

I founded the solution at #434

I tried this solution and it did not work, to continue running beyond the 5 minutes I had to go to settings -> battery -> battery optimization and check to not optimize my application.

@tobika
Copy link

tobika commented Apr 1, 2019

@rafaelgalle this is great. I see that there is already the functionality in this plugin (not released on npm yet) to disable battery optimization in the code. disableBatteryOptimizations
537f7a8

Did anybody use this already?

@MyoungboKim
Copy link

MyoungboKim commented Apr 5, 2019

@tobika
This function is not available in ionic3.

Outputs the following message:
Property 'disableBatteryOptimizations' does not exist on type 'BackgroundMode'. Did you mean 'disableWebViewOptimizations'?

I'm install plugin
$ionic cordova plugin add https://github.com/katzer/cordova-plugin-background-mode.git.

How can I use it?

@tobika
Copy link

tobika commented Apr 5, 2019

@MyoungboKim are you using the ionic native wrapper @ionic-native/background-mode to access this plugin? It is normal that this is not updated because this feature on the background-mode plugin has not been published to npm yet.

You should be able to enable this function without the wrapper like so :

        cordova.plugins.backgroundMode.on('activate', () => {
          console.log('ACTIVATE background mode');
          cordova.plugins.backgroundMode.disableWebViewOptimizations();
          cordova.plugins.backgroundMode.disableBatteryOptimizations(); // <- HERE
        });

@tobika
Copy link

tobika commented Apr 5, 2019

I just saw in this commit that we have to add the permissions on our own: a966905#diff-53f390d375398624afe1cfe1125f42bf

any special reasons for this @katzer ? (by the way, thanks for this amazing plugin :) )

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

@MyoungboKim
Copy link

MyoungboKim commented Apr 5, 2019

@tobika
Thank you for your answer.
The solution was completely solved.
cordova.plugins.backgroundMode.disableBatteryOptimizations();

@n1705771
Copy link

n1705771 commented Apr 12, 2019

I have installed the latest background mode plugin,
I can see the definition of function disableBatteryOptimizations() in the cordova background plugin module's src/android/disableBatteryOptimizations

but I got error when I build:
Property 'disableBatteryOptimizations' does not exist on type 'BackgroundMode'. Did you mean
'disableWebViewOptimizations'?

why this happens?

My code is:

  this.backgroundMode.on("activate").subscribe(()=>{
       this.backgroundMode.disableWebViewOptimizations();
      this.backgroundMode.disableBatteryOptimizations();
      console.log("background activate !!!!");
  });

background mode plugin version is:

version="0.7.2"

@MyoungboKim
Copy link

@n1705771
declare var cordova;
cordova.plugins.backgroundMode.disableBatteryOptimizations();

@n1705771
Copy link

n1705771 commented Apr 12, 2019

@MyoungboKim still got build error:
[14:57:42] typescript: C:/ionicapp/src/pages/home/home.ts, line: 130
Modifiers cannot appear here.

 L129:  this.backgroundMode.disableWebViewOptimizations();
 L130:  declare var cordova;
 L131:  cordova.plugin.backgroundMode.disableBatteryOptimizations();

[14:57:42] ionic-app-script task: "build"
[14:57:42] Error: Failed to transpile program
Error: Failed to transpile program

Code is like:

 this.backgroundMode.on("activate").subscribe(()=>{
    this.backgroundMode.disableWebViewOptimizations();
    declare var cordova;
    cordova.plugins.backgroundMode.disableBatteryOptimizations();
    console.log("background activate !!!!");
  });

background mode plugin version is:

    version="0.7.2"

@MyoungboKim
Copy link

MyoungboKim commented Apr 12, 2019

@n1705771
I downloaded the plugin and installed it as a local plugin.
"declare var cordova;" was declared a global variable at the lower limit of "import".

@n1705771
Copy link

@MyoungboKim Thanks. I successfully build it following your direction. But the plug-in seems doesn't solve the problem. Still running for only 5 min if without power cable plugged. Does it work for you?

@tobika
Copy link

tobika commented Apr 12, 2019

@n1705771 your final code looks alright, you get the console.log at the end?

did you add <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
in
platforms/android/app/src/main/AndroidManifest.xml

Maybe this makes a difference? I haven't had the 5min problem yet because my app is constantly streaming music which seems to keep it alive.

Just general info: Keep in mind that the @ionic-native bindings are not up to date with the github plugin version.

@n1705771
Copy link

@tobika
I did add permission in androidmanifest.xml, but it doesn't help. I check the console.log, cannot see

"background activate !!!!", 

and find the background mode error:

Uncaught TypeError: cordova.plugins.backgroundMode.ondeactivate is not a function

The new code is as below, I declare the var cordova as a global variable at the lower limit of "import", build was succeeded.

this.backgroundMode.on("activate").subscribe(()=>{
  this.backgroundMode.disableWebViewOptimizations();
  cordova.plugins.backgroundMode.disableBatteryOptimizations();
  console.log("background activate !!!!");
});

@tobika
Copy link

tobika commented Apr 12, 2019

@n1705771 can you tell me which version of @ionic-native you are using?
for me there is a problem in @ionic-native/background-mode@^5.4.0

the .on is not correct
on(event: string, callback: (...args: any[]) => void, scope?: object): Observable<any>;
it should be
on(event: string): Observable<any>;
I'll try to do a PR one of these days.

About your other error, I don't have this one but I see you found the existing issue about it.

@n1705771
Copy link

@tobika great thanks! Can you tell me how to check the version?
what I did is update the background-mode to the latest one by using command:

 npm install --save @ionic-native/background-mode@latest

Then I got the result as:

 ....
 + @ionic-native/background-mode@5.4.0
 updated 1 package and audited 3854 packages in 8.914s
 found 20 vulnerabilities (4 low, 11 moderate, 5 high)
 run `npm audit fix` to fix them, or `npm audit` for details

So, I think version is 5.4.0

@tobika
Copy link

tobika commented Apr 12, 2019

@n1705771 ok, but as I said ths version is a bit incorrect which will result in your event not being called.
As a workaround you can apply the modification I wrote above in the file:
node_modules/@ionic-native/background-mode/ngx/index.d.ts

@n1705771
Copy link

n1705771 commented Apr 12, 2019

@tobika I deleted background-mode plugin in folders of node_modules and plugins, also removed the background-mode info from package.json and package-lock.json. Then, install the latest plugin again.

After that, the version became:

"@ionic-native/background-mode": "^4.20.0"

I have checked the

 node_modules/@ionic-native/background-mode/ngx/index.d.ts

It is:

 on(event: string): Observable<any>;

I rebuild the program, but still have the same error:

Uncaught TypeError: cordova.plugins.backgroundMode.onactivate is not a function
at <anonymous>:1:80

@n1705771
Copy link

n1705771 commented Apr 15, 2019

I fixed about issue as the solution given in #431.

Now I see console log:

"background activate !!!!"

But still issue exists! I set 1 min interval for app to upload some data to server, the app only works well if the external power is supplied to my android phone (connect power cable to phone with external power source).

My code is:

if (this.platform.is('android')){
  this.backgroundMode.enable();

  this.powerManagement.dim().then((value)=>{
        console.log('enablebackground: Wakelock acquired');
        this.powerManagement.setReleaseOnPause(false).then(()=>{
            console.log('enablebackground: setReleaseOnPause success');
        }).catch(()=>{
            console.log('enablebackground: setReleaseOnPause Failed to set');
        });
    }).catch(()=>{
      console.log('enablebackground: Failed to acquire wakelock');
    });

  this.backgroundMode.on("activate").subscribe(()=>{
    this.backgroundMode.disableWebViewOptimizations();
    cordova.plugins.backgroundMode.disableBatteryOptimizations(); //disable Battery optimizations
    console.log("background activate !!!!");

  });
  this.backgroundMode.on("deactivate").subscribe(()=>{
    console.log("background deactivate");
    this.locationtracker.stopTracking();
    setTimeout(()=>{
      startUpload();
    }, 500);
  });
} else {
  this.backgroundMode.enable();
}

@MyoungboKim
Copy link

MyoungboKim commented Apr 15, 2019

@n1705771
Do you use the function of "excludeFromTaskList()"?
If you use "excludeFromTaskList()" in "Android Pie" when I check it out, the background action ends when you start another app.

if not covered by the above.
ionic cordova plugin rm cordova-plugin-background-mode
ionic cordova plugin add https://github.com/katzer/cordova-plugin-background-mode.git
This might help.

@n1705771
Copy link

n1705771 commented Apr 15, 2019

Thanks. I removed the plugin and re-installed the latest plugin.

For my Huawei Mate 10, after turning off the power save mode in battery setting, it works! Seems this issue also related to different brands of phone manufactures. (#400), I am curious if there is a way can automatically set the power save mode off when once the application is opened?

@MInesGomes
Copy link

MInesGomes commented Apr 17, 2019

I installed

https://github.com/lkonzen-garupa/cordova-plugin-background-mode

And in plugin.xml is already this:

 <config-file target="AndroidManifest.xml" parent="/manifest">
            <uses-permission android:name="android.permission.WAKE_LOCK" />
            <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
            <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
        </config-file>

and add the code for wake_lock, Working in android 5, 7, 10 :-)

luca-itro pushed a commit to itroteam/cordova-plugin-background-mode that referenced this issue Apr 25, 2019
Battery optimization issue fix: katzer#430 (comment)
@Peiyongliang
Copy link

my hpone is honor10 android9,this plugin didnot work ~~~who can help me,thanks!

@vrdriver
Copy link

Thanks. I removed the plugin and re-installed the latest plugin.

For my Huawei Mate 10, after turning off the power save mode in battery setting, it works! Seems this issue also related to different brands of phone manufactures. (#400), I am curious if there is a way can automatically set the power save mode off when once the application is opened?

I have a P20 Mate Pro, and it stops a background upload after 60 minutes. I tried this with 3 programs all doing some GPS tracking and they all stopped at exactly the same time. It's like the phone just like to deep sleep for no reason.

@woodz-
Copy link

woodz- commented Jul 15, 2020

With no upload / download or GPS magic in action, but with a continuous ble connection on Android 10 it does its job well if you follow the approach in "Quirks" section:

cordova.plugins.backgroundMode.on('activate', function() {
   cordova.plugins.backgroundMode.disableWebViewOptimizations(); 
});

But why does performance decrease (application resource usage increases, especially CPU) triply? How could this be explained?

@maikonigor
Copy link

you can call the wakeUp method at a given time.

const timer = 4*60*1000; // four minutes
 this.backgroundMode.enable();
 this.backgroundMode.on('activate')
    .subscribe(()=>{
       setInterval(()=>{
            this.backgroundMode.disableBatteryOptimizations();
            this.backgroundMode.disableWebViewOptimizations();
            this.backgroundMode.wakeUp();
            this.backgroundMode.moveToForeground();
          },timer);
    });

@YSoni89
Copy link

YSoni89 commented Aug 4, 2022

@katzer ,
I am trying to use this plugin for one of my use case. I need to run some processes in background when app (built in ionic 5 and angular 10) is minimize and screen is locked. I am using Samsung X cover Android 10 + version. Its working fine for longer period of time (approx. 14-15 hours) if I plugin my phone (USB cable charging on), minimize the App with screen lock but it does not give similar results if I unplug the phone from charger. App stop the background processes after 2-3 hours.

Can you please advice here?

Below is the code implemented ->

On device platform ready => below code is written
this.background.setDefaults();
this.background.enable();
this.background.on('activate').subscribe(() => {
this.background.disableWebViewOptimizations();
this.background.disableBatteryOptimizations();
});

I tried using this.powerOptimization.RequestOptimizations();
this.powerOptimization.IsIgnoringBatteryOptimizations();
along with above options.

Permissions are set in my AndroidManifest.xml file such as -


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests