生活随笔
收集整理的這篇文章主要介紹了
HarmonyOS之深入解析通知的使用
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、簡介
① 概念
通知提供應(yīng)用的即時(shí)消息或通信消息,用戶可以直接刪除或點(diǎn)擊通知觸發(fā)進(jìn)一步的操作。 通知目前支持六種樣式:普通文本、長文本、圖片、社交、多行文本和媒體樣式。創(chuàng)建通知時(shí)必須包含一種樣式。 通知支持快捷回復(fù)。
② 應(yīng)用場景
HarmonyOS 提供了通知功能,即在一個(gè)應(yīng)用的 UI 界面之外顯示的消息,主要用來提醒用戶有來自該應(yīng)用中的信息。當(dāng)應(yīng)用向系統(tǒng)發(fā)出通知時(shí),它將先以圖標(biāo)的形式顯示在通知欄中,用戶可以下拉通知欄查看通知的詳細(xì)信息。 常見的使用場景: 顯示應(yīng)用的推送消息,如廣告、版本更新等。 顯示當(dāng)前正在進(jìn)行的事件,如播放音樂、導(dǎo)航、下載等。
二、API 說明
通知相關(guān)基礎(chǔ)類包含 NotificationSlot、NotificationRequest和NotificationHelper。基礎(chǔ)類之間的關(guān)系如下所示:
① NotificationSlot
NotificationSlot 可以對提示音、振動(dòng)、鎖屏顯示和重要級別等進(jìn)行設(shè)置。一個(gè)應(yīng)用可以創(chuàng)建一個(gè)或多個(gè) NotificationSlot,在發(fā)布通知時(shí),通過綁定不同的 NotificationSlot,實(shí)現(xiàn)不同用途。 NotificationSlot 需要先通過 NotificationHelper 的 addNotificationSlot(NotificationSlot) 方法發(fā)布后,通知才能綁定使用;所有綁定該 NotificationSlot 的通知在發(fā)布后都具備相應(yīng)的特性,對象在創(chuàng)建后,將無法更改這些設(shè)置,對于是否啟動(dòng)相應(yīng)設(shè)置,用戶有最終控制權(quán)。 不指定 NotificationSlot 時(shí),當(dāng)前通知會(huì)使用默認(rèn)的 NotificationSlot,默認(rèn)的 NotificationSlot 優(yōu)先級為 LEVEL_DEFAULT。 NotificationSlot 主要接口:
接口名描述 NotificationSlot(String id, String name, int level) 構(gòu)造NotificationSlot setLevel(int level) 設(shè)置NotificationSlot的級別 setName(String name) 設(shè)置NotificationSlot的命名 setDescription(String description) 設(shè)置NotificationSlot的描述信息 enableBypassDnd(boolean bypassDnd) 設(shè)置是否繞過系統(tǒng)的免打擾模式 setEnableVibration(boolean vibration) 設(shè)置收到通知時(shí)是否使能振動(dòng) setLockscreenVisibleness(int visibleness) 設(shè)置在鎖屏場景下,收到通知后是否顯示,以及顯示的效果 setEnableLight(boolean isLightEnabled) 設(shè)置收到通知時(shí)是否開啟呼吸燈,前提是當(dāng)前硬件支持呼吸燈 setLedLightColor(int color) 設(shè)置收到通知時(shí)的呼吸燈顏色 setSlotGroup(String groupId) 綁定當(dāng)前NotificationSlot到一個(gè)NotificationSlot組
NotificationSlot 的級別目前支持如下幾種, 由低到高: LEVEL_MIN:表示通知可以發(fā)布,但是不顯示在通知欄,不自動(dòng)彈出,無提示音;該級別不適用于前臺(tái)服務(wù)的場景。 LEVEL_LOW:表示通知可以發(fā)布且顯示在通知欄,不自動(dòng)彈出,無提示音。 LEVEL_DEFAULT:表示通知發(fā)布后可在通知欄顯示,不自動(dòng)彈出,觸發(fā)提示音。 LEVEL_HIGH:表示通知發(fā)布后可在通知欄顯示,自動(dòng)彈出,觸發(fā)提示音。
② NotificationRequest
NotificationRequest 用于設(shè)置具體的通知對象,包括設(shè)置通知的屬性,如:通知的分發(fā)時(shí)間、小圖標(biāo)、大圖標(biāo)、自動(dòng)刪除等參數(shù),以及設(shè)置具體的通知類型,如普通文本、長文本等。 NotificationRequest 主要接口:
接口名描述 NotificationRequest() 構(gòu)建一個(gè)通知 NotificationRequest(int notificationId) 構(gòu)建一個(gè)通知,指定通知的id。通知的Id在應(yīng)用內(nèi)容具有唯一性,如果不指定,默認(rèn)為0 setNotificationId?(int notificationId) 設(shè)置當(dāng)前通知id setAutoDeletedTime?(long time) 設(shè)置通知自動(dòng)取消的時(shí)間戳 setContent?(NotificationRequest.NotificationContent content) 設(shè)置通知的具體內(nèi)容 setDeliveryTime?(long deliveryTime) 設(shè)置通知分發(fā)的時(shí)間戳 setSlotId(String slotId) 設(shè)置通知的NotificationSlot id setTapDismissed?(boolean tapDismissed) 設(shè)置通知在用戶點(diǎn)擊后是否自動(dòng)取消 setLittleIcon?(PixelMap smallIcon) 設(shè)置通知的小圖標(biāo),在通知左上角顯示 setBigIcon(PixelMap bigIcon) 設(shè)置通知的大圖標(biāo),在通知的右邊顯示 setGroupValue(String groupValue) 設(shè)置分組通知,相同分組的通知在通知欄顯示時(shí),將會(huì)折疊在一組應(yīng)用中顯示 addActionButton(NotificationActionButton actionButton) 設(shè)置通知添加通知ActionButton setIntentAgent(IntentAgent agent) 設(shè)置通知承載指定的IntentAgent,在通知中實(shí)現(xiàn)即將觸發(fā)的事件
具體的通知類型:目前支持六種類型,包括普通文本 NotificationNormalContent、長文本 NotificationLongTextContent、圖片 NotificationPictureContent、多行 NotificationMultiLineContent、社交 NotificationConversationalContent、媒體 NotificationMediaContent。 通知類型的主要接口:
類名接口名描述 NotificationNormalContent setTitle(String title) 設(shè)置通知標(biāo)題 NotificationNormalContent setText?(String text) 設(shè)置通知內(nèi)容 NotificationNormalContent setAdditionalText(String additionalText) 設(shè)置通知次要內(nèi)容,是對通知內(nèi)容的補(bǔ)充 NotificationPictureContent setBriefText(String briefText) 設(shè)置通知概要內(nèi)容,是對通知內(nèi)容的總結(jié) NotificationPictureContent setExpandedTitle(String expandedTitle) 設(shè)置附加圖片的通知展開時(shí)的標(biāo)題 NotificationPictureContent setBigPicture(PixelMap bigPicture) 設(shè)置通知的圖片內(nèi)容,附加在setText?(String text)下方 NotificationLongTextContent setLongText?(String longText) 設(shè)置通知的長文本 NotificationConversationalContent setConversationTitle(String conversationTitle) 設(shè)置社交通知的標(biāo)題 NotificationConversationalContent addConversationalMessage(ConversationalMessage message) 通知添加一條消息 NotificationMultiLineContent addSingleLine(String line) 在當(dāng)前通知中添加一行文本 NotificationMediaContent setAVToken(AVToken avToken) 將媒體通知綁定指定的AVToken NotificationMediaContent setShownActions(int[] actions) 設(shè)置媒體通知待展示的按鈕
通知發(fā)布后,通知的設(shè)置不可修改。如果下次發(fā)布通知使用相同的 id,就會(huì)更新之前發(fā)布的通知。
③ NotificationHelper
NotificationHelper 封裝了發(fā)布、更新、刪除通知等靜態(tài)方法。 NotificationHelper 主要接口:
接口名描述 publishNotification(NotificationRequest request) 發(fā)布一條通知 publishNotification(String tag, NotificationRequest request) 發(fā)布一條帶TAG的通知 cancelNotification(int notificationId) 取消指定的通知 cancelNotification(String tag, int notificationId) 取消指定的帶TAG的通知 cancelAllNotifications() 取消之前發(fā)布的所有通知 addNotificationSlot(NotificationSlot slot) 創(chuàng)建一個(gè)NotificationSlot getNotificationSlot(String slotId) 獲取NotificationSlot removeNotificationSlot(String slotId) 刪除一個(gè)NotificationSlot getActiveNotifications() 獲取當(dāng)前應(yīng)用發(fā)的活躍通知 getActiveNotificationNums() 獲取系統(tǒng)中當(dāng)前應(yīng)用發(fā)的活躍通知的數(shù)量 setNotificationBadgeNum(int num) 設(shè)置通知的角標(biāo) setNotificationBadgeNum() 設(shè)置當(dāng)前應(yīng)用中活躍狀態(tài)通知的數(shù)量在角標(biāo)顯示
三、通知的使用
① 創(chuàng)建 NotificationSlot
NotificationSlot 可以設(shè)置公共通知的震動(dòng),鎖屏模式,重要級別等,并通過調(diào)用 NotificationHelper.addNotificationSlot() 發(fā)布 NotificationSlot 對象。
NotificationSlot slot
= new
NotificationSlot ( "slot_001" , "slot_default" , NotificationSlot
. LEVEL_MIN
) ; slot
. setDescription ( "NotificationSlotDescription" ) ; slot
. setEnableVibration ( true
) ; slot
. setLockscreenVisibleness ( NotificationRequest
. VISIBLENESS_TYPE_PUBLIC
) ; slot
. setEnableLight ( true
) ; slot
. setLedLightColor ( Color
. RED
. getValue ( ) ) ; try
{ NotificationHelper
. addNotificationSlot ( slot
) ; } catch ( RemoteException ex
) { HiLog
. error ( LABEL
, "Exception occurred during addNotificationSlot invocation." ) ; }
② 發(fā)布通知
構(gòu)建 NotificationRequest 對象,應(yīng)用發(fā)布通知前,通過 NotificationRequest 的 setSlotId() 方法與 NotificationSlot 綁定,使該通知在發(fā)布后都具備該對象的特征。
int notificationId
= 1 ; NotificationRequest request
= new
NotificationRequest ( notificationId
) ; request
. setSlotId ( slot
. getId ( ) ) ;
調(diào)用 setContent() 設(shè)置通知的內(nèi)容:
String title
= "title" ; String text
= "There is a normal notification content." ; NotificationNormalContent content
= new
NotificationNormalContent ( ) ; content
. setTitle ( title
) . setText ( text
) ; NotificationRequest
. NotificationContent notificationContent
= new NotificationRequest
. NotificationContent ( content
) ; request
. setContent ( notificationContent
) ;
調(diào)用 publishNotification() 發(fā)布通知:
try
{ NotificationHelper
. publishNotification ( request
) ; } catch ( RemoteException ex
) { HiLog
. error ( LABEL
, "Exception occurred during publishNotification invocation." ) ; }
③ 取消通知
取消通知分為取消指定單條通知和取消所有通知,應(yīng)用只能取消自己發(fā)布的通知。 調(diào)用 cancelNotification() 取消指定的單條通知。
int notificationId
= 1 ; try
{ NotificationHelper
. cancelNotification ( notificationId
) ; } catch ( RemoteException ex
) { HiLog
. error ( LABEL
, "Exception occurred during cancelNotification invocation." ) ; }
調(diào)用 cancelAllNotifications() 取消所有通知:
try
{ NotificationHelper
. cancelAllNotifications ( ) ; } catch ( RemoteException ex
) { HiLog
. error ( LABEL
, "Exception occurred during cancelAllNotifications invocation." ) ; }
總結(jié)
以上是生活随笔 為你收集整理的HarmonyOS之深入解析通知的使用 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。