NotificationManager manager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = null;
//第二步:实例化通知栏构造器NotificationCompat.Builder:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//判断API
NotificationChannel mChannel = new NotificationChannel(id, name,
NotificationManager.IMPORTANCE_HIGH);
manager.createNotificationChannel(mChannel);
notification = new NotificationCompat.Builder(this, id)
.setContentTitle("这是一个内容标题11")//设置通知栏标题
.setContentText("10") //设置通知栏显示内容
.setWhen(System.currentTimeMillis())//通知产生的时间。
// 会在通知信息里显示,通常是系统获取到的时间
.setSmallIcon(R.drawable.b)//设置通知小ICON
.build();
} else {
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, id)
.setContentTitle("这是一个内容标题")
.setContentText("1")
.setSmallIcon(R.drawable.a)
.setLargeIcon(BitmapFactory.decodeResource(getResources()
, R.mipmap.ic_launcher));
// .setOngoing(true);
notification = notificationBuilder.build();
}
//第三步:对Builder进行配置:
manager.notify(1, notification);