NotificationChannel issue in Android O

China☆狼群 提交于 2019-11-30 07:52:13
Milad Moosavi

As it is written in Android Documentation:

https://developer.android.com/preview/features/notification-channels.html

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.
mChannel.setDescription(description);

mChannel.enableLights(true);
// Sets the notification light color for notifications posted to this
// channel, if the device supports this feature.
mChannel.setLightColor(Color.RED);

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

mNotificationManager.createNotificationChannel(mChannel);

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.")
    .setSmallIcon(R.drawable.ic_notify_status)
    .setChannelId(CHANNEL_ID)
    .build();

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

Update:

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

NotificationCompat.Builder mBuilder;
mBuilder = new NotificationCompat.Builder(getApplicationContext())
    .setSmallIcon(R.mipmap.ic_launcher_icon)
    .setContentTitle("Title")
    .setContentText("Text")
    .setOngoing(true)
    .setChannelId(id);

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

Messages on Toast and Logcat talk about you should be paid attention to 2 items and them order:

  1. NotificationChannel mChannel = new NotificationChannel(id, name, importance);
  2. builder = new NotificationCompat.Builder(this, id);

Also NotificationManager notifManager and NotificationChannel mChannel are created only once.

There are required setters for Notification:

builder.setContentTitle() // required  
       .setSmallIcon()    // required 
       .setContentText()  // required  

See the example in On Android 8.1 API 27 notification does not display.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!