Android通知Notification
首先获取系统服务NotificationManager:
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//或
NotificationManager notificationManager = (NotificationManager) getSystemService(NotificationManager.class)
//或AndroidX库中的兼容NotificationManager
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);1.1、通知渠道
其次,对于Andrid 8.0 O以上的版本需要添加通知渠道(NotificationChannel)才能正常显示通知,创建一个NotificationChannel对象,并将其添加到NotificationManager中。后面创建Notification通知的时候需要指定通知渠道ID。
NotificationChannel channel = new NotificationChannel(CHANNELLID, "下载", NotificationManager.IMPORTANCE_DEFAULT);
channel.setSound(null, null);
channel.enableLights(true);
channel.setLightColor(Color.YELLOW);
channel.setShowBadge(true);
manager.createNotificationChannel(channel);1.2、通知Builder
创建通知,需要借助Notification.Builder类,或者AndroidX库中的NotificationCompat.Builder兼容
Notification.Builder builder;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
builder = new Notification.Builder(this, CHANNELLID);//CHANNELLID一定要存在,否则通知无法发出
} else {
builder = new Notification.Builder(this);
}1.3、设置通知各种属性
创建好Notification.Builder对象,就可以设置通知的各种属性
setContentTitle(CharSequence):设置标题
setContentText(CharSequence):设置内容
setSubText(CharSequence):设置内容下面一小行的文字
setTicker(CharSequence):设置收到通知时在顶部显示的文字信息
setWhen(long):设置通知时间,一般设置的是收到通知时的System.currentTimeMillis()
setSmallIcon(int):设置右下角的小图标,在接收到通知的时候顶部也会显示这个小图标
setLargeIcon(Bitmap):设置左边的大图还可以进行一些高级的设置,设置通知提醒方式,铃声、震动、呼吸灯。
setDefaults(int):向通知添加声音、闪灯和振动效果的最简单、 使用默认(defaults)属性,可以组合多个属性
Notification.DEFAULT_VIBRATE(添加默认震动提醒);
Notification.DEFAULT_SOUND(添加默认声音提醒);
Notification.DEFAULT_LIGHTS(添加默认三色灯提醒)
Notification.DEFAULT_ALL(添加默认以上3种全部提醒)
setVibrate(long[]):设置振动方式,比如:
setVibrate(new long[] {0,300,500,700});延迟0ms,然后振动300ms,在延迟500ms, 接着再振动700ms
//设置三色灯,参数依次是:灯光颜色, 亮持续时间,暗的时间。需要为Notification设置flags为Notification.FLAG_SHOW_LIGHTS支持三色灯提醒
setLights(int argb, int onMs, int offMs):
setSound(Uri):设置接收到通知时的铃声,可以用系统的,也可以自己设置,例子如下:
setDefaults(Notification.DEFAULT_SOUND) //获取默认铃声
setSound(Uri.parse("file:///sdcard/xx/xx.mp3")) //获取自定义铃声
setSound(Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "5")) //获取Android多媒体库内的铃声给通知添加进度条,和某些设置互斥,不能同时设置:
setProgress(int,int,boolean):设置带进度条的通知 参数依次为:进度条最大数值,当前进度,进度是否不确定 如果为确定的进度条还有一个重要的设置PendingIntent,具体PendingIntent 的用法另写一篇详细的博客PendingIntent初识。
Intent intent = new Intent(this, Avtivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
setContentIntent(contentIntent );例子:
builder.setContentIntent(contentIntent)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setTicker("新消息")
.setProgress(100, 50, true)
.setWhen(System.currentTimeMillis())
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon_header))
.setContentTitle("标题")
.setContentText("内容")发送通知:最后通过Builder对象,直接构造一个Notification对象,借助NotificationManager进行通知的显示:
Notification notification = builder.build()
manager.notify(0, notification);1.4、取消通知
通知取消有很多种方法:
①可以在Notification.Builder中设置setAutoCancel(true)。
setAutoCancel(boolean):用户点击Notification点击面板后是否让通知取消(默认不取消)②Notification的flag设置为FLAG_AUTO_CANCEL
notification.flags |= Notification.FLAG_AUTO_CANCEL;③通过 NotificationManager 调用 cancel(int id) 方法清除指定 ID 的通知
通过 NotificationManager 调用 cancel(String tag, int id) 方法清除指定 TAG 和 ID 的通知
notificationManager.cancel(0);④通过 NotificationManager 调用 cancelAll() 方法清除所有该应用之前发送的通知
notificationManager.cancelAll();参考博客:

