question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Android 8.1 - invalid channel for service notification

See original GitHub issue

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)

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:9
  • Comments:20

github_iconTop GitHub Comments

15reactions
writer0713commented, Aug 7, 2018

@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
14reactions
Bharat-Rayasamcommented, Dec 23, 2018

@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

Read more comments on GitHub >

github_iconTop Results From Across the Web

invalid channel for service notification - android - Stack Overflow
Create Application Class Like this public class App extends Application { public static final String CHANNEL_ID = "exampleServiceChannel"; @ ...
Read more >
Android 8.1 crashes with "invalid channel for service notification"
One possible root cause is SdlRouterSErvice.java#L1073 as it's using a deprecated method to build the notification. Also reported in this ...
Read more >
Android – Bad notification for start Foreground, Invalid channel for ...
Android 8.0: java.lang.IllegalStateException: Not allowed to start service Intent. I got solution. For pre-8.0 devices, you have to just use startService() , ...
Read more >
android 8.1 invalid channel for service notification
You are creating a notification channel but never assigning it in your NotificationCompat.Builder: var notification = new NotificationCompat.Builder(context) ...
Read more >
invalid channel for service notification - Bountysource
Android O (8.1.0) : invalid channel for service notification.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found