日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

004 鸿蒙应用开发-通知栏

發布時間:2024/1/8 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 004 鸿蒙应用开发-通知栏 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

一.通知概述

通知簡介

通知業務流程

廣播的類型

接口說明

開發前期準備

二.發送普通文本類型通知

1.先初始化廣播的請求request

2.然后發送廣播

3.顯示效果如下

三.發送長文本類型廣播

1.構建發送廣播的參數request

2.然后發送廣播

3.顯示效果如下

注意事項

四.發送多行文本類型廣播

1.構建發送廣播的參數request

2.發送廣播

3.顯示效果

注意事項

五.發送圖片類型廣播

代碼

顯示效果

六.發送意圖類型廣播

1.創建wantAgent字段

2.構建發送廣播的參數request

3.發送廣播

4.顯示結果


一.通知概述

通知簡介

應用可以通過通知接口發送通知消息,終端用戶可以通過通知欄查看通知內容,也可以點擊通知來打開應用。

通知常見的使用場景:

  • 顯示接收到的短消息、即時消息等。

  • 顯示應用的推送消息,如廣告、版本更新等。

  • 顯示當前正在進行的事件,如下載等。

HarmonyOS通過ANS(Advanced Notification Service,通知系統服務)對通知類型的消息進行管理,支持多種通知類型,如基礎類型通知、進度條類型通知。

通知業務流程

通知業務流程由通知子系統、通知發送端、通知訂閱端組成。

一條通知從通知發送端產生,通過IPC通信發送到通知子系統,再由通知子系統分發給通知訂閱端。

系統應用還支持通知相關配置,如使能開關、配置參數由系統配置發起請求,發送到通知子系統存儲到內存和數據庫。

廣播的類型

  • NOTIFICATION_CONTENT_BASIC_TEXT:普通文本類型
  • NOTIFICATION_CONTENT_LONG_TEXT:長文本類型
  • NOTIFICATION_CONTENT_MULTILINE:多行文本類型
  • NOTIFICATION_CONTENT_PICTURE:圖片類型

廣播的類型主要分為普通文本類型,發送普通的文本廣播;長文本類型,發送長文本類型的廣播;多行文本類型,可以將文字多行顯示發送廣播;發送圖片類型的廣播。

接口說明

通知發布接口如下表所示,不同發布類型通知由NotificationRequest的字段攜帶不同的信息。

接口名

描述

publish(request: NotificationRequest, callback: AsyncCallback<void>): void

發布通知。

cancel(id: number, label: string, callback: AsyncCallback<void>): void

取消指定的通知。

cancelAll(callback: AsyncCallback<void>): void;

取消所有該應用發布的通知。

開發前期準備

導包

import?NotificationManager from?'@ohos.notificationManager';

二.發送普通文本類型通知

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.然后發送廣播

@Entry

@Component

struct Index {

??build() {

????Row() {

??????Column() {

????????Button("發送普通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.顯示效果如下

點擊發送普通廣播按鈕后下拉通知欄

三.發送長文本類型廣播

長文本類型通知繼承了普通文本類型的字段,同時新增了長文本內容、內容概要和通知展開時的標題。通知默認顯示與普通文本相同,展開后,標題顯示為展開后標題內容,內容為長文本內容。

1.構建發送廣播的參數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.然后發送廣播

@Entry

@Component

struct Index {

??build() {

????Row() {

??????Column() {

????????Button("發送長文本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.顯示效果如下

點擊按鈕后然后下拉通知欄顯示效果

注意事項

  • 目前測試發現長文本要足夠長,如果不夠長則只會顯示出長文本內容,普通文本內容顯示不出來

四.發送多行文本類型廣播

多行文本類型通知繼承了普通文本類型的字段,同時新增了多行文本內容、內容概要和通知展開時的標題。通知默認顯示與普通文本相同,展開后,標題顯示為展開后標題內容,多行文本內容多行顯示。

1.構建發送廣播的參數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.發送廣播

@Entry

@Component

struct Index {

??build() {

????Row() {

??????Column() {

????????Button("發送多行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.顯示效果

點擊后下拉通知欄

注意事項

  • 如果文本只有一行,會只顯示出多行文本類型的內容,不顯示普通文本類型的內容

五.發送圖片類型廣播

圖片類型通知繼承了普通文本類型的字段,同時新增了圖片內容、內容概要和通知展開時的標題,圖片內容為PixelMap型對象,其大小不能超過2M。

代碼

@Entry

@Component

struct Index {

??build() {

????Row() {

??????Column() {

????????Button("發送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',

??????????????????}

????????????????},

??????????????}

??????????????// 發送通知

??????????????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%')

??}

}

顯示效果

點擊按鈕后下拉通知欄顯示

六.發送意圖類型廣播

意圖類型的廣播就是發送后可以點擊并跳轉到頁面的廣播,意圖類型通知繼承了普通文本類型的字段,同時新增了wantAgent字段,此參數的跳轉到哪個頁面的意思

1.創建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(_ => {

??????????// 創建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參數

2.構建發送廣播的參數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.發送廣播

// 通知發送

NotificationManager.publish(notificationRequest, (err) => {

????if?(err) {

????????console.error(`[ANS] failed to publish, error[${err}]`);

????????return;

????}

????console.info(`[ANS] publish success `);

});

4.顯示結果

點擊后下拉通知欄點擊通知欄

總結

以上是生活随笔為你收集整理的004 鸿蒙应用开发-通知栏的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。