HarmonyOS 本地推送没有提示

创建本地推送的时候,没有任何的提示,如何创建才能给用户提示有新消息呢? 创建代码在下方

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.');
  });
}
HarmonyOS
2024-12-25 16:00:39
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Excelsior_abit

可以指定notificationSlotType

let notificationRequest: notificationManager.NotificationRequest = {
  notificationSlotType: notificationManager.SlotType.SERVICE_INFORMATION,
  content: {
    notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
    normal: {
      title: '测试标题123',
      text: '测试内容456',
    },
  },
  wantAgent: wantAgentObj
};

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#通知渠道类型说明

分享
微博
QQ
微信
回复
2024-12-25 19:24:17
相关问题
HarmonyOS 实况窗推送提示没有权限
47浏览 • 1回复 待解决
读取本地json 文件提示权限拒绝
3369浏览 • 1回复 待解决
启动本地模拟器,提示Haxm安装失败
813浏览 • 1回复 待解决
HarmonyOS 推送服务限制每日推送条数
724浏览 • 1回复 待解决