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

Fahid Nadeem picture Fahid Nadeem · Jul 30, 2018 · Viewed 17.9k times · Source

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);

Answer

saikrishna279 picture saikrishna279 · Oct 21, 2018

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.