cordova-plugin-foreground-service
cordova-plugin-foreground-service copied to clipboard
Foreground service does not work on Android 10
I have successfully implemented this plugin for Android 8 and Android 9 to let my Ionic app stream music while the phone is locked. The music keeps playing without interruptions.
Now users with Android 10 phones are complaining that the audio drops out after 5 minutes. I am able to confirm that on e.g. Nokia 6.3 and Samsung A20 with Android 10.
Is there a solution to make this plugin work on Android 10?
I added this line to my config.xml (under the tag <platform name="android">
) without success:
<edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/uses-permission" xmlns:android="http://schemas.android.com/apk/res/android"> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> </edit-config>
I have successfully implemented this plugin for Android 8 and Android 9 to let my Ionic app stream music while the phone is locked. The music keeps playing without interruptions.
Now users with Android 10 phones are complaining that the audio drops out after 5 minutes. I am able to confirm that on e.g. Nokia 6.3 and Samsung A20 with Android 10.
Is there a solution to make this plugin work on Android 10?
I added this line to my config.xml (under the tag
<platform name="android">
) without success:
<edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/uses-permission" xmlns:android="http://schemas.android.com/apk/res/android"> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> </edit-config>
Same issue! Have you found a solution?
@spinninghamster Even though I liked this plugin very much, the lack of support for Andorid 10 made me switch to this repo:
https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background/src/master/
It's been recently maintained and works perfectly on all devices that I have tested with.
Here is some example code for the mentioned plugin, with some small checks for device dependencies:
platform.ready().then( () => {
if (this.device.version) {
// Read the main android version to determine how to handle background / foreground tasks
this.androidVersion = parseInt(this.device.version.charAt(0));
};
(<any>window).cordova.plugins.backgroundMode.setDefaults({
title: 'Title for the notification',
text: 'Textual description for the notification',
subText: 'Some subtext for the notification',
icon: 'icon', // this will look for icon.png in platforms/android/res/drawable|mipmap
color: 'F14F4D',
resume: true,
hidden: false,
bigText: false,
channelName: 'my-app-notification',
channelDescription: 'App channel description',
showWhen: false,
visibility: 'private'
});
(<any>window).cordova.plugins.backgroundMode.on('activate', function() {
(<any>window).cordova.plugins.backgroundMode.disableWebViewOptimizations();
// I noticed you should remove Battery Optimizations for Android 8 & 9 for it to work.
if (this.androidVersion < 9) {
(<any>window).cordova.plugins.backgroundMode.disableBatteryOptimizations();
}
});
});
Is it possible you are accessing location while the service is running? The 5 minute timeout makes me doubt this is the specific issue here, but nevertheless for those that do have location in use on android 10 you need to declare the service type: https://developer.android.com/guide/components/foreground-services#types and will need to be targeting to build against API 29+.
Location service type as an example combined with the uses-permission
line @NorthFred had:
<platform name="android">
<config-file parent="/*" target="AndroidManifest.xml" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
</config-file>
<edit-config file="AndroidManifest.xml" mode="merge" target="/manifest/application/service[@android:name='com.davidbriglio.foreground.ForegroundService']" xmlns:android="http://schemas.android.com/apk/res/android">
<service android:foregroundServiceType="location" />
</edit-config>
...
</platform>
Will need camera and microphone here when Android 11 starts rolling.