cordova-plugin-background-mode
cordova-plugin-background-mode copied to clipboard
Android 8+ issues and working plugin
Instead of replying to the several issues that already exist on this topic, I'll create a new "issue" or topic with a solution for the Android 8+ crashes.
I've given up on the "cordova-plugin-background-mode" plugin for Android 8+. Instead I came across this one: https://www.npmjs.com/package/cordova-plugin-foreground-service
It will of course depend on your project needs, whether this one will suffice, but it sure does the trick for my project for Android 8+, without crashes after 5 min!
Using the plugin in your project is as simple as this:
(<any>window).cordova.plugins.foregroundService.start('My Background Service', 'Keeps app active in background', null, 1);
And call stop() if you want to disable to plugin.
Hi, since I needed many of the features that this plugin has implemented after a lot of trial and error wanted to share my current settings which so far work from Android 5 to 8.1 (not tested on iOS however):
<plugin name="cordova-plugin-splashscreen" spec="5.0.2" />
<plugin name="cordova-plugin-ionic-webview" spec="^2.0.0" />
<plugin name="cordova-plugin-ionic-keyboard" spec="^2.0.5" />
<plugin name="cordova-sqlite-storage" spec="^2.3.3" />
<plugin name="cordova-plugin-device" spec="^2.0.2" />
<plugin name="cordova-plugin-nativeaudio" spec="^3.0.9" />
<plugin name="cordova-plugin-background-mode" spec="https://github.com/tushe/cordova-plugin-background-mode" spec="^0.7.2" />
<plugin name="cordova-plugin-app-version" spec="^0.1.9" />
<plugin name="cordova-plugin-statusbar" spec="^2.4.2" />
<plugin name="cordova-plugin-vibration" spec="^3.1.0" />
<plugin name="cordova-plugin-android-volume" spec="0.0.12" />
<plugin name="in.lucasdup.bringtofront" spec="https://github.com/markeeftb/cordova-bring-to-front" />
<plugin name="cordova-plugin-whitelist" spec="1.3.3" />
**<plugin name="phonegap-plugin-push" spec="2.1.3">
<variable name="FCM_VERSION" value="11.6.2" />
</plugin>
<engine name="android" spec="^7.0.0" />**
To add the specific 7.0.0 I used: cordova platform add [email protected] (note that if you want to get the push plugin to work with that specific version the supported push plugin must be 2.1.x).
And although I dont use GPS but still need the app to keep running even if the screen is off or batery save mode is enabled, so after I added this on my ionic 3/typescript project it worked as expected:
**this.backgroundMode.on('activate').subscribe(() => {
this.backgroundMode.disableWebViewOptimizations();
});**
Hope it helps someone.
I make a change in java code to work. See this #414
@EriveltonMS I tried your suggestion, but it did not work on either Android 8 nor 8.1 device :(
my app crashes with Android 8.1 but works at Android 8.0 disableWebViewOptimizations doesn't fix the pb. The cordova-plugin-background-mode has not been updated since 2years. in the forground pluggin start() function is undefined. i figured out that cordova.plugins.backgroundMode.enable() causes the app's crash. The solution is simply not to enable it at all as it appears Android OS 8.1 and 9.0 handles the background mode itself. At least the music plays in the background without any pluggin
@stsier What about when you run the app under lock screen, without charger plugged. How long does the music keep playing? In my situation, Android kills it off after about 3 minutes... (Android 8+)
@NorthFred it works great. I use "audio" html tag and listen to streaming music. With constant internet connection (mobile or wifi) i play music for hours. with connection disruptions music stops and it restarts if the disruption is short (1-2 minutes). My app doesn't do other background tasks so I can't say more. Tested on (iphone ios10 and android 8.0 with background mode plugin) and android 8.1 (without plugin)
@stsier You mentioned in previous comment that "The solution is simply not to enable it", how do you trigger the background audio? Can you elaborate how you implemented it in the code?
add an <audio>
tag with a source pointing to a streaming audio and call audio.play();
anywhere in the code. I didn't try with a local audio file.
on an Android8.0 and iphone you need to do cordova.plugins.backgroundMode.enable();
if you want to play music when your app is in the background. As I explained earlier, this causes an Android 8.1 and 9.0 crash, so I just check the device's OS version and don't call this on >8.0 devices.
@panchazo Thanks for your comments. I have made the same configurations as yours. But my app only runs about 3 minutes in background mode if the device is not plugged in power cable. I would like to know whether your app work well on background mode even though without device plugged in?
looks like android uses battery saving. You need to remove your app from the list of monitored apps in the battery settings.
@n1705771 at least up to Android 8 in the way that I posted my app works. BUT on my Xiaomi mi a1 with Android 9 it is too aggressive to close background running apps and it will close it. In other Android 9 distros it works however if once you install the app you allow it to access Internet and data without restrictions and in background and also you must take it out from the battery optimization list... Just like @stsier commented previously. If I find a better way I'll share it, for the time being that's all I have...
If you look at issue 400, I used a wake lock solution to get past the background data issue. This works with the phone off charger...
Cordova 9 will not work with your plugin. Using 8 it does work as long as I keep android target API at 26. If I switch the target API to 28, even using your most recent version, it fails as soon as I use the camera from the App. I am hopeful that by August someone will fix the background plugin so it does not fail with API target of 28.
add an
<audio>
tag with a source pointing to a streaming audio and callaudio.play();
anywhere in the code. I didn't try with a local audio file. on an Android8.0 and iphone you need to docordova.plugins.backgroundMode.enable();
if you want to play music when your app is in the background. As I explained earlier, this causes an Android 8.1 and 9.0 crash, so I just check the device's OS version and don't call this on >8.0 devices.
Just wanted to reply here and say that this was indeed the issue. I only run the enable code on android versions below 8.1 and the crash has stopped, and I can confirm that the app does still run in the background.
My code: // Enable Background Mode - Issue causing crash on Android version > 8 so we check for that function getAndroidVersion(ua) { ua = (ua || navigator.userAgent).toLowerCase(); var match = ua.match(/android\s([0-9.]*)/i); return match ? match[1] : undefined; }
var androidvers = 0;
androidvers = parseFloat(getAndroidVersion()); //4.2
if (androidvers === undefined || androidvers < 8.1) {
cordova.plugins.backgroundMode.setEnabled(true);
}