cordova-plugin-background-mode
cordova-plugin-background-mode copied to clipboard
Background Mode is running for only 5 min in Android 9
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
Are you sure that you are using the latest git version of the plugin and you have enabled the plugin inside platform.ready
block?
Yes I have enabled that inside platform.ready block and i have latest git version. I am facing this issue only for android 9.
+1
+1 I'm having the same problem. Do you have any suggestions for handling this dilemma?
+1.
Anybody have any solution?
+1 I'm having the same problem.
I founded the solution at #434
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.
@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
https://github.com/katzer/cordova-plugin-background-mode/commit/537f7a859f6ae7aacadcc42eb6db28571a20eea1
Did anybody use this already?
@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?
@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
});
I just saw in this commit that we have to add the permissions on our own: https://github.com/katzer/cordova-plugin-background-mode/commit/a9669052c0e927eedaa03d896217a011b87eb43c#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" />
@tobika
Thank you for your answer.
The solution was completely solved.
cordova.plugins.backgroundMode.disableBatteryOptimizations();
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"
@n1705771
declare var cordova;
cordova.plugins.backgroundMode.disableBatteryOptimizations();
@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"
@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".
@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?
@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.
@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 !!!!");
});
@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.
@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/[email protected]
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
@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
@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
I fixed about issue as the solution given in https://github.com/katzer/cordova-plugin-background-mode/issues/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();
}
@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.
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. (https://github.com/katzer/cordova-plugin-background-mode/issues/400), I am curious if there is a way can automatically set the power save mode off when once the application is opened?
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 :-)
my hpone is honor10 android9,this plugin didnot work ~~~who can help me,thanks!
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.
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?
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);
});