004 鸿蒙应用开发-通知栏
目錄
一.通知概述
通知簡介
通知業(yè)務流程
廣播的類型
接口說明
開發(fā)前期準備
二.發(fā)送普通文本類型通知
1.先初始化廣播的請求request
2.然后發(fā)送廣播
3.顯示效果如下
三.發(fā)送長文本類型廣播
1.構建發(fā)送廣播的參數(shù)request
2.然后發(fā)送廣播
3.顯示效果如下
注意事項
四.發(fā)送多行文本類型廣播
1.構建發(fā)送廣播的參數(shù)request
2.發(fā)送廣播
3.顯示效果
注意事項
五.發(fā)送圖片類型廣播
代碼
顯示效果
六.發(fā)送意圖類型廣播
1.創(chuàng)建wantAgent字段
2.構建發(fā)送廣播的參數(shù)request
3.發(fā)送廣播
4.顯示結果
一.通知概述
通知簡介
應用可以通過通知接口發(fā)送通知消息,終端用戶可以通過通知欄查看通知內容,也可以點擊通知來打開應用。
通知常見的使用場景:
-
顯示接收到的短消息、即時消息等。
-
顯示應用的推送消息,如廣告、版本更新等。
-
顯示當前正在進行的事件,如下載等。
HarmonyOS通過ANS(Advanced Notification Service,通知系統(tǒng)服務)對通知類型的消息進行管理,支持多種通知類型,如基礎類型通知、進度條類型通知。
通知業(yè)務流程
通知業(yè)務流程由通知子系統(tǒng)、通知發(fā)送端、通知訂閱端組成。
一條通知從通知發(fā)送端產生,通過IPC通信發(fā)送到通知子系統(tǒng),再由通知子系統(tǒng)分發(fā)給通知訂閱端。
系統(tǒng)應用還支持通知相關配置,如使能開關、配置參數(shù)由系統(tǒng)配置發(fā)起請求,發(fā)送到通知子系統(tǒng)存儲到內存和數(shù)據庫。
廣播的類型
- NOTIFICATION_CONTENT_BASIC_TEXT:普通文本類型
- NOTIFICATION_CONTENT_LONG_TEXT:長文本類型
- NOTIFICATION_CONTENT_MULTILINE:多行文本類型
- NOTIFICATION_CONTENT_PICTURE:圖片類型
廣播的類型主要分為普通文本類型,發(fā)送普通的文本廣播;長文本類型,發(fā)送長文本類型的廣播;多行文本類型,可以將文字多行顯示發(fā)送廣播;發(fā)送圖片類型的廣播。
接口說明
通知發(fā)布接口如下表所示,不同發(fā)布類型通知由NotificationRequest的字段攜帶不同的信息。
| publish(request: NotificationRequest, callback: AsyncCallback<void>): void | 發(fā)布通知。 |
| cancel(id: number, label: string, callback: AsyncCallback<void>): void | 取消指定的通知。 |
| cancelAll(callback: AsyncCallback<void>): void; | 取消所有該應用發(fā)布的通知。 |
開發(fā)前期準備
導包
| import?NotificationManager from?'@ohos.notificationManager'; |
二.發(fā)送普通文本類型通知
1.先初始化廣播的請求request
| let notificationRequest = { ??id:?1, ??content: { ????contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,?// 普通文本類型通知 ????normal: { ??????title:?'test_title',?//標題,必選項 ??????text:?'test_text',?//內容,必選項 ??????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.顯示效果如下
點擊發(fā)送普通廣播按鈕后下拉通知欄
三.發(fā)送長文本類型廣播
長文本類型通知繼承了普通文本類型的字段,同時新增了長文本內容、內容概要和通知展開時的標題。通知默認顯示與普通文本相同,展開后,標題顯示為展開后標題內容,內容為長文本內容。
1.構建發(fā)送廣播的參數(shù)request
| let notificationRequestLong = { ??id:?2, ??content: { ????contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_LONG_TEXT,?// 長文本類型通知 ????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ā)送長文本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.顯示效果如下
點擊按鈕后然后下拉通知欄顯示效果
注意事項
- 目前測試發(fā)現(xiàn)長文本要足夠長,如果不夠長則只會顯示出長文本內容,普通文本內容顯示不出來
四.發(fā)送多行文本類型廣播
多行文本類型通知繼承了普通文本類型的字段,同時新增了多行文本內容、內容概要和通知展開時的標題。通知默認顯示與普通文本相同,展開后,標題顯示為展開后標題內容,多行文本內容多行顯示。
1.構建發(fā)送廣播的參數(shù)request
| let notificationRequestLines = { ??id:?3, ??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'], ????} ??} } |
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.顯示效果
點擊后下拉通知欄
注意事項
- 如果文本只有一行,會只顯示出多行文本類型的內容,不顯示普通文本類型的內容
五.發(fā)送圖片類型廣播
圖片類型通知繼承了普通文本類型的字段,同時新增了圖片內容、內容概要和通知展開時的標題,圖片內容為PixelMap型對象,其大小不能超過2M。
代碼
| @Entry @Component struct Index { ??build() { ????Row() { ??????Column() { ????????Button("發(fā)送Image Notification").onClick(_ => { ??????????// 圖片構造 ??????????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ā)送意圖類型廣播
意圖類型的廣播就是發(fā)送后可以點擊并跳轉到頁面的廣播,意圖類型通知繼承了普通文本類型的字段,同時新增了wantAgent字段,此參數(shù)的跳轉到哪個頁面的意思
1.創(chuàng)建wantAgent字段
| // 通過WantAgentInfo的operationType設置動作類型。 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.構建發(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.顯示結果
點擊后下拉通知欄點擊通知欄
總結
以上是生活随笔為你收集整理的004 鸿蒙应用开发-通知栏的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c#实现任务栏气泡提醒
- 下一篇: HTML文字溢出出现.....