NotificationChannel issue in Android O

As it is written in Android Documentation:

If you target Android O and post a notification without specifying a valid notifications channel, the notification fails to post and the system logs an error.

To solve this issue you need to create a NotificationChannel.

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// The id of the channel.
String id = "my_channel_01";

// The user-visible name of the channel.
CharSequence name = getString(R.string.channel_name);

// The user-visible description of the channel.
String description = getString(R.string.channel_description);

int importance = NotificationManager.IMPORTANCE_LOW;

NotificationChannel mChannel = new NotificationChannel(id, name,importance);

// Configure the notification channel.

// Sets the notification light color for notifications posted to this
// channel, if the device supports this feature.

mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});


And then assign it to your Notification like this:

mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

// Sets an ID for the notification, so it can be updated.
int notifyID = 1;

// The id of the channel.
String CHANNEL_ID = "my_channel_01";

// Create a notification and set the notification channel.
Notification notification = new Notification.Builder(MainActivity.this)
    .setContentTitle("New Message")
    .setContentText("You've received new messages.")

// Issue the notification.
mNotificationManager.notify(id, notification);


In case you want to use NotificationCompat here is a simple example:

NotificationCompat.Builder mBuilder;
mBuilder = new NotificationCompat.Builder(getApplicationContext())

In fact you have to use Notification Builder in order to set channel id via setChannelId();

Leave a Comment