在线观看www成人影院-在线观看www日本免费网站-在线观看www视频-在线观看操-欧美18在线-欧美1级

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內(nèi)不再提示

鴻蒙系統(tǒng)中如何實現(xiàn)通知功能

OpenHarmony技術社區(qū) ? 來源:鴻蒙技術社區(qū) ? 作者:軟通張二龍 ? 2021-09-06 09:42 ? 次閱讀

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

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學習之用,如有內(nèi)容侵權或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • 鴻蒙系統(tǒng)

    關注

    183

    文章

    2641

    瀏覽量

    67847
  • HarmonyOS
    +關注

    關注

    80

    文章

    2088

    瀏覽量

    32388

原文標題:鴻蒙OS通知功能,輕松實現(xiàn)!

文章出處:【微信號:gh_834c4b3d87fe,微信公眾號:OpenHarmony技術社區(qū)】歡迎添加關注!文章轉(zhuǎn)載請注明出處。

收藏 人收藏

    評論

    相關推薦
    熱點推薦

    【HarmonyOS 5】桌面快捷方式功能實現(xiàn)詳解

    【HarmonyOS 5】桌面快捷方式功能實現(xiàn)詳解 ##鴻蒙開發(fā)能力 ##HarmonyOS SDK應用服務##鴻蒙金融類應用 (金融理財# 一、前言 在移動應用開發(fā)
    的頭像 發(fā)表于 06-21 16:42 ?107次閱讀
    【HarmonyOS 5】桌面快捷方式<b class='flag-5'>功能</b><b class='flag-5'>實現(xiàn)</b>詳解

    鴻蒙Next實現(xiàn)瀑布流布局

    高度或?qū)捀弑龋瑴p少動態(tài)測量開銷 合理實現(xiàn)懶加載:對于非首屏內(nèi)容或圖片資源,一定要實現(xiàn)懶加載 漸進式增強體驗:先確保基礎功能可用,再添加動畫和交互效果 測試與優(yōu)化:在不同設備上測試性能表現(xiàn),針對卡頓問題進行專項優(yōu)化 遵循
    發(fā)表于 06-10 14:17

    HarmonyOS NEXT應用開發(fā)-Notification Kit(用戶通知服務)通知類型、級別與渠道

    :SystemCapability.Notification.Notification 名稱 值 說明 LEVEL_NONE 0 表示關閉通知功能。 LEVEL_MIN 1 表示通知功能
    發(fā)表于 06-09 14:39

    國產(chǎn)操作系統(tǒng)加速崛起——鴻蒙電腦補齊鴻蒙生態(tài)最重要拼圖

    實現(xiàn)重要突破。 “鴻蒙電腦的推出,是鴻蒙操作系統(tǒng)生態(tài)版圖得以完整的關鍵一步。”浙江大學傳媒與國際文化學院常務副院長方興東對科技日報記者說,“目前全球只有
    的頭像 發(fā)表于 05-21 11:41 ?151次閱讀

    DevEco Studio AI輔助開發(fā)工具兩大升級功能 鴻蒙應用開發(fā)效率再提升

    HarmonyOS應用的AI智能輔助開發(fā)助手——CodeGenie,該AI助手深度集成在DevEco Studio,提供鴻蒙知識智能問答、鴻蒙ArkTS代碼補全/生成和萬能卡片生成等功能
    發(fā)表于 04-18 14:43

    【「鴻蒙操作系統(tǒng)設計原理與架構」閱讀體驗】01-初始華為鴻蒙

    ,然后介紹了鴻蒙操作系統(tǒng)出現(xiàn)的原因,在國產(chǎn)化的地位以及標志性的意義,同時也介紹了關于鴻蒙操作系統(tǒng)的基礎設計理念以及
    發(fā)表于 01-25 11:05

    AIGC入門及鴻蒙入門

    模型,能夠生成與給定文本描述相符的圖像。 鴻蒙系統(tǒng)入門 1. 基礎知識: 鴻蒙系統(tǒng)(HarmonyOS)是華為推出的一款分布式操作系統(tǒng),旨
    發(fā)表于 01-13 10:32

    企業(yè)展廳系統(tǒng)集成設計,鴻蒙生態(tài)創(chuàng)新中心展廳智能控制方案

    的展示廳量身定制的展廳系統(tǒng),通過精細化的系統(tǒng)設計與網(wǎng)絡連接技術的運用,確保展廳所有設備能夠穩(wěn)定、高效地接收和執(zhí)行控主機的指令,實現(xiàn)了對
    的頭像 發(fā)表于 12-27 17:12 ?546次閱讀
    企業(yè)展廳<b class='flag-5'>中</b>控<b class='flag-5'>系統(tǒng)</b>集成設計,<b class='flag-5'>鴻蒙</b>生態(tài)創(chuàng)新中心展廳智能控制方案

    開源鴻蒙系統(tǒng)外設指紋儀模塊功能演示#OpenHarmony

    鴻蒙系統(tǒng)
    深圳市視美泰技術股份有限公司
    發(fā)布于 :2024年12月17日 10:45:58

    名單公布!【書籍評測活動NO.53】鴻蒙操作系統(tǒng)設計原理與架構

    的底層設計邏輯出發(fā),針對不同關鍵子系統(tǒng)的目標功能實現(xiàn)路徑做實際分析解讀,幫助開發(fā)者理解鴻蒙操作系統(tǒng)的底層邏輯,開發(fā)更適合
    發(fā)表于 12-16 15:10

    Taro 鴻蒙技術內(nèi)幕系列(三) - 多語言場景下的通用事件系統(tǒng)設計

    生態(tài)系統(tǒng),雖然原生應用通常基于 ArkTS 實現(xiàn),但在實際研發(fā)過程中發(fā)現(xiàn),使用 C++ 可以顯著提升應用框架和業(yè)務的性能表現(xiàn)。隨著鴻蒙系統(tǒng)
    的頭像 發(fā)表于 11-27 11:42 ?572次閱讀
    Taro <b class='flag-5'>鴻蒙</b>技術內(nèi)幕系列(三) - 多語言場景下的通用事件<b class='flag-5'>系統(tǒng)</b>設計

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

    華為鴻蒙系統(tǒng)刷屏了!10月22日, 華為正式發(fā)布原生鴻蒙操作系統(tǒng)HarmonyOS NEXT,成為與蘋果iOS系統(tǒng)和谷歌安卓
    的頭像 發(fā)表于 10-25 11:43 ?799次閱讀
    華為<b class='flag-5'>鴻蒙</b><b class='flag-5'>系統(tǒng)</b>正式發(fā)布,華鼎冷鏈科技攜手共筑國產(chǎn)OS生態(tài)

    華為原生鴻蒙操作系統(tǒng)正式發(fā)布

    10月22日晚,華為舉行了一場盛大的發(fā)布會,正式推出了其原生鴻蒙操作系統(tǒng)HarmonyOS NEXT,也被稱為鴻蒙5.0。這一發(fā)布標志著鴻蒙系統(tǒng)
    的頭像 發(fā)表于 10-23 16:52 ?1039次閱讀

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

    10月22日晚間,原生鴻蒙之夜暨華為全場景新品發(fā)布會正式召開,華為常務董事、終端BG董事長、智能汽車解決方案BU董事長余承東宣布,搭載鴻蒙操作系統(tǒng),包括Open Harmony的生態(tài)設備超過10億。我們
    的頭像 發(fā)表于 10-23 12:04 ?2468次閱讀
    <b class='flag-5'>鴻蒙</b>生態(tài)設備超10億!原生<b class='flag-5'>鴻蒙</b>發(fā)布,國產(chǎn)操作<b class='flag-5'>系統(tǒng)</b><b class='flag-5'>實現(xiàn)</b>自主可控

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

    類似的框架,我們需要自行實現(xiàn)以確保核心基礎能力的完整。 鴻蒙虛擬機的開發(fā)經(jīng)歷了從最初 ArkTs2V8 到 JSVM + Roma新架構方案 。在此過程,我們實現(xiàn)了完整的
    的頭像 發(fā)表于 09-30 14:42 ?2916次閱讀
    <b class='flag-5'>鴻蒙</b>跨端實踐-JS虛擬機架構<b class='flag-5'>實現(xiàn)</b>
    主站蜘蛛池模板: 手机看片福利国产 | 亚洲激情综合 | 美女扒开尿口给男人爽的视频 | 精品特级毛片 | 曰本裸色私人影院噜噜噜影院 | 亚洲天堂电影在线观看 | 国产高清一区二区 | 色天使美国 | 32pao强力打造免费高速高清 | 欧美日韩精品乱国产538 | 丁香婷婷九月 | 22eee在线播放成人免费视频 | 亚洲一区二区三区四 | 国产精品久久久久久久久福利 | 色偷偷资源 | 国产理论精品 | 亚洲视频五区 | 性夜黄a爽爽免费视频国产 羞羞答答xxdd影院欧美 | 国产精品久久免费观看 | 日本媚薬痉挛在线观看免费 | 国模论坛| 免费在线观看视频网站 | 操插干| 日本特黄特色免费大片 | 影音先锋ady69色资源网站 | 国产精品推荐天天看天天爽 | 欧美精品hdvideosex | 久久久国产精品免费 | 日韩亚洲人成网站在线播放 | 天天澡天天摸天天添视频 | 国产午夜a理论毛片在线影院 | 每日最新avhd101天天看新片 | 精品视频卡1卡2卡3 精品视频免费看 | 国产精品资源站 | 天天射天天操天天色 | 波多野结衣在线免费视频 | a资源在线观看 | 久久久精品免费热线观看 | 高h乱肉辣文辣书阁 | 久久成人福利视频 | 精品二区 |