【中秋國慶不斷更】HarmonyOS對通知類消息的管理與發佈通知(下) 一、發佈進度條類型通知 進度條通知也是常見的通知類型,主要應用於文件下載、事務處理進度顯示。HarmonyOS提供了進度條模板,發佈通知應用設置好進度條模板的屬性值,如模板名、模板數據,通過通知子系統發送到通知欄顯示。 目前系統 ...
【中秋國慶不斷更】HarmonyOS對通知類消息的管理與發佈通知(下)
一、發佈進度條類型通知
進度條通知也是常見的通知類型,主要應用於文件下載、事務處理進度顯示。HarmonyOS提供了進度條模板,發佈通知應用設置好進度條模板的屬性值,如模板名、模板數據,通過通知子系統發送到通知欄顯示。
目前系統模板僅支持進度條模板,通知模板NotificationTemplate中的data參數為用戶自定義數據,用於顯示與模塊相關的數據,效果示意如下圖所示。
介面說明
isSupportTemplate()是查詢模板是否支持介面,目前僅支持進度條模板。
介面名 | 描述 |
---|---|
isSupportTemplate(templateName: string, callback: AsyncCallback |
查詢模板是否存在。 |
開發步驟
1. 導入模塊。
import NotificationManager from '@ohos.notificationManager';
2. 系統是否支持進度條模板,查詢結果為支持downloadTemplate模板類通知。
NotificationManager.isSupportTemplate('downloadTemplate').then((data) => {
console.info(`[ANS] isSupportTemplate success`);
let isSupportTpl: boolean = data; // isSupportTpl的值為true表示支持支持downloadTemplate模板類通知,false表示不支持
// ...
}).catch((err) => {
console.error(`[ANS] isSupportTemplate failed, error[${err}]`);
});
說明
查詢系統支持進度條模板後,再進行後續的步驟操作。
3. 構造進度條模板對象,併發布通知。
let template = {
name:'downloadTemplate',
data: {
title: '標題:',
fileName: 'music.mp4',
progressValue: 30,
progressMaxValue:100,
}
}
//構造NotificationRequest對象
let notificationRquest = {
id: 1,
slotType: notify.SlotType.OTHER_TYPES,
template: template,
content: {
contentType: notify.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
normal: {
title: template.data.title + template.data.fileName,
text: "sendTemplate",
additionalText: "30%"
}
},
deliveryTime: new Date().getTime(),
showDeliveryTime: true
}
notify.publish(notificationRquest).then(() => {
console.info(`[ANS] publish success `);
}).catch((err) => {
console.error(`[ANS] failed to publish, error[${err}]`);
});
二、為通知添加行為意圖
WantAgent提供了封裝行為意圖的能力,這裡所說的行為意圖主要是指拉起指定的應用組件及發佈公共事件等能力。HarmonyOS支持以通知的形式,將WantAgent從發佈方傳遞至接收方,從而在接收方觸發WantAgent中指定的意圖。例如,在通知消息的發佈者發佈通知時,通常期望用戶可以通過通知欄點擊拉起目標應用組件。為了達成這一目標,開發者可以將WantAgent封裝至通知消息中,當系統接收到WantAgent後,在用戶點擊通知欄時觸發WantAgent的意圖,從而拉起目標應用組件。
為通知添加行為意圖的實現方式如下圖所示:發佈通知的應用嚮應用組件管理服務AMS(Ability Manager Service)申請WantAgent,然後隨其他通知信息一起發送給桌面,當用戶在桌面通知欄上點擊通知時,觸發WantAgent動作。
圖1 攜帶行為意圖的通知運行機制
介面說明
具體介面描述,詳見WantAgent介面文檔。
介面名 | 描述 |
---|---|
getWantAgent(info: WantAgentInfo, callback: AsyncCallback |
創建WantAgent。 |
trigger(agent: WantAgent, triggerInfo: TriggerInfo, callback?: Callback |
觸發WantAgent意圖。 |
cancel(agent: WantAgent, callback: AsyncCallback |
取消WantAgent。 |
getWant(agent: WantAgent, callback: AsyncCallback |
獲取WantAgent的want。 |
equal(agent: WantAgent, otherAgent: WantAgent, callback: AsyncCallback |
判斷兩個WantAgent實例是否相等。 |
開發步驟
1. 導入模塊。
import NotificationManager from '@ohos.notificationManager';
import wantAgent from '@ohos.app.ability.wantAgent';
2.創建WantAgentInfo信息。
場景一:創建拉起Ability的WantAgent的WantAgentInfo信息。
let wantAgentObj = null; // 用於保存創建成功的wantAgent對象,後續使用其完成觸發的動作。
// 通過WantAgentInfo的operationType設置動作類型。
let wantAgentInfo = {
wants: [
{
deviceId: '',
bundleName: 'com.example.test',
abilityName: 'com.example.test.MainAbility',
action: '',
entities: [],
uri: '',
parameters: {}
}
],
operationType: wantAgent.OperationType.START_ABILITY,
requestCode: 0,
wantAgentFlags:[wantAgent.WantAgentFlags.CONSTANT_FLAG]
}
場景二:創建發佈公共事件的WantAgent的WantAgentInfo信息。
let wantAgentObj = null; // 用於保存創建成功的WantAgent對象,後續使用其完成觸發的動作。
// wantAgentInfo
let wantAgentInfo = {
wants: [
{
action: 'event_name', // 設置事件名。
parameters: {},
}
],
operationType: wantAgent.OperationType.SEND_COMMON_EVENT,
requestCode: 0,
wantAgentFlags: [wantAgent.WantAgentFlags.CONSTANT_FLAG],
}
3. 創建WantAgent。
// 創建WantAgent
wantAgent.getWantAgent(wantAgentInfo, (err, data) => {
if (err) {
console.error('[WantAgent]getWantAgent err=' + JSON.stringify(err));
} else {
console.info('[WantAgent]getWantAgent success');
wantAgentObj = data;
}
});
4. 構造NotificationRequest對象。
// 構造NotificationRequest對象
let notificationRequest = {
content: {
contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
normal: {
title: 'Test_Title',
text: 'Test_Text',
additionalText: 'Test_AdditionalText',
},
},
id: 1,
label: 'TEST',
wantAgent: wantAgentObj,
}
5. 發佈WantAgent通知。
// 通知發送
NotificationManager.publish(notificationRequest, (err) => {
if (err) {
console.error(`[ANS] failed to publish, error[${err}]`);
return;
}
console.info(`[ANS] publish success `);
});
6. 用戶通過點擊通知欄上的通知,即可觸發WantAgent的動作。
本文由博客一文多發平臺 OpenWrite 發佈!