那曲檬骨新材料有限公司

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

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

3天內不再提示

鴻蒙系統中如何實現通知功能

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

HarmonyOS 通過 ANS(Advanced Notification Service,即通知增強服務)系統服務來為應用程序提供發布通知的能力。

通知提供應用的即時消息或通信消息,用戶可以直接刪除或點擊通知觸發進一步的操作。

功能介紹

HarmonyOS 提供了通知功能,即在一個應用的 UI 界面之外顯示的消息,主要用來提醒用戶有來自該應用中的信息

當應用向系統發出通知時,它將先以圖標的形式顯示在通知欄中,用戶可以下拉通知欄查看通知的詳細信息。

常見的使用場景:

顯示接收到短消息、即時消息等。

顯示應用的推送消息,如廣告、版本更新等。

顯示當前正在進行的事件,如播放音樂、導航、下載等。

開發指南

通知的開發指導分為創建 NotificationSlot、發布通知和取消通知等開發場景。

①創建 NotificationSlot

代碼如下:

NotificationSlot slot = new NotificationSlot(“slot_001”, “slot_default”, NotificationSlot.LEVEL_MIN); // 創建notificationSlot對象

slot.setLevel(LEVEL_HIGH);//設置通知優先級

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.”);

}

②發布通知

構建 NotificationRequest 對象:

request = new NotificationRequest(notificationId);

request.setSlotId(slot.getId());

調用 setContent() 設置通知的內容:

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); // 設置通知的內容

request.setTapDismissed(!tapDismissed);//設置用戶點擊通知后自動消失

調用 publishNotification() 發布通知:

try {

NotificationHelper.publishNotification(request);

} catch (RemoteException ex) {

HiLog.warn(LABEL, “publishNotification occur exception.”);

}

③取消通知

如下:

調用cancelNotification()取消指定的單條通知。

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.”);

}

④通過 IntentAgent 觸發通知的跳轉

代碼如下:

//點擊通知,跳轉至指定的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);

實現效果圖:

附上源碼:

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.調用publishNotification()發送通知

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.創建NotificationSlot

NotificationSlot slot = new NotificationSlot(“slot_001”, “slot_default”, NotificationSlot.LEVEL_MIN); // 創建notificationSlot對象

slot.setLevel(LEVEL_HIGH);//設置通知優先級

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); // 設置通知的內容

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 =“測試通知”;

//通知內容文本

String content=“通知的內容已經到了!”;

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

聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。 舉報投訴
  • 鴻蒙系統
    +關注

    關注

    183

    文章

    2638

    瀏覽量

    66714
  • HarmonyOS
    +關注

    關注

    79

    文章

    1982

    瀏覽量

    30579

原文標題:鴻蒙OS通知功能,輕松實現!

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

收藏 人收藏

    評論

    相關推薦

    【「鴻蒙操作系統設計原理與架構」閱讀體驗】01-

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

    AIGC入門及鴻蒙入門

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

    開源鴻蒙系統外設指紋儀模塊功能演示#OpenHarmony

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

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

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

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

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

    華為原生鴻蒙操作系統正式發布

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

    鴻蒙生態設備超10億!原生鴻蒙發布,國產操作系統實現自主可控

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

    鴻蒙跨端實踐-JS虛擬機架構實現

    類似的框架,我們需要自行實現以確保核心基礎能力的完整。 鴻蒙虛擬機的開發經歷了從最初 ArkTs2V8 到 JSVM + Roma新架構方案 。在此過程,我們實現了完整的
    的頭像 發表于 09-30 14:42 ?2518次閱讀
    <b class='flag-5'>鴻蒙</b>跨端實踐-JS虛擬機架構<b class='flag-5'>實現</b>

    關于BLE通知值的通知長度問題求解

    FreeRtos 的 streambuffer 的壓力。 如果 notify 支持每次通知大小大于 244 的數組,我需要做哪些修改才能實現每次通知大小為 512 或 1024 的
    發表于 05-27 08:32

    最新開源代碼證實!“鴻蒙原生版”微信正在積極開發

    遷移到另一個操作系統平臺的時間和成本會非常高。 目前看來,微信的鴻蒙原生版確實正在開發,并且已經取得了一定的進展。這對于鴻蒙操作系統的推廣
    發表于 05-08 17:08

    鴻蒙系統三防平板怎么樣

    方便地進行操作。 在實際應用鴻蒙系統三防平板表現出色。無論是在戶外探險中提供導航、拍攝等功能,還是在工地作業協助進行數據分析、圖紙繪
    發表于 04-12 14:26

    鴻蒙OS開發學習:【尺寸適配實現

    鴻蒙開發,尺寸適配是一個重要的概念,它可以幫助我們在不同屏幕尺寸的設備上正確顯示和布局我們的應用程序。本文將介紹如何在鴻蒙開發實現尺寸
    的頭像 發表于 04-10 16:05 ?1860次閱讀
    <b class='flag-5'>鴻蒙</b>OS開發學習:【尺寸適配<b class='flag-5'>實現</b>】

    鴻蒙系統三防平板電腦突出的性能和環境的適用性

    保障用戶數據的安全。綜上所述,鴻蒙系統三防平板在性能、功能、防護能力、應用生態、續航能力和安全性等方面都具備明顯的優勢。無論是在日常生活還是工作,都能夠為用戶提供便捷、高效、安全的使
    發表于 04-09 14:24

    鴻蒙OS開發實例:【通知消息】

    HarmonyOS 論壇中有研發人員求助,反饋通知沒有沒有聲音,因此在真機上驗證了一下,果不其然,沒有通知的提示音,后來解決辦法也非常簡單,在手機設置應用,將可以打開的通知提示統統改
    的頭像 發表于 04-01 15:34 ?653次閱讀
    <b class='flag-5'>鴻蒙</b>OS開發實例:【<b class='flag-5'>通知</b>消息】

    鴻蒙實戰項目開發:【短信服務】

    ||---SmsModel.ets// 封裝短信類 具體實現 發送短信功能在SmsModel : /* * Copyright (c) 2022 Huawei Device Co., Ltd.
    發表于 03-03 21:29
    百家乐官网小揽| 同花顺百家乐的玩法技巧和规则 | 百家乐游戏百家乐| 大发888娱乐亚洲| 太阳城申博娱乐| 新大发888娱乐城| 大发888开户xa11| 大发888游戏下载官网免费| 大发888真钱娱乐| 大发888hanpa| 和记国际娱乐| 禹城市| 百家乐官网连黑记录| 百家乐官网庄闲必赢| 闲和庄百家乐官网娱乐场| A8百家乐官网的玩法技巧和规则| 正规博彩通| 巴厘岛百家乐官网的玩法技巧和规则 | 大富豪百家乐| 威尼斯人娱乐场官网48008| 金三角娱乐城| 百家乐官网永利赌场娱乐网规则| 百家乐试玩全讯网2| 大发888为什么打不开| 长泰县| 百家乐官网筹码防伪| 沙龙百家乐代理| 澳门百家乐官网死局| 百家乐的必赢方法| 棋牌游戏注册送6元| 乐天堂百家乐官网娱乐网| 真人百家乐体验金| 浏阳市| 大发888为什么打不开| 网上百家乐官网赌城| 二八杠算法| 百家乐官网网络真人斗地主| 蓝盾百家乐官网赌场娱乐网规则| 线上百家乐怎么玩| 百家乐官网赌博大揭密| 百家乐平注法到|