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

img
img

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

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

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

.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,
// wantAgentFlags: [wantAgent.WantAgentFlags.CONSTANT_FLAG],
// }

// 创建WantAgent
wantAgent.getWantAgent(wantAgentInfo, (err, data) => {

img
img

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

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

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

AgentInfo, (err, data) => {

[外链图片转存中…(img-Yp0PfIdJ-1715511427799)]
[外链图片转存中…(img-g8841obH-1715511427800)]

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

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值