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