这是一个基于 Android 10 源码,全面分析 Android通知系统实现原理 的系列,这是第一篇,全系列将覆盖:java
本篇关注通知的简单使用(发送、更新、删除)和经常使用接口及其意义,同时会解释两个容易混淆的接口的区别,setSmallIcon 和 setLargeIcon
,以及发送通知时传入的tag、id
参数的用途。android
附上 Google Notification 官方文档,包含了各类类型通知的 使用说明 和 使用注意项,本系列是剖析 Android通知系统实现原理,不会在使用上作过多说明。bash
来看看如何发送一条通知:app
private void sendNoti(){
final String CHANNEL_ID = "demo_channel_id";
final String CHANNEL_NAME = "demo_channel_name";
final String CHANNEL_DESCRIPTION = "demo_channel_des";
final CharSequence title = "title";
final CharSequence content = "content";
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// 步骤1:告诉 Notification.Builder 咱们须要的通知内容
final Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContentTitle(title)
.setContentText(content)
.setWhen(System.currentTimeMillis())
.setContentIntent(contentPendingIntent)
.setDeleteIntent(deletePendingIntent)
.addAction(action);
// 步骤2:Android 8.0 新增了 NotificationChannel,这里须要作版本区分
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
mChannel.setDescription(CHANNEL_DESCRIPTION);
notificationManager.createNotificationChannel(mChannel);
builder.setChannelId(CHANNEL_ID);
}
// 步骤3:拿到咱们前面让 Notification.Builder 去帮忙构建的通知
Notification notification = builder.build();
// 步骤4:调用 NotificationManager.notify() 发送通知
notificationManager.notify(notiTag, notiId, notification);
}
复制代码
总结一下 通知的使用流程 就是:ide
Notification.Builder
构建通知Channel
Notification.Builder.build();
获取通知NotificationManager.notify()
发送通知能够看到使用是十分简单的,下面对这几个步骤展开说明一下:函数
1.Notification 类基于 建造者模式,提供了一个静态内部类 Builder,以方便咱们设置将要生成的通知的各类属性,下面解释下经常使用的方法:post
这里强调下 setSmallIcon() 与 setLargeIcon() 的区别:ui
2.下面看看通知的发送、更新、删除等操做:this
2.1 通知发送:spa
通知发送的入口是:NotificationManager.notify() / notifyAsUser()
函数,其中notify()
有两个重载方法:
/*frameworks/base/core/java/android/app/NotificationManager.java*/
public void notify(int id, Notification notification)
{
notify(null, id, notification);
}
public void notify(String tag, int id, Notification notification)
{
notifyAsUser(tag, id, notification, mContext.getUser());
}
public void notifyAsUser(String tag, int id, Notification notification, UserHandle user) {......}
复制代码
最终调用的都是notifyAsUser(String tag, int id, Notification notification, UserHandle user)
这个方法,能够看到 id 和 notification 是最基本的参数,不可为空,tag 和 user 可为空,当咱们不传入这两个参数时,系统会默认补充 tag = null 和 user = Context.getUser(),先说明下这几个参数:
2.2 通知更新:
有些时候咱们发送完通知后,须要对通知的内容进行更新,例如替换图标资源、文字资源等,这个时候就须要对通知进行更新,通知的更新调用的也是 notify
方法,假定咱们前面已经经过 notify(tag, id, notification)
发送了一条通知,如今咱们但愿更新该通知,那么咱们能够从新构建一个 Notification 对象,而后再次调用 notify(tag, id, notification)
,传入同样的tag、id
,便能实现更新通知的目的了,常见的场景包括 下载中的通知,音乐播放器的通知 等。
2.3 通知删除:
NotificationManager
提供了多个删除通知的接口:
/*frameworks/base/core/java/android/app/NotificationManager.java*/
public void cancel(int id)
{
cancel(null, id);
}
public void cancel(String tag, int id)
{
cancelAsUser(tag, id, mContext.getUser());
}
public void cancelAsUser(String tag, int id, UserHandle user) {......}
public void cancelAll() {......}
复制代码
能够看到,删除通知依旧须要用到咱们前面将的 tag、id
,这个很好理解,就很少解释了,注意 NotificationManager
还提供了一个 cancelAll()
接口让咱们删除某应用的所有通知。