一.通知概述
通知簡(jiǎn)介
應(yīng)用可以通過(guò)通知接口發(fā)送通知消息颂碧,終端用戶(hù)可以通過(guò)通知欄查看通知內(nèi)容窟却,也可以點(diǎn)擊通知來(lái)打開(kāi)應(yīng)用剖笙。
通知常見(jiàn)的使用場(chǎng)景:
顯示接收到的短消息、即時(shí)消息等涧窒。
顯示應(yīng)用的推送消息心肪,如廣告、版本更新等纠吴。
顯示當(dāng)前正在進(jìn)行的事件硬鞍,如下載等。
HarmonyOS通過(guò)ANS(Advanced Notification Service呜象,通知系統(tǒng)服務(wù))對(duì)通知類(lèi)型的消息進(jìn)行管理膳凝,支持多種通知類(lèi)型碑隆,如基礎(chǔ)類(lèi)型通知恭陡、進(jìn)度條類(lèi)型通知。
通知業(yè)務(wù)流程
通知業(yè)務(wù)流程由通知子系統(tǒng)上煤、通知發(fā)送端休玩、通知訂閱端組成。
一條通知從通知發(fā)送端產(chǎn)生劫狠,通過(guò)IPC通信發(fā)送到通知子系統(tǒng)拴疤,再由通知子系統(tǒng)分發(fā)給通知訂閱端。
系統(tǒng)應(yīng)用還支持通知相關(guān)配置独泞,如使能開(kāi)關(guān)呐矾、配置參數(shù)由系統(tǒng)配置發(fā)起請(qǐng)求,發(fā)送到通知子系統(tǒng)存儲(chǔ)到內(nèi)存和數(shù)據(jù)庫(kù)懦砂。
廣播的類(lèi)型
NOTIFICATION_CONTENT_BASIC_TEXT:普通文本類(lèi)型
NOTIFICATION_CONTENT_LONG_TEXT:長(zhǎng)文本類(lèi)型
NOTIFICATION_CONTENT_MULTILINE:多行文本類(lèi)型
NOTIFICATION_CONTENT_PICTURE:圖片類(lèi)型
廣播的類(lèi)型主要分為普通文本類(lèi)型蜒犯,發(fā)送普通的文本廣播组橄;長(zhǎng)文本類(lèi)型,發(fā)送長(zhǎng)文本類(lèi)型的廣播罚随;多行文本類(lèi)型玉工,可以將文字多行顯示發(fā)送廣播;發(fā)送圖片類(lèi)型的廣播淘菩。
接口說(shuō)明
通知發(fā)布接口如下表所示遵班,不同發(fā)布類(lèi)型通知由NotificationRequest的字段攜帶不同的信息。
接口名描述
publish(request: NotificationRequest, callback: AsyncCallback<void>): void發(fā)布通知潮改。
cancel(id: number, label: string, callback: AsyncCallback<void>): void取消指定的通知狭郑。
cancelAll(callback: AsyncCallback<void>): void;取消所有該應(yīng)用發(fā)布的通知。
開(kāi)發(fā)前期準(zhǔn)備
導(dǎo)包
import?NotificationManager from?'@ohos.notificationManager';
二.發(fā)送普通文本類(lèi)型通知
1.先初始化廣播的請(qǐng)求request
let notificationRequest = {
??id:?1,
??content: {
????contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,?// 普通文本類(lèi)型通知
????normal: {
??????title:?'test_title',?//標(biāo)題进陡,必選項(xiàng)
??????text:?'test_text',?//內(nèi)容愿阐,必選項(xiàng)
??????additionalText:?'test_additionalText',?//附加信息,非必選
????}
??}
}
2.然后發(fā)送廣播
@Entry
@Component
struct Index {
??build() {
????Row() {
??????Column() {
????????Button("發(fā)送普通Notification").onClick(_ => {
??????????NotificationManager.publish(notificationRequest, (err) => {
????????????if?(err) {
??????????????console.error(`[ANS] failed to publish, error[${err}]`);
??????????????return;
????????????}
????????????console.info(`[ANS] publish success`);
??????????});
????????}).margin({ bottom:?20?})
??????}.alignItems(HorizontalAlign.Start).padding(20)
??????.width('100%')
????}.alignItems(VerticalAlign.Top)
????.height('100%')
??}
}
3.顯示效果如下
點(diǎn)擊發(fā)送普通廣播按鈕后下拉通知欄
三.發(fā)送長(zhǎng)文本類(lèi)型廣播
長(zhǎng)文本類(lèi)型通知繼承了普通文本類(lèi)型的字段趾疚,同時(shí)新增了長(zhǎng)文本內(nèi)容缨历、內(nèi)容概要和通知展開(kāi)時(shí)的標(biāo)題。通知默認(rèn)顯示與普通文本相同糙麦,展開(kāi)后辛孵,標(biāo)題顯示為展開(kāi)后標(biāo)題內(nèi)容,內(nèi)容為長(zhǎng)文本內(nèi)容赡磅。
1.構(gòu)建發(fā)送廣播的參數(shù)request
let notificationRequestLong = {
??id:?2,
??content: {
????contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_LONG_TEXT,?// 長(zhǎng)文本類(lèi)型通知
????longText: {
??????title:?'test_title',
??????text:?'test_text',
??????additionalText:?'test_additionalText',
??????longText:?'test_longTextssssssssssssssssssssssssssssssssssssss',
??????briefText:?'test_briefText',
??????expandedTitle:?'test_expandedTitle',
????}
??}
}
2.然后發(fā)送廣播
@Entry
@Component
struct Index {
??build() {
????Row() {
??????Column() {
????????Button("發(fā)送長(zhǎng)文本Notification").onClick(_ => {
??????????NotificationManager.publish(notificationRequestLong, (err) => {
????????????if?(err) {
??????????????console.error(`[ANS] failed to publish, error[${err}]`);
??????????????return;
????????????}
????????????console.info(`[ANS] publish success`);
??????????});
????????}).margin({ bottom:?20?})
??????}.alignItems(HorizontalAlign.Start).padding(20)
??????.width('100%')
????}.alignItems(VerticalAlign.Top)
????.height('100%')
??}
}
3.顯示效果如下
點(diǎn)擊按鈕后然后下拉通知欄顯示效果
注意事項(xiàng)
目前測(cè)試發(fā)現(xiàn)長(zhǎng)文本要足夠長(zhǎng)魄缚,如果不夠長(zhǎng)則只會(huì)顯示出長(zhǎng)文本內(nèi)容,普通文本內(nèi)容顯示不出來(lái)
四.發(fā)送多行文本類(lèi)型廣播
多行文本類(lèi)型通知繼承了普通文本類(lèi)型的字段焚廊,同時(shí)新增了多行文本內(nèi)容冶匹、內(nèi)容概要和通知展開(kāi)時(shí)的標(biāo)題。通知默認(rèn)顯示與普通文本相同咆瘟,展開(kāi)后嚼隘,標(biāo)題顯示為展開(kāi)后標(biāo)題內(nèi)容,多行文本內(nèi)容多行顯示袒餐。
1.構(gòu)建發(fā)送廣播的參數(shù)request
let notificationRequestLines = {
??id:?3,
??content: {
????contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_MULTILINE,?// 多行文本類(lèi)型通知
????multiLine: {
??????title:?'test_title',
??????text:?'test_text',
??????briefText:?'test_briefText',
??????longTitle:?'test_longTitle',
??????lines: ['line_01',?'line_02',?'line_03',?'line_04'],
????}
??}
}
2.發(fā)送廣播
@Entry
@Component
struct Index {
??build() {
????Row() {
??????Column() {
????????Button("發(fā)送多行Notification").onClick(_ => {
??????????NotificationManager.publish(notificationRequestLines, (err) => {
????????????if?(err) {
??????????????console.error(`[ANS] failed to publish, error[${err}]`);
??????????????return;
????????????}
????????????console.info(`[ANS] publish success`);
??????????});
????????}).margin({ bottom:?20?})
??????}.alignItems(HorizontalAlign.Start).padding(20)
??????.width('100%')
????}.alignItems(VerticalAlign.Top)
????.height('100%')
??}
}
3.顯示效果
注意事項(xiàng)
如果文本只有一行飞蛹,會(huì)只顯示出多行文本類(lèi)型的內(nèi)容,不顯示普通文本類(lèi)型的內(nèi)容
五.發(fā)送圖片類(lèi)型廣播
圖片類(lèi)型通知繼承了普通文本類(lèi)型的字段灸眼,同時(shí)新增了圖片內(nèi)容卧檐、內(nèi)容概要和通知展開(kāi)時(shí)的標(biāo)題,圖片內(nèi)容為PixelMap型對(duì)象焰宣,其大小不能超過(guò)2M霉囚。
代碼
@Entry
@Component
struct Index {
??build() {
????Row() {
??????Column() {
????????Button("發(fā)送Image Notification").onClick(_ => {
??????????// 圖片構(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:image.PixelMapFormat.RGBA_8888,size: {height:100, width :?150}};
??????????image
????????????.createPixelMap(color, opts)
????????????.then( value => {
??????????????value.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: NotificationManager.ContentType.NOTIFICATION_CONTENT_PICTURE,
??????????????????picture: {
????????????????????title:?'test_title',
????????????????????text:?'test_text',
????????????????????additionalText:?'test_additionalText',
????????????????????picture: value,
????????????????????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;
????????????})
????????}).margin({ bottom:?20?})
??????}.alignItems(HorizontalAlign.Start).padding(20)
??????.width('100%')
????}.alignItems(VerticalAlign.Top)
????.height('100%')
??}
}
顯示效果
六.發(fā)送意圖類(lèi)型廣播
意圖類(lèi)型的廣播就是發(fā)送后可以點(diǎn)擊并跳轉(zhuǎn)到頁(yè)面的廣播,意圖類(lèi)型通知繼承了普通文本類(lèi)型的字段匕积,同時(shí)新增了wantAgent字段盈罐,此參數(shù)的跳轉(zhuǎn)到哪個(gè)頁(yè)面的意思
1.創(chuàng)建wantAgent字段
// 通過(guò)WantAgentInfo的operationType設(shè)置動(dòng)作類(lèi)型逻澳。
let wantAgentInfoDisplay = {
??wants: [
??????{
????????deviceId:?'',
????????bundleName:?'com.example.notificationtest',
????????abilityName:?'MainAbility'
??????}
??],
??operationType: wantAgent.OperationType.START_ABILITY,
??requestCode:?0,
??wantAgentFlags: [wantAgent.WantAgentFlags.CONSTANT_FLAG]
}
@Entry
@Component
struct NotificationWantAgent {
??@State?message: string =?'Hello World'
??build() {
????Row() {
??????Column() {
????????Button("意圖通知").onClick(_ => {
??????????// 創(chuàng)建WantAgent
??????????wantAgent.getWantAgent(wantAgentInfoDisplay, (err, data) => {
????????????if?(err) {
??????????????console.error('[WantAgent]getWantAgent err='?+ JSON.stringify(err));
????????????}?else?{
??????????????console.info('[WantAgent]getWantAgent success');
????????????}
??????????});
????????})
??????}.padding(20).alignItems(HorizontalAlign.Start)
??????.width('100%')
????}
????.height('100%').alignItems(VerticalAlign.Top)
??}
}
如上得到的data就是wantAgent參數(shù)
2.構(gòu)建發(fā)送廣播的參數(shù)request
let notificationRequest = {
????????????????content: {
??????????????????contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
??????????????????normal: {
????????????????????title:?'Test_Title',
????????????????????text:?'Test_Text',
????????????????????additionalText:?'Test_AdditionalText',
??????????????????},
????????????????},
????????????????id:?6,
????????????????label:?'TEST',
????????????????wantAgent: data,
??????????????}
3.發(fā)送廣播
// 通知發(fā)送
NotificationManager.publish(notificationRequest, (err) => {
????if?(err) {
????????console.error(`[ANS] failed to publish, error[${err}]`);
????????return;
????}
????console.info(`[ANS] publish success `);
});
4.顯示結(jié)果