HarmonyOS 鸿蒙Next关于使用arkts通知功能的问题
HarmonyOS 鸿蒙Next关于使用arkts通知功能的问题 代码如下:
@Entry @Component struct Index {
@State message: string = ‘Hello World’
async explicitStartAbility() {
try {
let want = {
deviceId: “”,
bundleName: “com.example.hhh”,
abilityName: “EntryAbility”
};
let context = getContext(this) as common.UIAbilityContext;
await context.startAbility(want);
console.info(explicit start ability succeed
);
} catch (error) {
console.info(explicit start ability failed with ${error.code}
);
}
}
build() { Row() { Column() { Text(this.message) .fontSize(50) .fontWeight(FontWeight.Bold) Button(‘按钮’, { type: ButtonType.Normal, stateEffect: true }) .fontSize(50) .onClick(() => { PublishNo(); }) .width(‘100%’) .height(‘50%’) Button(‘执行’, { type: ButtonType.Normal, stateEffect: true }) .fontSize(50) .width(‘100%’) .height(‘50%’) .onClick(() => { this.explicitStartAbility(); }) } } } }
function sleep(arg0: number) { var now = new Date(); var exitTime = now.getTime() + arg0; while (true) { now = new Date(); if (now.getTime() > exitTime) return; } throw new Error(‘Function not implemented.’); }
function PublishNo() { function openNotificationPermission() { notification.requestEnableNotification().then(() => { console.log(“zybxx123”); }).catch((err) => { console.log(“zybxx456”); }); } openNotificationPermission(); let wantAgentObj = null; let wantAgentInfo = { wants: [ { deviceId: ‘’, bundleName: “com.example.application”, abilityName: “com.example.application.EntryAbility” } ], operationType: wantAgent.OperationType.START_ABILITY, requestCode: 0, wantAgentFlags: [wantAgent.WantAgentFlags.CONSTANT_FLAG] } as wantAgent.WantAgentInfo; console.log(“zyb1”); function getWantAgentCallback(err, data) { console.info(’===========================>getWantAgentCallback=======================>’); wantAgentObj=data; console.log(“zyb21”) } try { wantAgent.getWantAgent(wantAgentInfo, getWantAgentCallback); } catch (paramError) { console.log('error: ’ + paramError.code + ', ’ + paramError.message); } console.log(“zyb2”);
let notificationRequest = {
slotType: NotificationManager.SlotType.CONTENT_INFORMATION,
content: {
contentType: NotificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
normal: {
title: ‘起床提醒’,
text: ‘起床了,起床了!’,
additionalText: ‘3’,
},
id: 1,
label: “TEST”,
wantAgent: wantAgentObj,
}
}
console.log(“zyb3”);
NotificationManager.publish(notificationRequest, (err) => {
if (err) {
console.error([ANS] failed to publish, error[${err}]
);
return;
}
console.info(zyb4
);
});
};
运行后,通知出现通知条目,但是下拉通知单时出现以下报错:
01-04 07:03:15.188 26176-15785/com.example.myapplication E 03900/Ace: [jsi_declarative_engine.cpp(CallAppFunc)-(0)] property “default” is not a object
请问各位大佬是怎么回事呀?
更多关于HarmonyOS 鸿蒙Next关于使用arkts通知功能的问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html
目前还没有找到答案。
更多关于HarmonyOS 鸿蒙Next关于使用arkts通知功能的问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
全的报错日志发一下呢
在HarmonyOS(鸿蒙)Next中,ArkTS的通知功能主要通过[@ohos](/user/ohos).notification
模块实现。开发者可以使用该模块创建、发送和管理通知。具体步骤如下:
-
导入模块:
import notification from '[@ohos](/user/ohos).notification';
-
创建通知内容: 使用
NotificationRequest
对象定义通知的内容和行为。let notificationRequest: notification.NotificationRequest = { content: { contentType: notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, normal: { title: "通知标题", text: "通知内容", additionalText: "附加信息" } }, id: 1, // 通知ID slotType: notification.SlotType.SOCIAL_COMMUNICATION // 通知类型 };
-
发送通知: 使用
publish
方法发送通知。notification.publish(notificationRequest).then(() => { console.log("通知发送成功"); }).catch((err) => { console.error("通知发送失败: " + JSON.stringify(err)); });
-
管理通知: 可以通过
cancel
方法取消通知,或使用getActiveNotifications
获取当前活动的通知。notification.cancel(1); // 取消ID为1的通知 notification.getActiveNotifications().then((notifications) => { console.log("当前活动通知: " + JSON.stringify(notifications)); });
ArkTS的通知功能支持多种通知类型和自定义行为,开发者可以根据需求配置通知的优先级、显示方式等。