通知
HarmonyOS提供了應(yīng)用的通知功能,即在應(yīng)用外層通過使用應(yīng)用圖標(biāo)進(jìn)行一些事件的通知剃氧。常見的使用場(chǎng)景:
- 顯示接收到短消息、即時(shí)消息等阻星。
- 顯示應(yīng)用的推送消息朋鞍,如廣告、版本更新等妥箕。
- 顯示當(dāng)前正在進(jìn)行的事件滥酥,如播放音樂、導(dǎo)航畦幢、下載等坎吻。
接口說明
通知相關(guān)基礎(chǔ)類包含NotificationSlot、NotificationRequest和NotificationHelper宇葱∈菡妫基礎(chǔ)類之間的關(guān)系如下所示:
圖1 通知基礎(chǔ)類關(guān)系圖
在這里插入圖片描述
- NotificationSlot
NotificationSlot可以對(duì)提示音、振動(dòng)黍瞧、鎖屏顯示和重要級(jí)別等進(jìn)行設(shè)置诸尽。一個(gè)應(yīng)用可以創(chuàng)建一個(gè)或多個(gè)NotificationSlot,在發(fā)布通知時(shí)印颤,通過綁定不同的NotificationSlot您机,實(shí)現(xiàn)不同用途。
NotificationSlot的級(jí)別目前支持如下幾種膀哲, 由低到高:
- LEVEL_NONE: 表示通知不發(fā)布往产。
- LEVEL_MIN:表示通知可以發(fā)布被碗,但是不顯示在通知欄某宪,不自動(dòng)彈出,無提示音锐朴;該級(jí)別不適用于前臺(tái)服務(wù)的場(chǎng)景兴喂。
- LEVEL_LOW:表示通知可以發(fā)布且顯示在通知欄,不自動(dòng)彈出,無提示音衣迷。
- LEVEL_DEFAULT:表示通知發(fā)布后可在通知欄顯示畏鼓,不自動(dòng)彈出,觸發(fā)提示音壶谒。
- LEVEL_HIGH:表示通知發(fā)布后可在通知欄顯示云矫,自動(dòng)彈出,觸發(fā)提示音汗菜。
- NotificationRequest
NotificationRequest用于設(shè)置具體的通知對(duì)象让禀,包括設(shè)置通知的屬性,如:通知的分發(fā)時(shí)間陨界、小圖標(biāo)巡揍、大圖標(biāo)、自動(dòng)刪除等參數(shù)菌瘪,以及設(shè)置具體的通知類型腮敌,如普通文本、長(zhǎng)文本等俏扩。
具體的通知類型:目前支持六種類型糜工,包括普通文本NotificationNormalContent、長(zhǎng)文本NotificationLongTextContent动猬、圖片NotificationPictureContent啤斗、多行NotificationMultiLineContent、社交NotificationConversationalContent赁咙、媒體NotificationMediaContent钮莲。
- NotificationHelper
NotificationHelper封裝了發(fā)布、更新彼水、刪除通知等靜態(tài)方法崔拥。
效果演示
演示地址 https://live.csdn.net/v/168000
開發(fā)步驟
通知的開發(fā)指導(dǎo)分為創(chuàng)建NotificationSlot、發(fā)布通知和取消通知等開發(fā)場(chǎng)景凤覆。
第一步链瓦、初始化NotificationSlot
public static final String SLOT_ID = "high";
public static final String SLOT_NAME = "Order notification";
//--------------------
....
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_main_ability_slice);
...
defineNotificationSlot(Const.SLOT_ID, Const.SLOT_NAME, NotificationSlot.LEVEL_HIGH);
...
}
//---------------------
private void defineNotificationSlot(String id, String name, int importance) {
// 創(chuàng)建notificationSlot對(duì)象
NotificationSlot notificationSlot = new NotificationSlot(id, name, importance);
// 設(shè)置振動(dòng)提醒
notificationSlot.setEnableVibration(true);
// 設(shè)置鎖屏模式
notificationSlot.setLockscreenVisibleness(NotificationRequest.VISIBLENESS_TYPE_PUBLIC);
Uri uri = Uri.parse(Const.SOUND_URI);
notificationSlot.setSound(uri);
try {
NotificationHelper.addNotificationSlot(notificationSlot);
} catch (RemoteException ex) {
HiLog.error(LABEL_LOG, "%{public}s", "defineNotificationSlot remoteException.");
}
}
第二步、發(fā)布通知
private void publishNotification(String title, String text) {
//構(gòu)建NotificationRequest對(duì)象盯桦,應(yīng)用發(fā)布通知前慈俯,通過NotificationRequest的setSlotId()方法與NotificationSlot綁定,使該通知在發(fā)布后都具備該對(duì)象的特征
notificationId = 0x1000001;
NotificationRequest request = new NotificationRequest(notificationId).setSlotId(Const.SLOT_ID)
.setTapDismissed(true);
//調(diào)用setContent()設(shè)置通知的內(nèi)容
request.setContent(createNotificationContent(title, text));
IntentAgent intentAgent = createIntentAgent(MainAbility.class.getName(),
IntentAgentConstant.OperationType.START_ABILITY);
request.setIntentAgent(intentAgent);
//調(diào)用publishNotification()發(fā)布通知
try {
NotificationHelper.publishNotification(request);
} catch (RemoteException ex) {
HiLog.error(LABEL_LOG, "%{public}s", "publishNotification remoteException.");
}
}
第三步拥峦、取消通知
取消通知分為取消指定單條通知和取消所有通知贴膘,應(yīng)用只能取消自己發(fā)布的通知。
- 調(diào)用cancelNotification()取消指定的單條通知略号。
private void cancel() {
try {
NotificationHelper.cancelNotification(notificationId);
} catch (RemoteException ex) {
HiLog.error(LABEL_LOG, "%{public}s", "cancel remoteException.");
}
}
- 調(diào)用cancelAllNotifications()取消所有通知
private void cancelAll() {
try {
NotificationHelper.cancelAllNotifications();
} catch (RemoteException ex) {
HiLog.error(LABEL_LOG, "%{public}s", "cancelAll remoteException.");
}
}