一个 Notification 的必要属性有三项,如果不设置则在运行时会抛出异常:
1.小图标,通过 setSmallIcon() 方法设置
2.标题,通过 setContentTitle() 方法设置
3.内容,通过 setContentText() 方法设置
除了以上三项,其它均为可选项。虽然如此,但还是应该给 Notification 设置一个 Action ,这样就可以直接跳转到 App 的某个 Activity 、启动一个 Service 或者发送一个 Broadcast。否则,Notification 仅仅只能起到通知的效果,而不能与用户交互。
当系统接收到通知时,可以通过震动、响铃、呼吸灯等多种方式进行提醒。
Notification 的创建主要涉及到 Notification.Builder ,Notification , NotificationManager,NotificationChannel 。
1、Notification.Builer : 使用建造者模式构建 Notification 对象。由于 Notification.Builder 仅支持 Android 4.1及之后的版本,为了解决兼容性问题, Google 在 Android Support v4 中加入了 NotificationCompat.Builder 类。对于某些在 Android 4.1 之后才特性,即使 NotificationCompat.Builder 支持该方法,在之前的版本中也不能运行。点我 查看更多关于 Notification 兼容性问题处理。文中使用的都是 NotificationCompat。
2、Notification : 通知对应类,保存通知相关的数据。NotificationManager 向系统发送通知时会用到。
3、NotificationManager : NotificationManager 是通知管理类,它是一个系统服务。调用 NotificationManager 的 notify() 方法可以向系统发送通知。
4、NotificationChannel: 通知渠道,是android8.0新增的特性,以提供统一的系统来帮助用户管理通知,如果App的targetSDKVersion>=26,没有设置channel通知渠道的话,就会导致通知无法展示。
发送通知四部曲:
1.获取 NotificationManager 实例
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);1
1
NotificationManagermanager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
2.创建NotificationChannel,做相关配置
String channelId = "1";
String channelName = "default";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
channel.enableLights(true);//开启指示灯,如果设备有的话。
channel.setLightColor(Color.RED);//设置指示灯颜色
channel.setShowBadge(true);//检测是否显示角标
manager.createNotificationChannel(channel);
}9
1. StringchannelId="1";
2.StringchannelName="default";
3.if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {
4.NotificationChannelchannel=newNotificationChannel(channelId,channelName,NotificationManager.IMPORTANCE_DEFAULT);
5.channel.enableLights(true);//开启指示灯,如果设备有的话。
6.channel.setLightColor(Color.RED);//设置指示灯颜色
7.channel.setShowBadge(true);//检测是否显示角标
8.manager.createNotificationChannel(channel);
3.实例化 NotificationCompat.Builder 并设置相关属性,生成Notification
Intent intent = new Intent(this, MainActivity.class);
PendingIntent activity = PendingIntent.getActivity(this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification build = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.ic_launcher)//设置小图
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.banner))//设置大图
.setContentText("我是内容")//设置内容
.setContentTitle("我是标题")//设置标题
.setContentIntent(activity)//设置延时意图
.setDefaults(Notification.DEFAULT_ALL)//设置提示效果
.setBadgeIconType(R.mipmap.ic_launcher)//设置设置角标样式
.setAutoCancel(true)//设置点击自动消失
.setNumber(3)//设置角标计数
.build();x
1.Intentintent=newIntent(this,MainActivity.class);
2.PendingIntentactivity=PendingIntent.getActivity(this,100,intent,PendingIntent.FLAG_UPDATE_CURRENT);
3
4.Notificationbuild=newNotificationCompat.Builder(this,channelId)
5
.setSmallIcon(R.mipmap.ic_launcher)//设置小图
6
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.banner))//设置大图
7
.setContentText("我是内容")//设置内容
8
.setContentTitle("我是标题")//设置标题
9
.setContentIntent(activity)//设置延时意图
10
.setDefaults(Notification.DEFAULT_ALL)//设置提示效果
11
.setBadgeIconType(R.mipmap.ic_launcher)//设置设置角标样式
12
.setAutoCancel(true)//设置点击自动消失
13
.setNumber(3)//设置角标计数
14
.build();
4.调用NotificationManager 方法notify()发送通知
manager.notify(100, build);
1
manager.notify(100,build);
相比发送最简单的通知,发送具有 Action 的通知多了创建 Intent 、 PendingIntent 和 setContentIntent() 这几步。
不难看出, PendingIntent 才是重点,那么, PendingIntent 是什么呢?
PendingIntent(延时意图)
PendingIntent 是一种特殊的 Intent ,字面意思可以解释为延迟的 Intent ,用于在某个事件结束后执行特定的 Action 。从上面带 Action 的通知也能验证这一点,当用户点击通知时,才会执行。
PendingIntent 是 Android 系统管理并持有的用于描述和获取原始数据的对象的标志(引用)。也就是说,即便创建该PendingIntent对象的进程被杀死了,这个PendingItent对象在其他进程中还是可用的。
日常使用中的短信、闹钟等都用到了 PendingIntent。
PendingIntent 主要可以通过以下三种方式获取:
PendingIntent 具有以下几种 flag:
FLAG_CANCEL_CURRENT:如果当前系统中已经存在一个相同的 PendingIntent 对象,那么就将先将已有的 PendingIntent 取消,然后重新生成一个 PendingIntent 对象。
FLAG_NO_CREATE:如果当前系统中不存在相同的 PendingIntent 对象,系统将不会创建该 PendingIntent 对象而是直接返回 null 。
FLAG_ONE_SHOT:该 PendingIntent 只作用一次。
FLAG_UPDATE_CURRENT:如果系统中已存在该 PendingIntent 对象,那么系统将保留该 PendingIntent 对象,但是会使用新的 Intent 来更新之前 PendingIntent 中的 Intent 对象数据,例如更新 Intent 中的 Extras 。
取消通知有如下 5 种方式:
1.点击通知栏的清除按钮,会清除所有可清除的通知
2.设置了 setAutoCancel() 或 FLAG_AUTO_CANCEL 的通知,点击该通知时会清除它
3.通过 NotificationManager 调用 cancel(int id) 方法清除指定 ID 的通知
4.通过 NotificationManager 调用 cancel(String tag, int id) 方法清除指定 TAG 和 ID 的通知
5.通过 NotificationManager 调用 cancelAll() 方法清除所有该应用之前发送的通知
如果你是通过 NotificationManager.notify(String tag, int id, Notification notify) 方法创建的通知,那么只能通过 NotificationManager.cancel(String tag, int id) 方法才能清除对应的通知,调用NotificationManager.cancel(int id) 无效。
通知效果(了解)
除了以上几种设置 Notification 默认通知效果,还可以通过以下几种 FLAG 设置通知效果。