HarmonyOS - 實(shí)現(xiàn)消息通知功能

??想了解更多關(guān)于開(kāi)源的內(nèi)容,請(qǐng)?jiān)L問(wèn):??
??51CTO 開(kāi)源基礎(chǔ)軟件社區(qū)??
前言
通知是手機(jī)軟件的消息推送,一般需要設(shè)置通知的權(quán)限為允許通知才能在狀態(tài)欄查看到通知。主要有以下使用場(chǎng)景:
- app內(nèi)的通知:如微信新消息的提醒,以及一些APP廣告的推送,APP版本更新。
- 系統(tǒng)的通知,如電量過(guò)低,短信提醒等。
- 顯示正在進(jìn)行的事件,如音樂(lè)播放,下載等都是通知。
效果展示

實(shí)現(xiàn)步驟
1、定義觸發(fā)通知的事件
(1)首先需要定義UI
(一般情況下,不需要UI,本實(shí)例為了能方便獲取觸發(fā)事件而定義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)實(shí)現(xiàn)JS FA調(diào)用PA的邏輯,并實(shí)現(xiàn)點(diǎn)擊事件
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;
},
//調(diào)用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、實(shí)現(xiàn)通知的邏輯
(1)實(shí)現(xiàn)onRemoteRequest()方法
在工程中新建一個(gè)InternalAbility繼承自AceInternalAbility,實(shí)現(xiàn)onRemoteRequest()方法。
/*
* 當(dāng)JS側(cè)調(diào)用FeatureAbility.callAbility(OBJECT)接口時(shí)調(diào)用此方法,通過(guò)JS傳來(lái)的指令執(zhí)行對(duì)應(yīng)的函數(shù)。
* */
public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) {
String result = data.readString();
switch (code) {
case 0x1001:
startTextNotification(reply);//文本類(lèi)型的通知
break;
case 0x1002:
startPictureNotification(reply);//圖片類(lèi)型的通知
break;
case 0x1003:
cancelNotification(reply);//取消通知
break;
default:
reply.writeString("服務(wù)沒(méi)有定義");//若是沒(méi)有對(duì)應(yīng)命令則回復(fù)
return false;
}
return true;
}
(2)在MainAbility中注冊(cè)與取消注冊(cè)
public void onStart(Intent intent) {
super.onStart(intent);
NotificationAbility.register(this);//當(dāng)MainAbility創(chuàng)建的時(shí)候注冊(cè)
}
public void onStop() {
super.onStop();
NotificationAbility.deRegister();//當(dāng)Ability銷(xiāo)毀的時(shí)候注銷(xiāo)
}
(3)通知開(kāi)發(fā)步驟
通知相關(guān)基礎(chǔ)類(lèi)包含NotificationSlot、NotificationRequest和NotificationHelper。
NotificationSlot可以對(duì)提示音、振動(dòng)、重要級(jí)別等進(jìn)行設(shè)置。一個(gè)應(yīng)用可以創(chuàng)建一個(gè)或多個(gè)NotificationSlot,在發(fā)布通知時(shí),通過(guò)綁定不同的NotificationSlot,實(shí)現(xiàn)不同用途。NotificationRequest用于設(shè)置具體的通知對(duì)象,包括設(shè)置通知的屬性,如:通知的分發(fā)時(shí)間、小圖標(biāo)、大圖標(biāo)、自動(dòng)刪除等參數(shù),以及設(shè)置具體的通知類(lèi)型,如普通文本、長(zhǎng)文本等。NotificationHelper封裝了發(fā)布、更新、刪除通知等靜態(tài)方法。在這里主要通過(guò)介紹文本消息通知和圖片消息通知。
定義通知類(lèi)型并設(shè)置基本屬性內(nèi)容
設(shè)置文本通知的頭部文本,通知標(biāo)題,通知的內(nèi)容。
//1.設(shè)置通知的類(lèi)型以及設(shè)置通知的標(biāo)題,正文等屬性
NotificationRequest.NotificationNormalContent normalContent
= new NotificationRequest.NotificationNormalContent();
normalContent.setTitle("文本消息通知");//設(shè)置通知的標(biāo)題
normalContent.setAdditionalText("頭部文本");//設(shè)置通知的頭部文本
normalContent.setText("這是一個(gè)文本消息通知");//設(shè)置通知的正文內(nèi)容
設(shè)置圖片通知的頭部文本,通知標(biāo)題,通知的簡(jiǎn)短介紹,通知圖片。
pictureContent.setTitle("notifiction");
PixelMap pixelMap = getPixMap();
pictureContent.setBigPicture(pixelMap);//設(shè)置通知展示圖片
pictureContent.setAdditionalText("這是一個(gè)圖片通知");//設(shè)置通知的頭部文本
pictureContent.setBriefText("對(duì)于通知的簡(jiǎn)介");//設(shè)置通知的簡(jiǎn)要介紹
定義通知的響應(yīng)按鈕
如果響應(yīng)的按鈕為文本則需要設(shè)置builder的第一個(gè)參數(shù)為null,若響應(yīng)的按鈕為圖片則需要設(shè)置builder的第一個(gè)參數(shù)為PixelMap對(duì)象。
//2.設(shè)置通知的響應(yīng)按鈕
IntentAgent intentAgent = setIntentAgent();
NotificationActionButton actionButton = new NotificationActionButton.Builder(null,
"回復(fù)", intentAgent)//設(shè)置回復(fù)按鈕文本內(nèi)容以及設(shè)置回復(fù)的action
.addNotificationUserInput(
new NotificationUserInput.Builder("QUICK_NOTIFICATION_REPLY")
.setTag("輸入文本").build())//設(shè)置回復(fù)消息的tag
.setSemanticActionButton(NotificationConstant.SemanticActionButton.ARCHIVE_ACTION_BUTTON)
.setAutoCreatedReplies(false)
.build();
NotificationRequest設(shè)置
通過(guò)NotificationRequest對(duì)象對(duì)消息進(jìn)行封裝,設(shè)置通知內(nèi)容,id以及回復(fù)按鈕。
NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(
normalContent);//將normalContent作為參數(shù)傳給NotificationRequest對(duì)象
NotificationRequest notificationRequest = new NotificationRequest(100);//設(shè)置通知id
notificationRequest.setContent(notificationContent);//notificationRequest對(duì)象設(shè)置通知內(nèi)容
notificationRequest.addActionButton(actionButton);//將回復(fù)動(dòng)作按鈕添加進(jìn)notificationRequest
發(fā)布通知
(發(fā)布通知后手機(jī)狀態(tài)欄會(huì)有通知信息顯示)。
通過(guò)調(diào)用NotificationHelper的publishNotification(NotificationRequest notificationRequest)。
NotificationHelper.publishNotification(notificationRequest);
取消通知
(取消通知后通知會(huì)從手機(jī)狀態(tài)欄消失)。
通過(guò)調(diào)用NotificationHelper的cancelNotification(notification id)方法來(lái)實(shí)現(xiàn),通過(guò)notificationid來(lái)辨別通知。
NotificationHelper.cancelNotification(100);
其他功能
若想對(duì)通知的提示音,振動(dòng),重要級(jí)別等進(jìn)行設(shè)置,需要用到NotificationSlot對(duì)象,需要在發(fā)布前就對(duì)其進(jìn)行設(shè)置。
其主要接口如下表。
接口名 | 描述 |
NotificationSlot(String id, String name, int level) | 構(gòu)造NotificationSlot。 |
setLevel(int level) | 設(shè)置NotificationSlot的級(jí)別。 |
setName(String name) | 設(shè)置NotificationSlot的命名。 |
setDescription(String description) | 設(shè)置NotificationSlot的描述信息。 |
enableBypassDnd(boolean bypassDnd) | 設(shè)置是否繞過(guò)系統(tǒng)的免打擾模式。 |
setEnableVibration(boolean vibration) | 設(shè)置收到通知時(shí)是否使能振動(dòng)。 |
setEnableLight(boolean isLightEnabled) | 設(shè)置收到通知時(shí)是否開(kāi)啟呼吸燈,前提是當(dāng)前硬件支持呼吸燈。 |
setLedLightColor(int color) | 設(shè)置收到通知時(shí)的呼吸燈顏色。 |
注意:這個(gè)對(duì)象只有在真機(jī)上才有真實(shí)效果。
總結(jié)
以上就是開(kāi)發(fā)一個(gè)消息通知的完整過(guò)程,對(duì)于消息通知的應(yīng)用是一個(gè)APP必不可少的部分,是APP與用戶交互的一個(gè)通道。
??想了解更多關(guān)于開(kāi)源的內(nèi)容,請(qǐng)?jiān)L問(wèn):??





























