cordova-plugin-background-mode icon indicating copy to clipboard operation
cordova-plugin-background-mode copied to clipboard

Android 8.1 - invalid channel for service notification

Open maerlynflagg opened this issue 5 years ago • 20 comments

i'm develope ionic app and there i'm used this plugin.

after opening my app and the app is loaded, i called the function "backgroundmode.enable()". after a short timespan app stopped with the issue:

invalid channel for service notification

after removing the usage of this plugin the app isn't stopped and runs all the time. all fine.

so, i checked this. i have added the plugin usage again and the exception came back. i removed it again, exception is gone

this is the full stacktrace, which is shown in Logcat (Android Studio):

    android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=-2 contentView=null vibrate=null sound=null defaults=0x0 flags=0x42 color=0x00000000 vis=PRIVATE)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2105)
        at android.os.Handler.dispatchMessage(Handler.java:109)
        at android.os.Looper.loop(Looper.java:166)
        at android.app.ActivityThread.main(ActivityThread.java:7377)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:469)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:963)

maerlynflagg avatar Aug 01 '18 09:08 maerlynflagg

@maerlynflagg I also spent long time to solve this problem. I solved this problem with below github (fork version).

try below command.

$ ionic cordova platform rm android
$ ionic cordova platform rm ios
$ ionic cordova plugin add https://github.com/tushe/cordova-plugin-background-mode.git
$ npm install --save @ionic-native/background-mode

$ ionic cordova platform add android
$ ionic cordova platform add ios

writer0713 avatar Aug 07 '18 06:08 writer0713

Have similar issues with Android 8.1 however re-adding the platform didn't resolve it. On Android P (Pixel 2) I receive the following error: Issue: java.lang.RuntimeException: Unable to create service de.appplant.cordova.plugin.background.ForegroundService: java.lang.SecurityException: Permission Denial: startForeground from pid=17789, uid=10151 requires android.permission.FOREGROUND_SERVICE So if I add the permission android.permission.FOREGROUND_SERVICE I get the fatal exception: android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification

Looks like an update might be needed - maybe something like this? ([https://stackoverflow.com/questions/47531742/startforeground-fail-after-upgrade-to-android-8-1])

EDIT: thanks @writer0713 that version has the fixes, updating ForegroundService.java with the updates from that git fixed the issue!

f18nfz avatar Aug 16 '18 14:08 f18nfz

Dont works in Android >= 8

victorhugoweb avatar Sep 20 '18 14:09 victorhugoweb

Similar problem on android 8.1 (Xiaomi MI8 Lite)

44b1b1c8-37a7-442d-ae6a-479667618d31

EvPut avatar Nov 19 '18 17:11 EvPut

@maerlynflagg

@maerlynflagg I also spent long time to solve this problem. I solved this problem with below github (fork version).

try below command.

$ ionic cordova platform rm android
$ ionic cordova platform rm ios
$ ionic cordova plugin add https://github.com/tushe/cordova-plugin-background-mode.git
$ npm install --save @ionic-native/background-mode

$ ionic cordova platform add android
$ ionic cordova platform add ios

@writer0713 This fixed the crash issue, but background plugin stopped working :(

Gurjit-ONEBCG avatar Nov 27 '18 12:11 Gurjit-ONEBCG

@Gurjit-ONEBCG

Below changes in the cordova-plugin-background-mode worked for me. Crash issue is resolved as well as background plugin is working fine.

  1. In ForegroundService.java made below changes: a) Add below import statement: import android.app.NotificationChannel; b) Add below global variables:
public static final String NOTIFICATION_CHANNEL_ID_SERVICE = "de.appplant.cordova.plugin.background";
public static final String NOTIFICATION_CHANNEL_ID_INFO = "com.package.download_info";

c) Replace keepAwake() method with below code:

private void keepAwake() {
       JSONObject settings = BackgroundMode.getSettings();
       boolean isSilent    = settings.optBoolean("silent", false);
       if (!isSilent) {
           if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
               NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
               nm.createNotificationChannel(new NotificationChannel(NOTIFICATION_CHANNEL_ID_SERVICE, "App Service", NotificationManager.IMPORTANCE_DEFAULT));
               nm.createNotificationChannel(new NotificationChannel(NOTIFICATION_CHANNEL_ID_INFO, "Download Info", NotificationManager.IMPORTANCE_DEFAULT));
           } else {
               startForeground(NOTIFICATION_ID, makeNotification());
           }
       }

       PowerManager powerMgr = (PowerManager)
               getSystemService(POWER_SERVICE);
       wakeLock = powerMgr.newWakeLock(
               PowerManager.PARTIAL_WAKE_LOCK, "BackgroundMode");
       wakeLock.acquire();
   } 
  1. Add below in AndroidManifest.xml file: <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

  2. In code where I invoked background mode plugin, used disableWebViewOptimizations option on activate:

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

Alternately, you can try https://github.com/katzer/cordova-plugin-background-mode/pull/416 but do remember to add FOREGROUND_SERVICE permission in AndroidManifest

Bharat-Rayasam avatar Dec 23 '18 13:12 Bharat-Rayasam

@Gurjit-ONEBCG

Below changes in the cordova-plugin-background-mode worked for me. Crash issue is resolved as well as background plugin is working fine.

  1. In ForegroundService.java made below changes: a) Add below import statement: import android.app.NotificationChannel; b) Add below global variables:
public static final String NOTIFICATION_CHANNEL_ID_SERVICE = "de.appplant.cordova.plugin.background";
public static final String NOTIFICATION_CHANNEL_ID_INFO = "com.package.download_info";

c) Replace keepAwake() method with below code:

private void keepAwake() {
       JSONObject settings = BackgroundMode.getSettings();
       boolean isSilent    = settings.optBoolean("silent", false);
       if (!isSilent) {
           if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
               NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
               nm.createNotificationChannel(new NotificationChannel(NOTIFICATION_CHANNEL_ID_SERVICE, "App Service", NotificationManager.IMPORTANCE_DEFAULT));
               nm.createNotificationChannel(new NotificationChannel(NOTIFICATION_CHANNEL_ID_INFO, "Download Info", NotificationManager.IMPORTANCE_DEFAULT));
           } else {
               startForeground(NOTIFICATION_ID, makeNotification());
           }
       }

       PowerManager powerMgr = (PowerManager)
               getSystemService(POWER_SERVICE);
       wakeLock = powerMgr.newWakeLock(
               PowerManager.PARTIAL_WAKE_LOCK, "BackgroundMode");
       wakeLock.acquire();
   } 
  1. Add below in AndroidManifest.xml file: <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
  2. In code where I invoked background mode plugin, used disableWebViewOptimizations option on activate:
cordova.plugins.backgroundMode.on('activate', function() {
       cordova.plugins.backgroundMode.disableWebViewOptimizations();
 });

Alternately, you can try #416 but do remember to add FOREGROUND_SERVICE permission in AndroidManifest

thanks!!! Work Great!!! I have a question. do I need do the changes everytime I compile my phonegap/android platform?

brauliofreire avatar Jan 13 '19 20:01 brauliofreire

@brauliofreire is it still working fine on Android 8+ phones?

ziyaddin avatar Jan 25 '19 00:01 ziyaddin

@brauliofreire is it still working fine on Android 8+ phones?

yes! I made tests in android 8.1 and it is working fine!!

brauliofreire avatar Jan 25 '19 00:01 brauliofreire

@katzer FYI. I haven't tested @Bharat-Rayasam's solution yet but it can be useful for you to come up with stable solution.

ziyaddin avatar Jan 30 '19 10:01 ziyaddin

@Gurjit-ONEBCG

Below changes in the cordova-plugin-background-mode worked for me. Crash issue is resolved as well as background plugin is working fine.

  1. In ForegroundService.java made below changes: a) Add below import statement: import android.app.NotificationChannel; b) Add below global variables:
public static final String NOTIFICATION_CHANNEL_ID_SERVICE = "de.appplant.cordova.plugin.background";
public static final String NOTIFICATION_CHANNEL_ID_INFO = "com.package.download_info";

c) Replace keepAwake() method with below code:

private void keepAwake() {
       JSONObject settings = BackgroundMode.getSettings();
       boolean isSilent    = settings.optBoolean("silent", false);
       if (!isSilent) {
           if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
               NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
               nm.createNotificationChannel(new NotificationChannel(NOTIFICATION_CHANNEL_ID_SERVICE, "App Service", NotificationManager.IMPORTANCE_DEFAULT));
               nm.createNotificationChannel(new NotificationChannel(NOTIFICATION_CHANNEL_ID_INFO, "Download Info", NotificationManager.IMPORTANCE_DEFAULT));
           } else {
               startForeground(NOTIFICATION_ID, makeNotification());
           }
       }

       PowerManager powerMgr = (PowerManager)
               getSystemService(POWER_SERVICE);
       wakeLock = powerMgr.newWakeLock(
               PowerManager.PARTIAL_WAKE_LOCK, "BackgroundMode");
       wakeLock.acquire();
   } 
  1. Add below in AndroidManifest.xml file: <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
  2. In code where I invoked background mode plugin, used disableWebViewOptimizations option on activate:
cordova.plugins.backgroundMode.on('activate', function() {
       cordova.plugins.backgroundMode.disableWebViewOptimizations();
 });

Alternately, you can try #416 but do remember to add FOREGROUND_SERVICE permission in AndroidManifest

@Bharat-Rayasam Thanks for this great help :1st_place_medal: :100:

tajindersinghnamdhari avatar Feb 01 '19 23:02 tajindersinghnamdhari

I was able to solve it this way: https://github.com/irceline/aq-mobile-be/issues/94#issuecomment-464293865

arup-b avatar Feb 16 '19 05:02 arup-b

Any chance on this solution be added to the main repo code?

lkonzen-garupa avatar Feb 28 '19 10:02 lkonzen-garupa

@katzer any chance?

lkonzen-garupa avatar Mar 04 '19 11:03 lkonzen-garupa

I hope that this fix will be added to the main repo code

fdambrosio avatar Jun 16 '19 13:06 fdambrosio

@Gurjit-ONEBCG

Below changes in the cordova-plugin-background-mode worked for me. Crash issue is resolved as well as background plugin is working fine.

  1. In ForegroundService.java made below changes: a) Add below import statement: import android.app.NotificationChannel; b) Add below global variables:
public static final String NOTIFICATION_CHANNEL_ID_SERVICE = "de.appplant.cordova.plugin.background";
public static final String NOTIFICATION_CHANNEL_ID_INFO = "com.package.download_info";

c) Replace keepAwake() method with below code:

private void keepAwake() {
       JSONObject settings = BackgroundMode.getSettings();
       boolean isSilent    = settings.optBoolean("silent", false);
       if (!isSilent) {
           if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
               NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
               nm.createNotificationChannel(new NotificationChannel(NOTIFICATION_CHANNEL_ID_SERVICE, "App Service", NotificationManager.IMPORTANCE_DEFAULT));
               nm.createNotificationChannel(new NotificationChannel(NOTIFICATION_CHANNEL_ID_INFO, "Download Info", NotificationManager.IMPORTANCE_DEFAULT));
           } else {
               startForeground(NOTIFICATION_ID, makeNotification());
           }
       }

       PowerManager powerMgr = (PowerManager)
               getSystemService(POWER_SERVICE);
       wakeLock = powerMgr.newWakeLock(
               PowerManager.PARTIAL_WAKE_LOCK, "BackgroundMode");
       wakeLock.acquire();
   } 
  1. Add below in AndroidManifest.xml file: <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
  2. In code where I invoked background mode plugin, used disableWebViewOptimizations option on activate:
cordova.plugins.backgroundMode.on('activate', function() {
       cordova.plugins.backgroundMode.disableWebViewOptimizations();
 });

Alternately, you can try #416 but do remember to add FOREGROUND_SERVICE permission in AndroidManifest

@Bharat-Rayasam this was the best solution! My application now works like a charm! Thank you!

I had to make a slight change though. this.backgroundMode.on('activate').subscribe(() => this.backgroundMode.disableWebViewOptimizations());

thanveerahamed avatar Jun 26 '19 15:06 thanveerahamed

@Gurjit-ONEBCG

Below changes in the cordova-plugin-background-mode worked for me. Crash issue is resolved as well as background plugin is working fine.

  1. In ForegroundService.java made below changes: a) Add below import statement: import android.app.NotificationChannel; b) Add below global variables:
public static final String NOTIFICATION_CHANNEL_ID_SERVICE = "de.appplant.cordova.plugin.background";
public static final String NOTIFICATION_CHANNEL_ID_INFO = "com.package.download_info";

c) Replace keepAwake() method with below code:

private void keepAwake() {
       JSONObject settings = BackgroundMode.getSettings();
       boolean isSilent    = settings.optBoolean("silent", false);
       if (!isSilent) {
           if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
               NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
               nm.createNotificationChannel(new NotificationChannel(NOTIFICATION_CHANNEL_ID_SERVICE, "App Service", NotificationManager.IMPORTANCE_DEFAULT));
               nm.createNotificationChannel(new NotificationChannel(NOTIFICATION_CHANNEL_ID_INFO, "Download Info", NotificationManager.IMPORTANCE_DEFAULT));
           } else {
               startForeground(NOTIFICATION_ID, makeNotification());
           }
       }

       PowerManager powerMgr = (PowerManager)
               getSystemService(POWER_SERVICE);
       wakeLock = powerMgr.newWakeLock(
               PowerManager.PARTIAL_WAKE_LOCK, "BackgroundMode");
       wakeLock.acquire();
   } 
  1. Add below in AndroidManifest.xml file: <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
  2. In code where I invoked background mode plugin, used disableWebViewOptimizations option on activate:
cordova.plugins.backgroundMode.on('activate', function() {
       cordova.plugins.backgroundMode.disableWebViewOptimizations();
 });

Alternately, you can try #416 but do remember to add FOREGROUND_SERVICE permission in AndroidManifest

did you do a merge request? :D

ZumelzuR avatar Jan 13 '20 18:01 ZumelzuR

@Bharat-Rayasam :- sir I tried ur code but is working only in some Android devices and all devices are version 9.i tested in Oppo F11pro,nokia, Samsung,but it's working only in Oppo Device another device I got same error.now what I do now sir plz help me?

kapilSoni101 avatar Feb 22 '20 03:02 kapilSoni101

  1. ForegroundService.java

I couldn't find the .java file, could you tell me where to find it?

app2b avatar Jan 01 '21 22:01 app2b

Hi,

You could add FOREGROUND_SERVICE permission in your app once without updating AndroidManifest everytime you build the app.

Add those lines to your config.xml to grant the missing permission:

<config-file parent="./" target="app/src/main/AndroidManifest.xml" xmlns:android="http://schemas.android.com/apk/res/android"> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> </config-file>

karimslim avatar Jun 04 '21 09:06 karimslim