日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HarmonyOS之深入解析通知的使用

發布時間:2024/5/21 编程问答 55 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HarmonyOS之深入解析通知的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、簡介

① 概念
  • 通知提供應用的即時消息或通信消息,用戶可以直接刪除或點擊通知觸發進一步的操作。
  • 通知目前支持六種樣式:普通文本、長文本、圖片、社交、多行文本和媒體樣式。創建通知時必須包含一種樣式。
  • 通知支持快捷回復。
② 應用場景
  • HarmonyOS 提供了通知功能,即在一個應用的 UI 界面之外顯示的消息,主要用來提醒用戶有來自該應用中的信息。當應用向系統發出通知時,它將先以圖標的形式顯示在通知欄中,用戶可以下拉通知欄查看通知的詳細信息。
  • 常見的使用場景:
    • 顯示接收到短消息、即時消息等。
    • 顯示應用的推送消息,如廣告、版本更新等。
    • 顯示當前正在進行的事件,如播放音樂、導航、下載等。

二、API 說明

  • 通知相關基礎類包含 NotificationSlot、NotificationRequest和NotificationHelper。基礎類之間的關系如下所示:

① NotificationSlot
  • NotificationSlot 可以對提示音、振動、鎖屏顯示和重要級別等進行設置。一個應用可以創建一個或多個 NotificationSlot,在發布通知時,通過綁定不同的 NotificationSlot,實現不同用途。
  • NotificationSlot 需要先通過 NotificationHelper 的 addNotificationSlot(NotificationSlot) 方法發布后,通知才能綁定使用;所有綁定該 NotificationSlot 的通知在發布后都具備相應的特性,對象在創建后,將無法更改這些設置,對于是否啟動相應設置,用戶有最終控制權。
  • 不指定 NotificationSlot 時,當前通知會使用默認的 NotificationSlot,默認的 NotificationSlot 優先級為 LEVEL_DEFAULT。
  • NotificationSlot 主要接口:
接口名描述
NotificationSlot(String id, String name, int level)構造NotificationSlot
setLevel(int level)設置NotificationSlot的級別
setName(String name)設置NotificationSlot的命名
setDescription(String description)設置NotificationSlot的描述信息
enableBypassDnd(boolean bypassDnd)設置是否繞過系統的免打擾模式
setEnableVibration(boolean vibration)設置收到通知時是否使能振動
setLockscreenVisibleness(int visibleness)設置在鎖屏場景下,收到通知后是否顯示,以及顯示的效果
setEnableLight(boolean isLightEnabled)設置收到通知時是否開啟呼吸燈,前提是當前硬件支持呼吸燈
setLedLightColor(int color)設置收到通知時的呼吸燈顏色
setSlotGroup(String groupId)綁定當前NotificationSlot到一個NotificationSlot組
  • NotificationSlot 的級別目前支持如下幾種, 由低到高:
    • LEVEL_NONE: 表示通知不發布。
    • LEVEL_MIN:表示通知可以發布,但是不顯示在通知欄,不自動彈出,無提示音;該級別不適用于前臺服務的場景。
    • LEVEL_LOW:表示通知可以發布且顯示在通知欄,不自動彈出,無提示音。
    • LEVEL_DEFAULT:表示通知發布后可在通知欄顯示,不自動彈出,觸發提示音。
    • LEVEL_HIGH:表示通知發布后可在通知欄顯示,自動彈出,觸發提示音。
② NotificationRequest
  • NotificationRequest 用于設置具體的通知對象,包括設置通知的屬性,如:通知的分發時間、小圖標、大圖標、自動刪除等參數,以及設置具體的通知類型,如普通文本、長文本等。
  • NotificationRequest 主要接口:
接口名描述
NotificationRequest()構建一個通知
NotificationRequest(int notificationId)構建一個通知,指定通知的id。通知的Id在應用內容具有唯一性,如果不指定,默認為0
setNotificationId?(int notificationId)設置當前通知id
setAutoDeletedTime?(long time)設置通知自動取消的時間戳
setContent?(NotificationRequest.NotificationContent content)設置通知的具體內容
setDeliveryTime?(long deliveryTime)設置通知分發的時間戳
setSlotId(String slotId)設置通知的NotificationSlot id
setTapDismissed?(boolean tapDismissed)設置通知在用戶點擊后是否自動取消
setLittleIcon?(PixelMap smallIcon)設置通知的小圖標,在通知左上角顯示
setBigIcon(PixelMap bigIcon)設置通知的大圖標,在通知的右邊顯示
setGroupValue(String groupValue)設置分組通知,相同分組的通知在通知欄顯示時,將會折疊在一組應用中顯示
addActionButton(NotificationActionButton actionButton)設置通知添加通知ActionButton
setIntentAgent(IntentAgent agent)設置通知承載指定的IntentAgent,在通知中實現即將觸發的事件
  • 具體的通知類型:目前支持六種類型,包括普通文本 NotificationNormalContent、長文本 NotificationLongTextContent、圖片 NotificationPictureContent、多行 NotificationMultiLineContent、社交 NotificationConversationalContent、媒體 NotificationMediaContent。
  • 通知類型的主要接口:
類名接口名描述
NotificationNormalContentsetTitle(String title)設置通知標題
NotificationNormalContentsetText?(String text)設置通知內容
NotificationNormalContentsetAdditionalText(String additionalText)設置通知次要內容,是對通知內容的補充
NotificationPictureContentsetBriefText(String briefText)設置通知概要內容,是對通知內容的總結
NotificationPictureContentsetExpandedTitle(String expandedTitle)設置附加圖片的通知展開時的標題
NotificationPictureContentsetBigPicture(PixelMap bigPicture)設置通知的圖片內容,附加在setText?(String text)下方
NotificationLongTextContentsetLongText?(String longText)設置通知的長文本
NotificationConversationalContentsetConversationTitle(String conversationTitle)設置社交通知的標題
NotificationConversationalContentaddConversationalMessage(ConversationalMessage message)通知添加一條消息
NotificationMultiLineContentaddSingleLine(String line)在當前通知中添加一行文本
NotificationMediaContentsetAVToken(AVToken avToken)將媒體通知綁定指定的AVToken
NotificationMediaContentsetShownActions(int[] actions)設置媒體通知待展示的按鈕
  • 通知發布后,通知的設置不可修改。如果下次發布通知使用相同的 id,就會更新之前發布的通知。
③ NotificationHelper
  • NotificationHelper 封裝了發布、更新、刪除通知等靜態方法。
  • NotificationHelper 主要接口:
接口名描述
publishNotification(NotificationRequest request)發布一條通知
publishNotification(String tag, NotificationRequest request)發布一條帶TAG的通知
cancelNotification(int notificationId)取消指定的通知
cancelNotification(String tag, int notificationId)取消指定的帶TAG的通知
cancelAllNotifications()取消之前發布的所有通知
addNotificationSlot(NotificationSlot slot)創建一個NotificationSlot
getNotificationSlot(String slotId)獲取NotificationSlot
removeNotificationSlot(String slotId)刪除一個NotificationSlot
getActiveNotifications()獲取當前應用發的活躍通知
getActiveNotificationNums()獲取系統中當前應用發的活躍通知的數量
setNotificationBadgeNum(int num)設置通知的角標
setNotificationBadgeNum()設置當前應用中活躍狀態通知的數量在角標顯示

三、通知的使用

① 創建 NotificationSlot
  • NotificationSlot 可以設置公共通知的震動,鎖屏模式,重要級別等,并通過調用 NotificationHelper.addNotificationSlot() 發布 NotificationSlot 對象。
NotificationSlot slot = new NotificationSlot("slot_001", "slot_default", NotificationSlot.LEVEL_MIN); // 創建notificationSlot對象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.");}
② 發布通知
  • 構建 NotificationRequest 對象,應用發布通知前,通過 NotificationRequest 的 setSlotId() 方法與 NotificationSlot 綁定,使該通知在發布后都具備該對象的特征。
int notificationId = 1;NotificationRequest request = new NotificationRequest(notificationId);request.setSlotId(slot.getId());
  • 調用 setContent() 設置通知的內容:
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); // 設置通知的內容
  • 調用 publishNotification() 發布通知:
try {NotificationHelper.publishNotification(request);} catch (RemoteException ex) {HiLog.error(LABEL, "Exception occurred during publishNotification invocation.");}
③ 取消通知
  • 取消通知分為取消指定單條通知和取消所有通知,應用只能取消自己發布的通知。
  • 調用 cancelNotification() 取消指定的單條通知。
int notificationId = 1;try {NotificationHelper.cancelNotification(notificationId);} catch (RemoteException ex) {HiLog.error(LABEL, "Exception occurred during cancelNotification invocation.");}
  • 調用 cancelAllNotifications() 取消所有通知:
try {NotificationHelper.cancelAllNotifications();} catch (RemoteException ex) {HiLog.error(LABEL, "Exception occurred during cancelAllNotifications invocation.");}

總結

以上是生活随笔為你收集整理的HarmonyOS之深入解析通知的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。