Hi,
I see that the ionic app we built is taking too much battery when running in background mode.
In the background mode – we only update the user’s location once an hour. I am using a setInterval timer which is set to run once an hour, and sends the user’s location to the server.
I see that the app takes about 40% of the battery when left in the background for about 8 hours.
There is no other code I have in the background service.
I am using cordova-plugin-background-mode.
Below is the code in the service.
function init() {
if (ionic.Platform.isWebView()) { // check if this runs on device
console.log('BackgroundService init');
// Android customization
cordova.plugins.backgroundMode.setDefaults({ text: 'KITIKI is running in the background' });
// // Enable background mode
cordova.plugins.backgroundMode.enable();
//
cordova.plugins.backgroundMode.onactivate = onEnterBackgroundMode;
cordova.plugins.backgroundMode.ondeactivate = onEnterForegroundMode;
}
}
var syncLocationInterval = 60*60*1000 // in ms -- once an hour
function onEnterBackgroundMode() {
console.log('App entering BACKGROUND MODE');
syncUserLocationWithServer(syncLocationInterval);
}
And in the function syncUserLocationWithServer, I am calling
$cordovaGeolocation.getCurrentPosition(positionOptions)
This is a show stopper for releasing the app – I really appreciate any help I can get from the community!
Kiran
1 Like
Those are third party plugins (ie not Ionic’s). I would suggest creating a test app that does not call GeoLocation at all. Send a dummy loc back to the server. Run that and see what battery life you get. If it is not draining the device, then the issue is either in that plugin or how you are using it within the app.
I created a test app which uses cordova-plugin-background-mode. – and all it does in the background service is to print to console log (a simple text) once an minute. Even that was taking a lot of battery – and the ios battery settings screen shows it is running in the background for about 12 hours.
I am puzzled as to why printing a simple text once a minute in the background takes so much batterry and how apps like facebook, foursquare and other apps which does backgroudn location tracking do no consume so much battery.
Are there any best practices for having logic in the backgroudn service?
Any help or advice please!!
I fell in similar problem, in an older device it was taking about 10% battery in an hour. I tried editing the code of cordova-background-mode, it seems it worked, you can fork also and do the same.
after forking, in the file https://github.com/katzer/cordova-plugin-background-mode/blob/master/src/android/BackgroundExt.java go to line 266, then update the line
int level = PowerManager.SCREEN_DIM_WAKE_LOCK |
to
int level = PowerManager.PARTIAL_WAKE_LOCK |
then remove the cordova-background-mode plugin from your project and add it from your forked plugin, command like
cordova plugin rm cordova-background-mode
cordova plugin add https://github.com/adnan333bd/cordova-plugin-background-mode.git
It’s also important to remember that when an Ionic app is running, it is running inside a constantly running web server to host the app.
It actually might be a good idea in your case to maybe program your own plugin that runs on its own thread of sorts so that it can stay active to update stuff but not be running a bunch of code that it doesn’t need to.
Just a thought