Bad notification for start Foreground, Invalid channel for service notification:

20,216

Solution 1

You forgot to set channel id for the notification.

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) builder.setChannelId(youChannelID);

The if statement is optional and depends on your Min SDK level.

Solution 2

it appears that you in 8.1 must create your own notification channel.

 private void startForeground() {
    String channelId ="";
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        createNotificationChannel("my_service", "My Background Service");
    }else{
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), channelId);
        Notification notification = notificationBuilder.setOngoing(true)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setPriority(PRIORITY_MIN)
                .setCategory(Notification.CATEGORY_SERVICE)
                .build();
        startForeground(101, notification);
    }


}

and after

 @RequiresApi(Build.VERSION_CODES.O)
private String createNotificationChannel(String channelId ,String channelName){
    NotificationChannel chan = new NotificationChannel(channelId,
            channelName, NotificationManager.IMPORTANCE_NONE);
    chan.setLightColor(Color.BLUE);
    chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.createNotificationChannel(chan);
    return channelId;
}

Update: Also don't forget to add the foreground permission as required Android P:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
Share:
20,216
Fahid Nadeem
Author by

Fahid Nadeem

Updated on July 09, 2022

Comments

  • Fahid Nadeem
    Fahid Nadeem almost 2 years

    So, i have an app on store with 50000 users. Recently i updated my android studio and compiled SDK to 27. I made some changes to support android 8 changes including Notification changes(e.g. Notification should have unique channel). Tested this on Android One phone(Running android 8). And everything went smooth. After updating the app on playstore i see these crashes on android 8 devices(mostly Google Pixel phones)

    Fatal Exception: 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=0x40 color=0xffffcf00 actions=3 vis=PRIVATE)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1768)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loop(Looper.java:164)
       at android.app.ActivityThread.main(ActivityThread.java:6494)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
    

    I see 7 of these Errors on Crashlytics. Each has same logs and there are about 5000+ crashes for around 10 users. So, apprently app was crashing on loop.

    The problem i am having is these logs don't provide any log from where i can see the source in my app that is initiating this crash. Is there any way i can find out which class is throwing this crash? Any help?

    Also i have this code for notifications:

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    
                int importance = NotificationManager.IMPORTANCE_HIGH;
                NotificationChannel mChannel = notificationManager.getNotificationChannel(id);
                if (mChannel == null) {
                    mChannel = new NotificationChannel(id, name, importance);
                    mChannel.setDescription(description);
                    notificationManager.createNotificationChannel(mChannel);
                }
            }
    
    
    
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context,id);
                builder.setContentTitle(pushMessages.size() + context.getString(R.string.new_notification))
                        .setContentIntent(pendingIntent)
                        .setContentText(pushMessages.get(0))
                        .setStyle(inboxStyle)
                        .setGroup(GROUP_KEY_NOTIFICATIONS)
                        .setGroupSummary(true)
                        .setAutoCancel(true);
    
                if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    builder.setSmallIcon(R.drawable.notification_img)
                            .setColor(context.getResources().getColor(R.color.settings_yellow));
    
    
                } else {
                    builder.setSmallIcon(R.mipmap.ic_launcher);
                }
                summaryNotification = builder.build();
    notificationManager.notify(i, summaryNotification);
    
  • Mahir Özdin
    Mahir Özdin about 5 years
    All other solutions was creating about your own channel and method but noboddy said the DIFFRENCE IS you must set channel id if SDK >= O Thanks for the awesome answer
  • Mustafa Berkay Mutlu
    Mustafa Berkay Mutlu over 4 years
    No need to do an if check for the API level, from the Javadoc of setChannelId: No-op on versions prior to {@link android.os.Build.VERSION_CODES#O}