本篇內容: ArtTS系統能力-通知的學習(3.1)
一、 知識儲備
1. 基礎類型通知
按內容分成四類:
類型 | 描述 |
---|
NOTIFICATION_CONTENT_BASIC_TEXT | 普通文本類型 |
NOTIFICATION_CONTENT_LONG_TEXT | 長文本類型 |
NOTIFICATION_CONTENT_MULTILINE | 多行文本類型 |
NOTIFICATION_CONTENT_PICTURE | 圖片類型 |
2. 帶進度類型通知

3. 帶事件響應類型通知
二、 效果一覽

三、 源碼剖析
import notificationManager from '@ohos.notificationManager'
import http from '@ohos.net.http'
import ResponseCode from '@ohos.net.http'
import image from '@ohos.multimedia.image'
import wantAgent from '@ohos.app.ability.wantAgent'function basicText() {let notificationRequest = {id: 1,content: {contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,normal: {title: '通知類型',text: '普通文本類型',additionalText: '我是補充標題'}}}notificationManager.publish(notificationRequest, err => {if (err) {console.error(`普通文本類型通知發布失敗: ${err}`)return;}console.info('普通文本類型通知發布成功')})
}function longText() {let notificationRequest = {id: 2,content: {contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_LONG_TEXT,longText: {title: '通知類型', //無效text: '長文本類型', //無效additionalText: '我是補充標題 ',longText: '我是長文本我是長文本我是長文本',briefText: '我是簡明信息', //無效expandedTitle: '我是擴展文本'}}}notificationManager.publish(notificationRequest, err => {if (err) {console.error('長文本類型通知發布失敗:' + err)return;}console.info('長文本類型通知發布成功')})
}function multiline() {let notificationRequest = {id: 3,content: {contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_MULTILINE,multiLine: {title: '通知類型', //無效additionalText: '我是補充標題',text: '多行文本類型', //無效briefText: '我是簡明信息', //無效longTitle: '我是長文本標題',lines: ['第一行', '第二行', '第三行', '第四行', '第五行']}}}notificationManager.publish(notificationRequest, err => {if (err) {console.error(`多行文本通知發布失敗: ${err}`)return;}console.info('多行文本通知發布成功')})
}function picture() {let imgUrl: string = 'https://img1.baidu.com/it/u=3241660985,1063915045&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1194';http.createHttp().request(imgUrl, (err, data) => {if (err) {console.error(`err is ${JSON.stringify(err)}`)} else {let code = data.responseCode;if (ResponseCode.ResponseCode.OK == code) {let res: any = data.result;let imageSource = image.createImageSource(res)let options = {alphaTye: 0, //透明度editable: false, //是否可編輯pixelFormat: 3, //像素格式scaleMode: 1, //縮略值size: { height: 100, wight: 100 } //創建圖片大小}imageSource.createPixelMap(options).then(pixelMap => {let imagePixelMap: PixelMap = undefined;imagePixelMap = pixelMap;let notificationRequest = {id: 4,content: {contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_PICTURE,picture: {title: '通知類型',text: '圖片通知',additionalText: '我是補充標題',briefText: '我是簡明信息',expandedTitle: '擴展消息',picture: imagePixelMap}}}notificationManager.publish(notificationRequest, err => { // 官方解釋 :2024.06.30 圖片類型通知。(預留能力,暫未支持)。if (err) {console.error(`圖片類型通知發布失敗: ${err}`)return;}console.info('圖片類型通知發布成功')})})}}})
}function progress() {let progress = 1;// for (let i = 0; i < 100; i++) {// setTimeout(() => {// progress += 1;//// }, 100);// }//需要先查詢系統是否支持進度條模板notificationManager.isSupportTemplate('downloadTemplate').then((data) => {let isSupport: boolean = data;if (isSupport) {let notificationRequest = {id: 5,content: {contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,normal: {title: '下載通知',text: '我正在下載',additionalText: '下載通知標題'}},template: { // 構造進度條模板,name字段當前需要固定配置為 downloadTemplatename: 'downloadTemplate',data: {title: '文檔下載',fileName: '阿吉',progressValue: 22}}}notificationManager.publish(notificationRequest, err => {if (err) {console.error(`下載進度提醒失敗 ${err}`)return;}console.info('下載進度提醒正常')})}}).catch(err => {console.error(`暫不支持:${err.message}`)})
}function action() {let wantAgentObj = null; //用于保存創建成功的wantAgent對象,后續使用其完成觸發的動作let wantAgentInfo = { //通過 wantAgentInfo的operationType設置動作類型wants: [{deviceId: '',bundleName: 'com.aji.first',abilityName: 'com.aji.first.SecondAbility',action: '',entities: [],uri: '',parameters: {}}],operationType: wantAgent.OperationType.START_ABILITY,requestCode: 0,wantAgentFlags: [wantAgent.WantAgentFlags.CONSTANT_FLAG]}wantAgent.getWantAgent(wantAgentInfo, (err, data) => {if (err) {console.error('失敗: ' + JSON.stringify(err))} else {wantAgentObj = data;}})let notificationRequest = {6id: 6,content: {contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,normal: {title: '標題',text: '進入應用',additionalText: '歡迎再次使用'},label: '阿吉',wantAgent: wantAgentObj}}notificationManager.publish(notificationRequest, err => {if (err) {console.error('發布失敗' + err)return;}console.info('發布成功')})
}@Entry
@Component
struct Index {build() {Column() {Button('普通文本類型').onClick(() => {basicText();}).margin(20)Button('長文本類型').onClick(() => {longText();}).margin(20)Button('多行文本類型').onClick(() => {multiline();}).margin(20)Button('圖片類型').onClick(() => {picture();}).margin(20)Button('進度條類型').onClick(() => {progress();}).margin(20)Button('帶意圖類型').onClick(() => {action();}).margin(20)}.width('100%').height('100%')}
}