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 設置通知效果


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