创建本地推送的时候,没有任何的提示,如何创建才能给用户提示有新消息呢? 创建代码在下方
async pushTest() {
let img = await getContext(this).resourceManager.getMediaContent($r('app.media.nbicon'));
let imageSource:image.ImageSource = image.createImageSource(img.buffer.slice(0));
let decodingOptions : image.DecodingOptions = {
editable: true,
desiredPixelFormat: 3,
}
// 创建pixelMap
let icon: image.PixelMap | undefined = await imageSource.createPixelMap(decodingOptions);
let wantAgentObj: WantAgent; // 用于保存创建成功的wantAgent对象,后续使用其完成触发的动作。
// 通过WantAgentInfo的operationType设置动作类型
let wantAgentInfo: wantAgent.WantAgentInfo = {
wants: [
{
deviceId: '',
bundleName: "com.xxx.mybundleName",
abilityName: "EntryAbility",
action: '',
entities: [],
uri: '',
parameters: {
'test1': 'test1',
'test2': 'test2',
'test3': 'test3',
}
}
],
operationType: wantAgent.OperationType.START_ABILITY,
requestCode: 0,
wantAgentFlags: [wantAgent.WantAgentFlags.CONSTANT_FLAG]
};
wantAgentObj = await wantAgent.getWantAgent(wantAgentInfo);
icon = undefined
let notificationRequest: notificationManager.NotificationRequest = {
content: {
notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
normal: {
title: '测试标题123',
text: '测试内容456',
},
},
wantAgent: wantAgentObj
};
if (icon) {
notificationRequest.smallIcon = icon
notificationRequest.largeIcon = icon
}
notificationManager.publish(notificationRequest, (err: BusinessError) => {
if (err) {
console.error(`Failed to publish notification. Code is ${err.code}, message is ${err.message}`);
return;
}
console.info('Succeeded in publishing notification.');
});
}
可以指定notificationSlotType
SlotType:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-notificationmanager-V5\#slottype
不同类型的通知渠道对应的通知提醒方式不同:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/notification-slot-V5\#通知渠道类型说明