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

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

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

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

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

HarmonyOS 通過 ANS(Advanced Notification Service,即通知增強(qiáng)服務(wù))系統(tǒng)服務(wù)來為應(yīng)用程序提供發(fā)布通知的能力。

通知提供應(yīng)用的即時(shí)消息或通信消息,用戶可以直接刪除或點(diǎn)擊通知觸發(fā)進(jìn)一步的操作。

功能介紹

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

當(dāng)應(yīng)用向系統(tǒng)發(fā)出通知時(shí),它將先以圖標(biāo)的形式顯示在通知欄中,用戶可以下拉通知欄查看通知的詳細(xì)信息。

常見的使用場(chǎng)景:

顯示接收到短消息、即時(shí)消息等。

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

顯示當(dāng)前正在進(jìn)行的事件,如播放音樂、導(dǎo)航、下載等。

開發(fā)指南

通知的開發(fā)指導(dǎo)分為創(chuàng)建 NotificationSlot、發(fā)布通知和取消通知等開發(fā)場(chǎng)景。

①創(chuàng)建 NotificationSlot

代碼如下:

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

slot.setLevel(LEVEL_HIGH);//設(shè)置通知優(yōu)先級(jí)

slot.setDescription(“NotificationSlotDescription”);

slot.setEnableVibration(true); // 設(shè)置振動(dòng)提醒

slot.setLockscreenVisibleness(NotificationRequest.VISIBLENESS_TYPE_PUBLIC);//設(shè)置鎖屏模式

slot.setEnableLight(true); // 設(shè)置開啟呼吸燈提醒

slot.setLedLightColor(Color.RED.getValue());// 設(shè)置呼吸燈的提醒顏色try {

NotificationHelper.addNotificationSlot(slot);

} catch (RemoteException ex) {

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

}

②發(fā)布通知

構(gòu)建 NotificationRequest 對(duì)象:

request = new NotificationRequest(notificationId);

request.setSlotId(slot.getId());

調(diào)用 setContent() 設(shè)置通知的內(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); // 設(shè)置通知的內(nèi)容

request.setTapDismissed(!tapDismissed);//設(shè)置用戶點(diǎn)擊通知后自動(dòng)消失

調(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)

代碼如下:

//點(diǎn)擊通知,跳轉(zhuǎn)至指定的Ability

Intent intent1 = new Intent();

// 指定要啟動(dòng)的Ability的BundleName和AbilityName字段

Operation operation = new Intent.OperationBuilder()

.withDeviceId(“”)

.withBundleName(“com.example.myapplication”)

.withAbilityName(“com.example.myapplication.SecondAbility”)

.build();

// 將Operation對(duì)象設(shè)置到Intent中

intent1.setOperation(operation);

List《Intent》 intentList = new ArrayList《》();

intentList.add(intent1);

// 定義請(qǐng)求碼int requestCode = 200;

// 設(shè)置flags

List《IntentAgentConstant.Flags》 flags = new ArrayList《》();

flags.add(IntentAgentConstant.Flags.UPDATE_PRESENT_FLAG);

// 指定啟動(dòng)一個(gè)有頁面的Ability

IntentAgentInfo paramsInfo = new IntentAgentInfo(requestCode,

IntentAgentConstant.OperationType.START_ABILITY, flags, intentList, null);

// 獲取IntentAgent實(shí)例

IntentAgent agent = IntentAgentHelper.getIntentAgent(context, paramsInfo);

//5.設(shè)置通知的IntentAgent

request.setIntentAgent(agent);

實(shí)現(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對(duì)象

slot.setLevel(LEVEL_HIGH);//設(shè)置通知優(yōu)先級(jí)

slot.setDescription(“NotificationSlotDescription”);

slot.setEnableVibration(true); // 設(shè)置振動(dòng)提醒

slot.setLockscreenVisibleness(NotificationRequest.VISIBLENESS_TYPE_PUBLIC);//設(shè)置鎖屏模式

slot.setEnableLight(true); // 設(shè)置開啟呼吸燈提醒

slot.setLedLightColor(Color.RED.getValue());// 設(shè)置呼吸燈的提醒顏色

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); // 設(shè)置通知的內(nèi)容

request.setTapDismissed(!tapDismissed);//設(shè)置用戶點(diǎn)擊通知后自動(dòng)消失

//4.返回應(yīng)用,首先獲取IntentAgent

Intent intent1 = new Intent();

// 指定要啟動(dòng)的Ability的BundleName和AbilityName字段

Operation operation = new Intent.OperationBuilder()

.withDeviceId(“”)

.withBundleName(“com.example.myapplication”)

.withAbilityName(“com.example.myapplication.SecondAbility”)

.build();

// 將Operation對(duì)象設(shè)置到Intent中

intent1.setOperation(operation);

List《Intent》 intentList = new ArrayList《》();

intentList.add(intent1);

// 定義請(qǐng)求碼

int requestCode = 200;

// 設(shè)置flags

List《IntentAgentConstant.Flags》 flags = new ArrayList《》();

flags.add(IntentAgentConstant.Flags.UPDATE_PRESENT_FLAG);

// 指定啟動(dòng)一個(gè)有頁面的Ability

IntentAgentInfo paramsInfo = new IntentAgentInfo(requestCode,

IntentAgentConstant.OperationType.START_ABILITY, flags, intentList, null);

// 獲取IntentAgent實(shí)例

IntentAgent agent = IntentAgentHelper.getIntentAgent(context, paramsInfo);

//5.設(shè)置通知的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);

//通知標(biāo)題

String title =“測(cè)試通知”;

//通知內(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》

責(zé)任編輯:haq

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

    關(guān)注

    183

    文章

    2627

    瀏覽量

    65784
  • HarmonyOS
    +關(guān)注

    關(guān)注

    79

    文章

    1946

    瀏覽量

    29736

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

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

收藏 人收藏

    評(píng)論

    相關(guān)推薦

    關(guān)于BLE通知值的通知長(zhǎng)度問題求解

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

    最新開源代碼證實(shí)!“鴻蒙原生版”微信正在積極開發(fā)

    遷移到另一個(gè)操作系統(tǒng)平臺(tái)的時(shí)間和成本會(huì)非常高。 目前看來,微信的鴻蒙原生版確實(shí)正在開發(fā),并且已經(jīng)取得了一定的進(jìn)展。這對(duì)于鴻蒙操作系統(tǒng)的推廣
    發(fā)表于 05-08 17:08

    鴻蒙系統(tǒng)三防平板怎么樣

    方便地進(jìn)行操作。 在實(shí)際應(yīng)用,鴻蒙系統(tǒng)三防平板表現(xiàn)出色。無論是在戶外探險(xiǎn)中提供導(dǎo)航、拍攝等功能,還是在工地作業(yè)協(xié)助進(jìn)行數(shù)據(jù)分析、圖紙繪
    發(fā)表于 04-12 14:26

    鴻蒙OS開發(fā)學(xué)習(xí):【尺寸適配實(shí)現(xiàn)

    鴻蒙開發(fā),尺寸適配是一個(gè)重要的概念,它可以幫助我們?cè)诓煌聊怀叽绲脑O(shè)備上正確顯示和布局我們的應(yīng)用程序。本文將介紹如何在鴻蒙開發(fā)實(shí)現(xiàn)尺寸
    的頭像 發(fā)表于 04-10 16:05 ?1463次閱讀
    <b class='flag-5'>鴻蒙</b>OS開發(fā)學(xué)習(xí):【尺寸適配<b class='flag-5'>實(shí)現(xiàn)</b>】

    鴻蒙OS開發(fā)實(shí)例:【通知消息】

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

    鴻蒙實(shí)戰(zhàn)項(xiàng)目開發(fā):【短信服務(wù)】

    ||---SmsModel.ets// 封裝短信類 具體實(shí)現(xiàn) 發(fā)送短信功能在SmsModel : /* * Copyright (c) 2022 Huawei Device Co., Ltd.
    發(fā)表于 03-03 21:29

    華為鴻蒙系統(tǒng)怎么樣 華為鴻蒙系統(tǒng)和安卓系統(tǒng)的區(qū)別

    華為鴻蒙系統(tǒng)是華為公司自主研發(fā)的全場(chǎng)景分布式操作系統(tǒng),于2019年8月首次發(fā)布。鴻蒙系統(tǒng)不同于傳統(tǒng)的操作
    的頭像 發(fā)表于 02-02 14:54 ?1346次閱讀

    如何在鴻蒙系統(tǒng)上安裝Google Play

    。但是,通過以下簡(jiǎn)易步驟仍然可以在鴻蒙系統(tǒng)上安裝Google Play。 了解鴻蒙系統(tǒng)和Google Play之間的不兼容性 鴻蒙
    的頭像 發(fā)表于 01-31 17:13 ?1.3w次閱讀

    鴻蒙OS和開源鴻蒙什么關(guān)系?

    開源鴻蒙(Open Harmony) 鴻蒙系統(tǒng)愿來的設(shè)計(jì)初衷,就是讓所有設(shè)備都可以運(yùn)行一個(gè)系統(tǒng),但是每個(gè)設(shè)備的運(yùn)算能力和功能都不同,所以內(nèi)核
    的頭像 發(fā)表于 01-30 15:44 ?825次閱讀
    <b class='flag-5'>鴻蒙</b>OS和開源<b class='flag-5'>鴻蒙</b>什么關(guān)系?

    鴻蒙系統(tǒng)和安卓的區(qū)別 鴻蒙系統(tǒng)有什么特別之處

    了分布式架構(gòu),可以在不同設(shè)備上實(shí)現(xiàn)無縫連接和協(xié)同工作。而安卓系統(tǒng)采用的是集中式架構(gòu),設(shè)備之間的連接和協(xié)同工作相對(duì)較為困難。 鴻蒙系統(tǒng)具備高度靈活性和可擴(kuò)展性,支持設(shè)備與設(shè)備之間的直接通
    的頭像 發(fā)表于 01-18 11:45 ?1.1w次閱讀

    鴻蒙原生應(yīng)用/元服務(wù)開發(fā)-消息通知整體說明

    。顯示當(dāng)前正在進(jìn)行的事件,如下載等。HarmonyOS通過ANS(Advanced Notification Service,通知系統(tǒng)服務(wù))對(duì)通知類型的消息進(jìn)行管理,支持多種通知類型,
    發(fā)表于 01-08 15:26

    鴻蒙原生應(yīng)用/元服務(wù)開發(fā)-通知添加行為意圖

    點(diǎn)擊通知欄上的通知,即可觸發(fā)WantAgent的動(dòng)作。 7.最終效果呈現(xiàn)如下圖,在實(shí)際上架的使用過程 ,API9是沒法實(shí)現(xiàn)分享的作用的,主要原因是其它設(shè)備不一定完成了
    發(fā)表于 01-05 15:07

    鴻蒙原生應(yīng)用/元服務(wù)開發(fā)-發(fā)布基礎(chǔ)類型通知類型與接口

    基礎(chǔ)類型通知主要應(yīng)用于發(fā)送短信息、提示信息、廣告推送等,支持普通文本類型、長(zhǎng)文本類型、多行文本類型和圖片類型。 表 基礎(chǔ)類型通知的內(nèi)容分類 目前系統(tǒng)
    發(fā)表于 01-03 14:46

    華為鴻蒙系統(tǒng)

    操作系統(tǒng),創(chuàng)造一個(gè)超級(jí)虛擬終端互聯(lián)的世界,將人、設(shè)備、場(chǎng)景有機(jī)地聯(lián)系在一起,將消費(fèi)者在全場(chǎng)景生活接觸的多種智能終端,實(shí)現(xiàn)極速發(fā)現(xiàn)、極速連接、硬件互助、資源共享,用合適的設(shè)備提供場(chǎng)景體驗(yàn)。^ [1-2
    發(fā)表于 11-02 19:39

    鴻蒙操作系統(tǒng)的前世今生

    上層接口和分布式能力的統(tǒng)一。 借助以上優(yōu)勢(shì), 鴻蒙操作系統(tǒng)實(shí)現(xiàn)不同終端設(shè)備之間的極速連接、硬件互助和資源共享,為不同的群體帶來升級(jí)體驗(yàn): 對(duì)消費(fèi)者而言, 鴻蒙操作
    發(fā)表于 10-08 19:55