HarmonyOS 通過 ANS(Advanced Notification Service,即通知增強服務)系統(tǒng)服務來為應用程序提供發(fā)布通知的能力。
通知提供應用的即時消息或通信消息,用戶可以直接刪除或點擊通知觸發(fā)進一步的操作。
功能介紹
HarmonyOS 提供了通知功能,即在一個應用的 UI 界面之外顯示的消息,主要用來提醒用戶有來自該應用中的信息。
當應用向系統(tǒng)發(fā)出通知時,它將先以圖標的形式顯示在通知欄中,用戶可以下拉通知欄查看通知的詳細信息。
常見的使用場景:
顯示接收到短消息、即時消息等。
顯示應用的推送消息,如廣告、版本更新等。
顯示當前正在進行的事件,如播放音樂、導航、下載等。
開發(fā)指南
通知的開發(fā)指導分為創(chuàng)建 NotificationSlot、發(fā)布通知和取消通知等開發(fā)場景。
①創(chuàng)建 NotificationSlot
代碼如下:
NotificationSlot slot = new NotificationSlot(“slot_001”, “slot_default”, NotificationSlot.LEVEL_MIN); // 創(chuàng)建notificationSlot對象
slot.setLevel(LEVEL_HIGH);//設置通知優(yōu)先級
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.warn(LABEL, “addNotificationSlot occur exception.”);
}
②發(fā)布通知
構建 NotificationRequest 對象:
request = new NotificationRequest(notificationId);
request.setSlotId(slot.getId());
調(diào)用 setContent() 設置通知的內(nèi)容:
NotificationRequest.NotificationLongTextContent content = new NotificationRequest.NotificationLongTextContent();
content.setTitle(title)
.setText(messageContent);
NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(
content);
request.setGroupValue(“group information”);
request.setContent(notificationContent); // 設置通知的內(nèi)容
request.setTapDismissed(!tapDismissed);//設置用戶點擊通知后自動消失
調(diào)用 publishNotification() 發(fā)布通知:
try {
NotificationHelper.publishNotification(request);
} catch (RemoteException ex) {
HiLog.warn(LABEL, “publishNotification occur exception.”);
}
③取消通知
如下:
調(diào)用cancelNotification()取消指定的單條通知。
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.”);
}
④通過 IntentAgent 觸發(fā)通知的跳轉(zhuǎn)
代碼如下:
//點擊通知,跳轉(zhuǎn)至指定的Ability
Intent intent1 = new Intent();
// 指定要啟動的Ability的BundleName和AbilityName字段
Operation operation = new Intent.OperationBuilder()
.withDeviceId(“”)
.withBundleName(“com.example.myapplication”)
.withAbilityName(“com.example.myapplication.SecondAbility”)
.build();
// 將Operation對象設置到Intent中
intent1.setOperation(operation);
List《Intent》 intentList = new ArrayList《》();
intentList.add(intent1);
// 定義請求碼int requestCode = 200;
// 設置flags
List《IntentAgentConstant.Flags》 flags = new ArrayList《》();
flags.add(IntentAgentConstant.Flags.UPDATE_PRESENT_FLAG);
// 指定啟動一個有頁面的Ability
IntentAgentInfo paramsInfo = new IntentAgentInfo(requestCode,
IntentAgentConstant.OperationType.START_ABILITY, flags, intentList, null);
// 獲取IntentAgent實例
IntentAgent agent = IntentAgentHelper.getIntentAgent(context, paramsInfo);
//5.設置通知的IntentAgent
request.setIntentAgent(agent);
實現(xiàn)效果圖:
附上源碼:
NotificationUtils:
public class NotificationUtils {
private static final int LEVEL_HIGH = 4;
private static NotificationRequest request;
private static final int MY_MODULE = 0x00201;
private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, MY_MODULE, “MY_TAG”);
private static final boolean tapDismissed = false;
//3.調(diào)用publishNotification()發(fā)送通知
public static void publishNotification() {
try {
NotificationHelper.publishNotification(request);
} catch (RemoteException ex) {
HiLog.warn(LABEL, “publishNotification occur exception.”);
}
}
public static void initNotificationSlot(Context context, int notificationId, String title,
String messageContent) {
//1.創(chuàng)建NotificationSlot
NotificationSlot slot = new NotificationSlot(“slot_001”, “slot_default”, NotificationSlot.LEVEL_MIN); // 創(chuàng)建notificationSlot對象
slot.setLevel(LEVEL_HIGH);//設置通知優(yōu)先級
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.warn(LABEL, “addNotificationSlot occur exception.”);
}
request = new NotificationRequest(notificationId);
request.setSlotId(slot.getId());
NotificationRequest.NotificationLongTextContent content = new NotificationRequest.NotificationLongTextContent();
content.setTitle(title)
.setText(messageContent);
NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(
content);
request.setGroupValue(“group information”);
request.setContent(notificationContent); // 設置通知的內(nèi)容
request.setTapDismissed(!tapDismissed);//設置用戶點擊通知后自動消失
//4.返回應用,首先獲取IntentAgent
Intent intent1 = new Intent();
// 指定要啟動的Ability的BundleName和AbilityName字段
Operation operation = new Intent.OperationBuilder()
.withDeviceId(“”)
.withBundleName(“com.example.myapplication”)
.withAbilityName(“com.example.myapplication.SecondAbility”)
.build();
// 將Operation對象設置到Intent中
intent1.setOperation(operation);
List《Intent》 intentList = new ArrayList《》();
intentList.add(intent1);
// 定義請求碼
int requestCode = 200;
// 設置flags
List《IntentAgentConstant.Flags》 flags = new ArrayList《》();
flags.add(IntentAgentConstant.Flags.UPDATE_PRESENT_FLAG);
// 指定啟動一個有頁面的Ability
IntentAgentInfo paramsInfo = new IntentAgentInfo(requestCode,
IntentAgentConstant.OperationType.START_ABILITY, flags, intentList, null);
// 獲取IntentAgent實例
IntentAgent agent = IntentAgentHelper.getIntentAgent(context, paramsInfo);
//5.設置通知的IntentAgent
request.setIntentAgent(agent);
}
}
MainAbilitySlice:
public class MainAbilitySlice extends AbilitySlice implements ClickedListener {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
//通知
Text notification = (Text) findComponentById(ResourceTable.Id_text_notification);
notification.setClickedListener(this);
//通知標題
String title =“測試通知”;
//通知內(nèi)容文本
String content=“通知的內(nèi)容已經(jīng)到了!”;
NotificationUtils.initNotificationSlot(this,1,title,content);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
@Override
public void onClick(Component component) {
switch (component.getId()){
case ResourceTable.Id_text_notification:
NotificationUtils.publishNotification();
break;
}
}
}
ability_main.xml:
《?xml version=“1.0” encoding=“utf-8”?》《DirectionalLayout
xmlns:ohos=“http://schemas.huawei.com/res/ohos”
ohos:height=“match_parent”
ohos:orientation=“vertical”
ohos:width=“match_parent”》
《Text
ohos:id=“$+id:text_notification”
ohos:width=“match_parent”
ohos:height=“match_content”
ohos:text_size=“25fp”
ohos:top_margin=“300vp”
ohos:left_margin=“15vp”
ohos:right_margin=“15vp”
ohos:background_element=“$graphic:background_text”
ohos:text=“通知”
ohos:text_weight=“1000”
ohos:text_alignment=“horizontal_center”/》《/DirectionalLayout》
background_text.xml:
《?xml version=“1.0” encoding=“utf-8”?》《shape xmlns:ohos=“http://schemas.huawei.com/res/ohos”
ohos:shape=“rectangle”》
《corners
ohos:radius=“20”/》
《solid
ohos:color=“#a5b3a9”/》《/shape》
責任編輯:haq
-
鴻蒙系統(tǒng)
+關注
關注
183文章
2641瀏覽量
67847 -
HarmonyOS
+關注
關注
80文章
2088瀏覽量
32388
原文標題:鴻蒙OS通知功能,輕松實現(xiàn)!
文章出處:【微信號:gh_834c4b3d87fe,微信公眾號:OpenHarmony技術社區(qū)】歡迎添加關注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
【HarmonyOS 5】桌面快捷方式功能實現(xiàn)詳解

鴻蒙Next實現(xiàn)瀑布流布局
HarmonyOS NEXT應用開發(fā)-Notification Kit(用戶通知服務)通知類型、級別與渠道
國產(chǎn)操作系統(tǒng)加速崛起——鴻蒙電腦補齊鴻蒙生態(tài)最重要拼圖
DevEco Studio AI輔助開發(fā)工具兩大升級功能 鴻蒙應用開發(fā)效率再提升
【「鴻蒙操作系統(tǒng)設計原理與架構」閱讀體驗】01-初始華為鴻蒙
AIGC入門及鴻蒙入門
企業(yè)展廳中控系統(tǒng)集成設計,鴻蒙生態(tài)創(chuàng)新中心展廳智能控制方案


名單公布!【書籍評測活動NO.53】鴻蒙操作系統(tǒng)設計原理與架構
Taro 鴻蒙技術內(nèi)幕系列(三) - 多語言場景下的通用事件系統(tǒng)設計

華為鴻蒙系統(tǒng)正式發(fā)布,華鼎冷鏈科技攜手共筑國產(chǎn)OS生態(tài)

華為原生鴻蒙操作系統(tǒng)正式發(fā)布
鴻蒙生態(tài)設備超10億!原生鴻蒙發(fā)布,國產(chǎn)操作系統(tǒng)實現(xiàn)自主可控

鴻蒙跨端實踐-JS虛擬機架構實現(xiàn)

評論