19

I'm trying to start foreground service as follow:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

    NotificationChannel chan = new NotificationChannel(
            getApplicationContext().getPackageName(),
            "My Foreground Service",
            NotificationManager.IMPORTANCE_LOW);
    chan.setLightColor(Color.BLUE);
    chan.setLockscreenVisibility(Notification.VISIBILITY_SECRET);

    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    assert manager != null;
    manager.createNotificationChannel(chan);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder( 
            this, "MyChannelId");
    Notification notification = notificationBuilder.setOngoing(true)
            .setSmallIcon(R.mipmap.my_icon)
            .setContentTitle("App is running on foreground")
            .setPriority(NotificationManager.IMPORTANCE_LOW)
            .setCategory(Notification.CATEGORY_SERVICE)
            .setChannelId("MyChannelId")
            .build();

    startForeground(1, notification);
}

This solution works for Android 8.0 Emulator. But I got the error below on Android 8.1, 9.0 and 10.0 Emulator.

android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=MyChannelId pri=2 contentView=null vibrate=null sound=null defaults=0x0 flags=0x42 color=0x00000000 category=service vis=PRIVATE)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1945)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7356)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

PS: I tried on a non-emulator Android 9.0 device and didn't get this error.

guipivoto
  • 18,327
  • 9
  • 60
  • 75
Perihan Mirkelam
  • 526
  • 1
  • 5
  • 18

3 Answers3

30

Issue is happening because you are creating a channel with an ID but sending the notification in a different ID:

// Here, you are using the package name as channel ID
NotificationChannel chan = new NotificationChannel(
              getApplicationContext().getPackageName(), 
              "My Foreground Service",
              NotificationManager.IMPORTANCE_LOW);

// But here, you are sending notifications to "MyChannelId"
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder( 
              this, "MyChannelId");

So, I believe you just need to change Channel ID to MyChannelID as follows:

NotificationChannel chan = new NotificationChannel(
              "MyChannelId",
              "My Foreground Service",
              NotificationManager.IMPORTANCE_LOW);
guipivoto
  • 18,327
  • 9
  • 60
  • 75
0

In my case I had created notification channel in MyApplication class but I did not register it in manifest file

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<application
    android:name=".MyApplication"
    android:allowBackup="true"
Makarand
  • 983
  • 9
  • 27
-1
   Intent intent = new Intent(mContext, BaseActivity.class);
    intent.putExtra("fragment",curapp.getSection());
    intent.putExtra("appmodel",curapp);

    PendingIntent pendingIntent = PendingIntent.getActivity(this.getApplicationContext(), 1, intent, PendingIntent.FLAG_IMMUTABLE);

    mNotificationManager = new NotificationCompat.Builder(mContext,curapp.getId().toString());
    notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    mNotificationManager.setContentTitle(curapp.getName())
            .setContentText(mContext.getString(R.string.preparing_for_download))
            .setSmallIcon(R.drawable.ic_white_logo)
            .setProgress(100, 0, true)
            .setContentIntent(pendingIntent)
            .setOngoing(true)
            .setAutoCancel(true);


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)  {
        NotificationChannel channel =
                new NotificationChannel("NOTIFICATION_ID", "Downloading..",
                        NotificationManager.IMPORTANCE_DEFAULT);
        channel.setLightColor(Color.BLUE);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        notificationManager.createNotificationChannel(channel);
        mNotificationManager.setChannelId("NOTIFICATION_ID");
    }

    Notification notification = mNotificationManager.build();
    startForeground(curapp.getId(), notification);