private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
//不同的重要程度会影响通知显示的方式
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
上述代码应该在应用启动时立即执行,可以放在 Application 中进行初始化。 ; K5 g: j6 E) b$ c) k- [, q" c% L三、设置通知栏的点击操作 ) z' E. D. y% C [7 y z7 O. j1 ] 一般点击通知栏会打开对应的 Activity 界面,具体代码如下:9 ?5 @/ r. V/ H" { u" C. R& e$ g