Android通知(Notification)的使用

1.通知的概述

Notification,是一种具有全局效果的通知,可以在系统的通知栏中显示。当 APP 向系统发出通知时,它将先以图标的形式显示在通知栏中。用户可以下拉通知栏查看通知的详细信息。通知栏和抽屉式通知栏均是由系统控制,用户可以随时查看。下面两张图均是来自 Google 官方文档。



1.2通知的使用场景


通知的目的是告知用户 App 事件。在平时的使用中,通知主要有以下几个作用:

1.显示接收到短消息、及时消息等信息(如QQ、微信、新浪、短信)

2.显示客户端的推送消息,如广告、优惠、版本更新、推荐新闻等,常用的第三方 SDK 有: JPush    信鸽  网易云信(偏重 IM )  阿里云推送

3.显示正在进行的事物,例如:后台运行的程序,如音乐播放进度、下载进度等

其中,前两点可以归结为与用户交互,第三点是实时的任务提醒,但不可否认的是,第三点也会与用户交互。

2.通知的使用

Notification 的基本操作主要有创建、更新、取消这三种。一个 Notification 的必要属性有三项,如果不设置则在运行时会抛出异常:
1.小图标,通过 setSmallIcon() 方法设置
2.标题,通过 setContentTitle() 方法设置
3.内容,通过 setContentText() 方法设置
除了以上三项,其它均为可选项。虽然如此,但还是应该给 Notification 设置一个 Action ,这样就可以直接跳转到 App 的某个 Activity 、启动一个 Service 或者发送一个 Broadcast。否则,Notification 仅仅只能起到通知的效果,而不能与用户交互。
当系统接收到通知时,可以通过震动、响铃、呼吸灯等多种方式进行提醒。


3.创建 Notification

Notification 的创建主要涉及到 Notification.Builder 、 Notification 、 NotificationManager 。
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() 方法可以向系统发送通知。
获取 NotificationManager 对象:

前面讲到,Notification 有三个必要属性。下面,我们就来创建一个简单的 Notification 。主要有以下三步:
1.获取 NotificationManager 实例
2.实例化 NotificationCompat.Builder 并设置相关属性

3.通过 builder.build() 方法生成 Notification 对象,并发送通知




前面讲到,Notification 有三个必要属性。下面,我们就来创建一个简单的 Notification 。主要有以下三步:
1.获取 NotificationManager 实例
2.实例化 NotificationCompat.Builder 并设置相关属性

3.通过 builder.build() 方法生成 Notification 对象,并发送通知


Google 官方是这么解释 setSmallIcon() 这个方法的:

Set the small icon resource, which will be used to represent the notification in the status bar. The platform template for the expanded view will draw this icon in the left, unless a large icon has also been specified, in which case the small icon will be moved to the right-hand side.


4.给 Notification 设置 Action

在前 创建 Notification 中发送的通知并不具备与用户交互的能力,这是因为我们并没有给 Notification 设置 Action 。在这里,我们就来讲讲如何给 Notification 设置 Action 。这里,我们来实现一个点击 Notification 跳转到 MainActivity 的效果。代码如下:


相比发送最简单的通知,发送具有 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.更新 Notification

更新通知很简单,只需要再次发送相同 ID 的通知即可,如果之前的通知还未被取消,则会直接更新该通知相关的属性;如果之前的通知已经被取消,则会重新创建一个新通知。

更新通知跟发送通知使用相同的方式。

6.取消 Notification

取消通知有如下 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) 无效。

7.设置 Notification 的通知效果

前面讲了 Notification 的创建、更新和取消,以及给 Notification 设置 Action 等基本操作。那么,我怎么给 Notification 设置诸如震动、铃声、呼吸灯等效果呢?别急,接下来马上就会告诉你怎么给 Notification 添加效果。

Notification 有震动、响铃、呼吸灯三种响铃效果,可以通过 setDefaults(int defualts) 方法来设置。 



除了以上几种设置 Notification 默认通知效果,还可以通过以下几种 FLAG 设置通知效果


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章