Android官方提供了NotificationCompat兼容类来帮助开发实现体验统一的Notification。
示例代码:
String text = "这里是内容";
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
Intent intent = new Intent(MainActivity.this,MainActivity.this.getClass());
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivit(MainActivity.this,100,intent,PendingIntent.FLAG_ONE_SHOT);
builder.setTicker("有一条通知")//设置在第一个通知到达时显示在状态栏的文本
.setSmallIcon(R.mipmap.ic_launcher)//设置小图标
.setContentTitle("内容的标题")//设置内容标题
.setStyle(new NotificationCompat.BigPictureStyle())//设置显示的样式
.setContentText(text)//设置显示的内容
.setNumber(100)//在通知的右侧设置一个数字
.setOngoing(false)//设置是否是一个可持续的通知
.setContentInfo("dsada")//设置右侧显示的文本,
.setAutoCancel(true)//设置此标志将使它以便当用户点击它在面板中的通知被自动取消
.setContentIntent(pendingIntent)
.setColor(Color.BLUE);
Notification notification = builder.build();
NotificationManagerCompat.from(MainActivity.this).notify(0,notification);