鸿蒙HarmonyOS实战- ArkTS语言基础类库(通知)_arkts实现语音播报

53 阅读4分钟

| NOTIFICATION_CONTENT_MULTILINE | 多行文本类型。 | | NOTIFICATION_CONTENT_PICTURE | 图片类型。 |

例如,你可以发送一条包含简短文字的通知来提醒用户某个事件即将发生,或者发送一条包含长文本的通知来提供详细的信息。此外,你还可以发送一条包含多行文本的通知,每行显示一条信息。如果需要显示图片,你可以发送一条包含图片的通知。基础类型通知非常灵活,可以根据具体需求来进行设置。

2.1.1 接口说明
接口名描述
publish(request: NotificationRequest, callback: AsyncCallback): void发布通知。
cancel(id: number, label: string, callback: AsyncCallback): void取消指定的通知。
cancelAll(callback: AsyncCallback): void;取消所有该应用发布的通知。
2.1.2 开发步骤

在HarmonyOS中,NotificationRequest类是用于创建通知的一个重要类。通过NotificationRequest类,可以设置通知的各种属性,如标题、内容、图标、声音、震动等。

2.1.2.1 普通文本类型

import NotificationManager from '@ohos.notificationManager'; let notificationRequest = { id: 1, content: { contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, // 普通文本类型通知 normal: { title: 'test_title', text: 'test_text', additionalText: 'test_additionalText', } } }

@Entry @Component struct Index { @State message: string = 'Hello World'

build() { Row() { Column() { Text(this.message) .fontSize(50) .fontWeight(FontWeight.Bold) .onClick(() => { NotificationManager.publish(notificationRequest, (err) => { if (err) { console.error([ANS] failed to publish, error[${err}]); return; } console.info([ANS] publish success); }); }) } .width('100%') } .height('100%') } }

在这里插入图片描述

2.1.2.2 长文本类型通知

import NotificationManager from '@ohos.notificationManager'; 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', } } } // 发布通知 NotificationManager.publish(notificationRequest, (err) => { if (err) { console.error([ANS] failed to publish, error[${err}]); return; } console.info([ANS] publish success); }); @Entry @Component struct Index { @State message: string = 'Hello World'

build() { Row() { Column() { Text(this.message) .fontSize(50) .fontWeight(FontWeight.Bold) .onClick(() => {

}) } .width('100%') } .height('100%') } }

在这里插入图片描述

2.1.2.3 多行文本类型通知

import NotificationManager from '@ohos.notificationManager'; 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'], } } }

// 发布通知 NotificationManager.publish(notificationRequest, (err) => { if (err) { console.error([ANS] failed to publish, error[${err}]); return; } console.info([ANS] publish success); }); @Entry @Component struct Index { @State message: string = 'Hello World'

build() { Row() { Column() { Text(this.message) .fontSize(50) .fontWeight(FontWeight.Bold) .onClick(() => {

}) } .width('100%') } .height('100%') } }

在这里插入图片描述

2.1.2.4 图片类型通知

import NotificationManager from '@ohos.notificationManager'; import image from '@ohos.multimedia.image'; // 图片构造 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}};

image // @ts-ignore .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: NotificationManager.ContentType.NOTIFICATION_CONTENT_PICTURE, picture: { title: 'test_title', text: 'test_text', additionalText: 'test_additionalText', picture: pixelmap, 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; })

@Entry @Component struct Index { @State message: string = 'Hello World'

build() { Row() { Column() { Text(this.message) .fontSize(50) .fontWeight(FontWeight.Bold) .onClick(() => {

})

} .width('100%') } .height('100%') } }

在这里插入图片描述

2.2 发布进度条类型通知

☀️2.2.1 接口说明
接口名描述
isSupportTemplate(templateName: string, callback: AsyncCallback): void查询模板是否存在。
☀️2.2.2 开发步骤

import NotificationManager from '@ohos.notificationManager'; NotificationManager.isSupportTemplate('downloadTemplate').then((data) => { console.info([ANS] isSupportTemplate success); let isSupportTpl: boolean = data; // isSupportTpl的值为true表示支持支持downloadTemplate模板类通知,false表示不支持 // ... }).catch((err) => { console.error([ANS] isSupportTemplate failed, error[${err}]); });

let template = { name:'downloadTemplate', data: { title: '标题:', fileName: 'music.mp4', progressValue: 30, progressMaxValue:100, } } //构造NotificationRequest对象 let notificationRquest = { id: 1, slotType: NotificationManager.SlotType.OTHER_TYPES, template: template, content: { contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, normal: { title: template.data.title + template.data.fileName, text: "sendTemplate", additionalText: "30%" } }, deliveryTime: new Date().getTime(), showDeliveryTime: true } NotificationManager.publish(notificationRquest).then(() => { console.info([ANS] publish success ); }).catch((err) => { console.error([ANS] failed to publish, error[${err}]); });

@Entry @Component struct Index { @State message: string = 'Hello World'

build() { Row() { Column() { Text(this.message) .fontSize(50) .fontWeight(FontWeight.Bold) .onClick(() => {

})

} .width('100%') } .height('100%') } }

在这里插入图片描述

2.3 为通知添加行为意图

WantAgent是HarmonyOS提供的一种功能,它允许开发者封装行为意图。行为意图主要用于拉起指定的应用组件或发布公共事件等。在HarmonyOS中,我们可以通过通知的方式将WantAgent从发布方传递给接收方,从而在接收方触发WantAgent中指定的意图。

举个例子,假设有一个应用发布一个通知消息,通常希望用户能够通过点击通知栏来打开目标应用组件。为了实现这个目标,开发者可以将WantAgent封装到通知消息中。当系统接收到带有WantAgent的通知消息时,用户点击通知栏时会触发WantAgent中指定的意图,从而打开目标应用组件。

为了实现通知中的行为意图,应用需要向应用组件管理服务(AMS)申请WantAgent,并将其与其他通知信息一起发送给桌面。当用户在桌面通知栏上点击通知时,系统会触发WantAgent的动作,从而实现目标应用组件的打开。

在这里插入图片描述

☀️2.3.1 接口说明
接口名描述
getWantAgent(info: WantAgentInfo, callback: AsyncCallback): void创建WantAgent。
trigger(agent: WantAgent, triggerInfo: TriggerInfo, callback?: Callback): void触发WantAgent意图。
cancel(agent: WantAgent, callback: AsyncCallback): void取消WantAgent。
getWant(agent: WantAgent, callback: AsyncCallback): void获取WantAgent的want。
equal(agent: WantAgent, otherAgent: WantAgent, callback: AsyncCallback): void判断两个WantAgent实例是否相等。
☀️2.3.2 开发步骤

import NotificationManager from '@ohos.notificationManager'; import wantAgent from '@ohos.app.ability.wantAgent';

let wantAgentObj = null; // 用于保存创建成功的wantAgent对象,后续使用其完成触发的动作。

// 通过WantAgentInfo的operationType设置动作类型。 let wantAgentInfo = { wants: [ { deviceId: '', bundleName: 'com.example.test', abilityName: 'com.example.test.MainAbility', action: '', entities: [], uri: '', parameters: {} } ], operationType: wantAgent.OperationType.START_ABILITY, requestCode: 0, wantAgentFlags:[wantAgent.WantAgentFlags.CONSTANT_FLAG] }

// // wantAgentInfo // let wantAgentInfo = { // wants: [ // { // action: 'event_name', // 设置事件名。 // parameters: {}, // } // ], // operationType: wantAgent.OperationType.SEND_COMMON_EVENT, // requestCode: 0,

img img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!