Why is Android 8 complaining about Bad notification for startForeground

-1

PLEASE HOLD ON, THE PROBLEM SEEMS TO BE ELSEWHERE . . .


The following code is generating the error on stock android 8.1:

if (Build.VERSION.SDK_INT >= 26) {

    String              channelId  = "com.myapp.notification.channel";
    String              channelName = "Myapp Background Service";
    NotificationChannel channel     = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
//            channel.setLightColor(Color.BLUE);
//            channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
//            channel.setSound(null, null);  //disable the sound

    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    manager.createNotificationChannel(channel);


    Notification notification = new NotificationCompat.Builder(context, channelId)
            .setOngoing(true)
            .setContentTitle("Myapp")
            .setContentText("Myapp")
            .setSmallIcon(R.drawable.ic_notif_icon)
            .setPriority(PRIORITY_HIGH)
            .setCategory(Notification.CATEGORY_SERVICE)
            .build();

    context.startForeground(14122, notification);
}

The error:

2018-12-14 17:17:39.387 24290-24290/com.myapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.myapp, PID: 24290
    android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1792)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6523)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:865)
android
notifications
asked on Stack Overflow Dec 14, 2018 by redochka • edited Dec 14, 2018 by redochka

2 Answers

0

the stack-trace reads channel=null ...which hints for not pushing the notification into a channel. my suspicion would be the channel ID com.myapp.notification.channel - while the default is DEFAULT_CHANNEL_ID (with underscores instead of dots). the documentation does not tell how a valid channel ID should look alike, while that's actually the only one difference I can spot there. the examples all use uppercase or lowercase characters, with underscores; the length of the string might also play a role. the channel IDs are even per package, therefore the name of the package in the channel ID is redundant information. this question also seems quite related.

answered on Stack Overflow Dec 14, 2018 by Martin Zeitler • edited Dec 14, 2018 by Martin Zeitler
0

The error was due to another code "pre android 8" that was creating notification without taking care of the channel.

Thank you from my heart for your help.

answered on Stack Overflow Dec 14, 2018 by redochka

User contributions licensed under CC BY-SA 3.0