androidandroid-notificationsandroid-8.0-oreonotificationmanager

Notifications are not shown on Android 8 and 9


I am developing an Android app that includes notifications. I have tried the new Android 8 notification code:

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, "Channel"+notificationId)
                .setSmallIcon(R.drawable.ic_notification_logo)
                .setContentTitle(notification.getTitle())
                .setContentText(notification.getBody())
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setContentIntent(pendingIntent)
                .setChannelId("Channel"+notificationId);

NotificationManager notificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        String CHANNEL_ID = "Channel" + notificationId;// The id of the channel.
        CharSequence name = "MyApp";// The user-visible name of the channel.
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
        notificationManager.createNotificationChannel(mChannel);
    }

    notificationManager.notify(notificationId, notification);

But still doesn't work and no notifications are shown neither on 8 nor 9.


Solution

  • I believe you need to replace

    notificationManager.notify(notificationId, notification);
    

    with

    notificationManager.notify(notificationId, notificationBuilder.build());
    

    to make it work.