HarmonyOS - 實現消息通知功能
前言
通知是手機軟件的消息推送,一般需要設置通知的權限為允許通知才能在狀態欄查看到通知。主要有以下使用場景:
- app內的通知:如微信新消息的提醒,以及一些APP廣告的推送,APP版本更新。
- 系統的通知,如電量過低,短信提醒等。
- 顯示正在進行的事件,如音樂播放,下載等都是通知。
效果展示
實現步驟
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中注冊與取消注冊
public void onStart(Intent intent) {
super.onStart(intent);
NotificationAbility.register(this);//當MainAbility創建的時候注冊
}
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與用戶交互的一個通道。