【中秋國慶不斷更】HarmonyOS對通知類消息的管理與發(fā)布通知(上)
一、 通知概述
通知簡介
應(yīng)用可以通過通知接口發(fā)送通知消息,終端用戶可以通過通知欄查看通知內(nèi)容,也可以點擊通知來打開應(yīng)用。
通知常見的使用場景:
? ● 顯示接收到的短消息、即時消息等。
? ● 顯示應(yīng)用的推送消息,如廣告、版本更新等。
? ● 顯示當(dāng)前正在進行的事件,如下載等。
HarmonyOS通過ANS(Advanced Notification Service,通知系統(tǒng)服務(wù))對通知類型的消息進行管理,支持多種通知類型,如基礎(chǔ)類型通知、進度條類型通知。
通知業(yè)務(wù)流程
通知業(yè)務(wù)流程由通知子系統(tǒng)、通知發(fā)送端、通知訂閱端組成。
一條通知從通知發(fā)送端產(chǎn)生,通過IPC通信發(fā)送到通知子系統(tǒng),再由通知子系統(tǒng)分發(fā)給通知訂閱端。
系統(tǒng)應(yīng)用還支持通知相關(guān)配置,如使能開關(guān)、配置參數(shù)由系統(tǒng)配置發(fā)起請求,發(fā)送到通知子系統(tǒng)存儲到內(nèi)存和數(shù)據(jù)庫。
二、發(fā)布基礎(chǔ)類型通知
基礎(chǔ)類型通知主要應(yīng)用于發(fā)送短信息、提示信息、廣告推送等,支持普通文本類型、長文本類型、多行文本類型和圖片類型。
表1 基礎(chǔ)類型通知中的內(nèi)容分類
類型 | 描述 |
---|---|
NOTIFICATION_CONTENT_BASIC_TEXT | 普通文本類型。 |
NOTIFICATION_CONTENT_LONG_TEXT | 長文本類型。 |
NOTIFICATION_CONTENT_MULTILINE | 多行文本類型。 |
NOTIFICATION_CONTENT_PICTURE | 圖片類型。 |
目前系統(tǒng)僅通知欄訂閱了通知,將通知顯示在通知欄里?;A(chǔ)類型通知呈現(xiàn)效果示意圖如下所示。
圖1 基礎(chǔ)類型通知呈現(xiàn)效果示意圖
接口說明
通知發(fā)布接口如下表所示,不同發(fā)布類型通知由NotificationRequest的字段攜帶不同的信息。
接口名 | 描述 |
---|---|
publish(request: NotificationRequest, callback: AsyncCallback |
發(fā)布通知。 |
cancel(id: number, label: string, callback: AsyncCallback |
取消指定的通知。 |
cancelAll(callback: AsyncCallback |
取消所有該應(yīng)用發(fā)布的通知。 |
開發(fā)步驟
? 1. 導(dǎo)入模塊。
import NotificationManager from '@ohos.notificationManager';
2.構(gòu)造NotificationRequest對象,并發(fā)布通知。
? ● 普通文本類型通知由標(biāo)題、文本內(nèi)容和附加信息三個字段組成,其中標(biāo)題和文本內(nèi)容是必填字段。
let notificationRequest = {
id: 1,
content: {
contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, // 普通文本類型通知
normal: {
title: 'test_title',
text: 'test_text',
additionalText: 'test_additionalText',
}
}
}
NotificationManager.publish(notificationRequest, (err) => {
if (err) {
console.error(`[ANS] failed to publish, error[${err}]`);
return;
}
console.info(`[ANS] publish success`);
});
運行效果如下圖所示。
? ● 長文本類型通知繼承了普通文本類型的字段,同時新增了長文本內(nèi)容、內(nèi)容概要和通知展開時的標(biāo)題。通知默認顯示與普通文本相同,展開后,標(biāo)題顯示為展開后標(biāo)題內(nèi)容,內(nèi)容為長文本內(nèi)容。
let notificationRequest = {
id: 1,
content: {
contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_LONG_TEXT, // 長文本類型通知
longText: {
title: 'test_title',
text: 'test_text',
additionalText: 'test_additionalText',
longText: 'test_longText',
briefText: 'test_briefText',
expandedTitle: 'test_expandedTitle',
}
}
}
// 發(fā)布通知
NotificationManager.publish(notificationRequest, (err) => {
if (err) {
console.error(`[ANS] failed to publish, error[${err}]`);
return;
}
console.info(`[ANS] publish success`);
});
運行效果如下圖所示。
? ● 多行文本類型通知繼承了普通文本類型的字段,同時新增了多行文本內(nèi)容、內(nèi)容概要和通知展開時的標(biāo)題。通知默認顯示與普通文本相同,展開后,標(biāo)題顯示為展開后標(biāo)題內(nèi)容,多行文本內(nèi)容多行顯示。
let notificationRequest = {
id: 1,
content: {
contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_MULTILINE, // 多行文本類型通知
multiLine: {
title: 'test_title',
text: 'test_text',
briefText: 'test_briefText',
longTitle: 'test_longTitle',
lines: ['line_01', 'line_02', 'line_03', 'line_04'],
}
}
}
// 發(fā)布通知
NotificationManager.publish(notificationRequest, (err) => {
if (err) {
console.error(`[ANS] failed to publish, error[${err}]`);
return;
}
console.info(`[ANS] publish success`);
});
運行效果如下圖所示。
通知繼承了普通文本類型的字段,同時新增了圖片內(nèi)容、內(nèi)容概要和通知展開時的標(biāo)題,圖片內(nèi)容為PixelMap型對象,其大小不能超過2M。
// 圖片構(gòu)造
const color = new ArrayBuffer(60000);
let bufferArr = new Uint8Array(color);
for (var i = 0; i<bufferArr.byteLength;i++) {
bufferArr[i++] = 60;
bufferArr[i++] = 20;
bufferArr[i++] = 220;
bufferArr[i] = 100;
}
let opts = { editable:true, pixelFormat:"ARGB_8888", size: {height:100, width : 150}};
await image
.createPixelMap(color, opts)
.then(async (pixelmap) => {
await pixelmap.getImageInfo().then(imageInfo => {
console.log("=====size: ====" + JSON.stringify(imageInfo.size));
}).catch(err => {
console.error("Failed to obtain the image pixel map information." + JSON.stringify(err));
return;
})
let notificationRequest = {
id: 1,
content: {
contentType: notify.ContentType.NOTIFICATION_CONTENT_PICTURE,
picture: {
title: 'test_title',
text: 'test_text',
additionalText: 'test_additionalText',
picture: pixelmap,
briefText: 'test_briefText',
expandedTitle: 'test_expandedTitle',
}
},
}
// 發(fā)送通知
NotificationManager.publish(notificationRequest, (err) => {
if (err) {
console.error(`[ANS] failed to publish, error[${err}]`);
return;
}
console.info(`[ANS] publish success `);
});
}).catch(err=>{
console.error('create pixelmap failed =========='+ JSON.stringify(err));
return;
})
運行效果如下圖所示。文章來源:http://www.zghlxwxcb.cn/news/detail-710339.html
本文由博客一文多發(fā)平臺 OpenWrite 發(fā)布!文章來源地址http://www.zghlxwxcb.cn/news/detail-710339.html
到了這里,關(guān)于【中秋國慶不斷更】HarmonyOS對通知類消息的管理與發(fā)布通知(上)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!