Notification not showing in Oreo Notification not showing in Oreo android android

Notification not showing in Oreo


In Android O it's a must to use a channel with your Notification Builder

below is a sample code :

// Sets an ID for the notification, so it can be updated.int notifyID = 1; String CHANNEL_ID = "my_channel_01";// The id of the channel. CharSequence name = getString(R.string.channel_name);// The user-visible name of the channel.int importance = NotificationManager.IMPORTANCE_HIGH;NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);// Create a notification and set the notification channel.Notification notification = new Notification.Builder(MainActivity.this)            .setContentTitle("New Message")            .setContentText("You've received new messages.")            .setSmallIcon(R.drawable.ic_notify_status)            .setChannelId(CHANNEL_ID)            .build();

Or with Handling compatibility by:

NotificationCompat notification =        new NotificationCompat.Builder(this)        .setSmallIcon(R.drawable.notification_icon)        .setContentTitle("My notification")        .setContentText("Hello World!")        .setChannelId(CHANNEL_ID).build();

Now make it notify

NotificationManager mNotificationManager =            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.createNotificationChannel(mChannel);// Issue the notification.mNotificationManager.notify(notifyID , notification);

or if you want a simple fix then use the following code:

NotificationManager mNotificationManager =            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {       mNotificationManager.createNotificationChannel(mChannel);    }

Updates:NotificationCompat.Builder reference

NotificationCompat.Builder(Context context)

This constructor was deprecated in API level 26.0.0so you should use

Builder(Context context, String channelId)

so no need to setChannelId with the new constructor.

And you should use the latest of AppCompat library currently 26.0.2

compile "com.android.support:appcompat-v7:26.0.+"

Source from Android Developers Channel on Youtube

Also, you could check official Android Docs


Here i post some quick solution function with intent handling

public void showNotification(Context context, String title, String body, Intent intent) {    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);    int notificationId = 1;    String channelId = "channel-01";    String channelName = "Channel Name";    int importance = NotificationManager.IMPORTANCE_HIGH;    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {        NotificationChannel mChannel = new NotificationChannel(                channelId, channelName, importance);        notificationManager.createNotificationChannel(mChannel);    }    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId)            .setSmallIcon(R.mipmap.ic_launcher)            .setContentTitle(title)            .setContentText(body);    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);    stackBuilder.addNextIntent(intent);    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(            0,            PendingIntent.FLAG_UPDATE_CURRENT    );    mBuilder.setContentIntent(resultPendingIntent);    notificationManager.notify(notificationId, mBuilder.build());}


In addition to this answer, you need to create the notification channel before it can be used.

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {      /* Create or update. */      NotificationChannel channel = new NotificationChannel("my_channel_01",          "Channel human readable title",           NotificationManager.IMPORTANCE_DEFAULT);      mNotificationManager.createNotificationChannel(channel);  }

Also you need to use channels only if your targetSdkVersion is 26 or higher.

If you are using NotificationCompat.Builder, you also need to update to the beta version of the support library: https://developer.android.com/topic/libraries/support-library/revisions.html#26-0-0-beta2 (to be able to call setChannelId on the compat builder).

Be careful as this library update raises minSdkLevel to 14.