成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

HarmonyOS - 實現消息通知功能

系統 OpenHarmony
對于消息通知的應用是一個APP必不可少的部分,是APP與用戶交互的一個通道。

??想了解更多關于開源的內容,請訪問:??

??51CTO 開源基礎軟件社區??

??https://ost.51cto.com??

前言

通知是手機軟件的消息推送,一般需要設置通知的權限為允許通知才能在狀態欄查看到通知。主要有以下使用場景:

  1. app內的通知:如微信新消息的提醒,以及一些APP廣告的推送,APP版本更新。
  2. 系統的通知,如電量過低,短信提醒等。
  3. 顯示正在進行的事件,如音樂播放,下載等都是通知。

效果展示

#夏日挑戰賽# HarmonyOS - 實現消息通知功能-開源基礎軟件社區

實現步驟

1、定義觸發通知的事件

(1)首先需要定義UI

(一般情況下,不需要UI,本實例為了能方便獲取觸發事件而定義UI)。

<!--文本通知按鈕-->
<button class="button_notification" onclick="clickStartInputNotification">
{{$t('strings.startInputNotifiction')}}
</button>
<!--圖片通知按鈕-->
<button class="button_notification" onclick="clickStartButtonNotifiction">
{{$t('strings.startButtonNotifiction')}}
</button>
<!--取消通知-->
<button class="button_notification" onclick="clickCancelNotification">
{{$t('strings.cancelNotifiction')}}
</button>

(2)實現JS FA調用PA的邏輯,并實現點擊事件

import prompt from '@system.prompt';
export default {
//文本通知
clickStartInputNotification:function(){
this.showToast("clickStartInputNotification");
this.notification(0x1001);
},
//圖片通知
clickStartButtonNotifiction:function(){
this.showToast("clickStartButtonNotifiction");
this.notification(0x1002);
},
//取消通知
clickCancelNotification:function(){
this.showToast("clickCancelNotification");
this.notification(0x1003);
},
//初始化action
initAction: function (code) {
var actionData = {};
actionData.notify = "this actionData form JS ";
var action = {};
action.bundleName = "com.chinasoft.example";
action.abilityName = "NotificationAbility";
action.messageCode = code;
action.data = actionData;
action.abilityType = 1;
action.syncOption = 0;
return action;
},
//調用PA
notification: async function(code) {
try {
var action = this.initAction(code);
var result = await FeatureAbility.callAbility(action);
console.info(" result = " + result);
this.showToast(result);
} catch (pluginError) {
console.error("startNotification : Plugin Error = " + pluginError);
}
},
}

2、實現通知的邏輯

(1)實現onRemoteRequest()方法

在工程中新建一個InternalAbility繼承自AceInternalAbility,實現onRemoteRequest()方法。

/*
* 當JS側調用FeatureAbility.callAbility(OBJECT)接口時調用此方法,通過JS傳來的指令執行對應的函數。
* */
public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) {
String result = data.readString();
switch (code) {
case 0x1001:
startTextNotification(reply);//文本類型的通知
break;
case 0x1002:
startPictureNotification(reply);//圖片類型的通知
break;
case 0x1003:
cancelNotification(reply);//取消通知
break;
default:
reply.writeString("服務沒有定義");//若是沒有對應命令則回復
return false;
}
return true;
}

(2)在MainAbility中注冊與取消注冊

@Override
public void onStart(Intent intent) {
super.onStart(intent);
NotificationAbility.register(this);//當MainAbility創建的時候注冊
}
@Override
public void onStop() {
super.onStop();
NotificationAbility.deRegister();//當Ability銷毀的時候注銷
}

(3)通知開發步驟

通知相關基礎類包含NotificationSlot、NotificationRequest和NotificationHelper。

NotificationSlot可以對提示音、振動、重要級別等進行設置。一個應用可以創建一個或多個NotificationSlot,在發布通知時,通過綁定不同的NotificationSlot,實現不同用途。NotificationRequest用于設置具體的通知對象,包括設置通知的屬性,如:通知的分發時間、小圖標、大圖標、自動刪除等參數,以及設置具體的通知類型,如普通文本、長文本等。NotificationHelper封裝了發布、更新、刪除通知等靜態方法。在這里主要通過介紹文本消息通知和圖片消息通知。

定義通知類型并設置基本屬性內容

設置文本通知的頭部文本,通知標題,通知的內容。

//1.設置通知的類型以及設置通知的標題,正文等屬性
NotificationRequest.NotificationNormalContent normalContent
= new NotificationRequest.NotificationNormalContent();
normalContent.setTitle("文本消息通知");//設置通知的標題
normalContent.setAdditionalText("頭部文本");//設置通知的頭部文本
normalContent.setText("這是一個文本消息通知");//設置通知的正文內容

設置圖片通知的頭部文本,通知標題,通知的簡短介紹,通知圖片。

pictureContent.setTitle("notifiction");
PixelMap pixelMap = getPixMap();
pictureContent.setBigPicture(pixelMap);//設置通知展示圖片
pictureContent.setAdditionalText("這是一個圖片通知");//設置通知的頭部文本
pictureContent.setBriefText("對于通知的簡介");//設置通知的簡要介紹

定義通知的響應按鈕

如果響應的按鈕為文本則需要設置builder的第一個參數為null,若響應的按鈕為圖片則需要設置builder的第一個參數為PixelMap對象。

//2.設置通知的響應按鈕
IntentAgent intentAgent = setIntentAgent();
NotificationActionButton actionButton = new NotificationActionButton.Builder(null,
"回復", intentAgent)//設置回復按鈕文本內容以及設置回復的action
.addNotificationUserInput(
new NotificationUserInput.Builder("QUICK_NOTIFICATION_REPLY")
.setTag("輸入文本").build())//設置回復消息的tag
.setSemanticActionButton(NotificationConstant.SemanticActionButton.ARCHIVE_ACTION_BUTTON)
.setAutoCreatedReplies(false)
.build();

NotificationRequest設置

通過NotificationRequest對象對消息進行封裝,設置通知內容,id以及回復按鈕。

NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(
normalContent);//將normalContent作為參數傳給NotificationRequest對象
NotificationRequest notificationRequest = new NotificationRequest(100);//設置通知id
notificationRequest.setContent(notificationContent);//notificationRequest對象設置通知內容
notificationRequest.addActionButton(actionButton);//將回復動作按鈕添加進notificationRequest

發布通知

(發布通知后手機狀態欄會有通知信息顯示)。

通過調用NotificationHelper的publishNotification(NotificationRequest notificationRequest)。

NotificationHelper.publishNotification(notificationRequest);

取消通知

(取消通知后通知會從手機狀態欄消失)。

通過調用NotificationHelper的cancelNotification(notification id)方法來實現,通過notificationid來辨別通知。

NotificationHelper.cancelNotification(100);

其他功能

若想對通知的提示音,振動,重要級別等進行設置,需要用到NotificationSlot對象,需要在發布前就對其進行設置。

其主要接口如下表。

接口名

描述

NotificationSlot(String id, String name, int level)

構造NotificationSlot。

setLevel(int level)

設置NotificationSlot的級別。

setName(String name)

設置NotificationSlot的命名。

setDescription(String description)

設置NotificationSlot的描述信息。

enableBypassDnd(boolean bypassDnd)

設置是否繞過系統的免打擾模式。

setEnableVibration(boolean vibration)

設置收到通知時是否使能振動。

setEnableLight(boolean isLightEnabled)

設置收到通知時是否開啟呼吸燈,前提是當前硬件支持呼吸燈。

setLedLightColor(int color)

設置收到通知時的呼吸燈顏色。

注意:這個對象只有在真機上才有真實效果。

總結

以上就是開發一個消息通知的完整過程,對于消息通知的應用是一個APP必不可少的部分,是APP與用戶交互的一個通道。

??想了解更多關于開源的內容,請訪問:??

??51CTO 開源基礎軟件社區??

??https://ost.51cto.com??。

責任編輯:jianghua 來源: 鴻蒙社區
相關推薦

2025-05-29 01:33:00

微服務架構系統

2017-03-16 08:46:57

延時消息環形隊列數據結構

2022-07-12 17:33:00

消息定時提醒鴻蒙

2022-07-28 14:31:04

canvas鴻蒙

2017-04-07 10:30:34

Windows 10Windows消息通知

2011-05-06 15:00:52

Service BroSQL Server

2021-08-04 10:22:27

鴻蒙HarmonyOS應用

2024-01-31 09:42:11

RabbitMQ消息隊列.NET

2011-10-19 09:30:23

jQuery

2022-06-02 14:27:05

UI框架JS

2025-03-31 08:39:55

2017-03-20 09:50:35

消息隊列架構消息

2011-07-18 13:56:19

2023-10-27 16:15:35

鴻蒙天氣服務功能

2011-07-22 16:47:53

iOS 通知 xcode

2021-02-19 09:17:48

微軟Edge瀏覽器

2015-05-11 10:57:01

Android M谷歌

2011-07-22 16:57:44

iOS 通知

2010-01-26 10:38:56

Android消息傳遞

2020-09-21 14:37:09

Python代碼微信
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 女同av亚洲女人天堂 | 麻豆精品一区二区三区在线观看 | 在线高清免费观看视频 | 99精品免费久久久久久久久日本 | 日韩中文在线 | 国产精品欧美一区二区 | 久久夜视频 | 在线国产视频 | av乱码 | 狠狠色狠狠色综合日日92 | 美女久久久| 国产一级免费在线观看 | 亚洲成人精品在线 | 国产偷录视频叫床高潮对白 | 97操操| 男人影音| 久久久久久久久久一区二区 | 欧美性极品xxxx做受 | 亚洲色欲色欲www | 久久精品91久久久久久再现 | 国产日韩欧美一区 | 日日夜夜操天天干 | 亚洲传媒在线 | 亚洲激情网站 | 色播视频在线观看 | 在线观看亚洲专区 | 欧美一区二区三区四区视频 | 久久久久国产 | 日韩三级在线观看 | 精品免费在线 | 日韩三级免费观看 | 日韩精品一区二区三区中文在线 | 久久久www成人免费精品张筱雨 | 99精品免费 | 黄色av网站在线免费观看 | 亚洲在线一区二区 | 国产精品毛片av一区 | 中文字幕在线看 | 一级毛片成人免费看a | 亚洲欧美国产精品久久 | 日韩欧美国产电影 |