HarmonyOS鸿蒙通知

一、基础类型-简单文本

1、导入模块

ts 复制代码
1、import notificationManager from '@ohos.notificationManager';

2、构造NotificationRequest对象,并发布通知。

普通文本类型通知由标题、文本内容和附加信息三个字段组成,其中标题和文本内容是必填字段

ts 复制代码
  let notificationRequest = {
      id: 1,
      content: {
        contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, // 这是普通文本类型通知
        normal: {
          title: 'huxiubo title',
          text: 'huxiubo text',
          additionalText: 'huxiubo additionalText',
        }
      }
    }

    notificationManager.publish(notificationRequest, (err) => {
      if (err) {
        console.error(`Failed, error[${err}]`);
        return;
      }
      console.info("Successed pushlish ");
    });

3、运行效果如下图所示

二、基础类型通知-长文本

长文本类型通知继承了普通文本类型的字段,同时新增了长文本内容、内容概要和通知展开时的标题。通知默认显示与普通文本相同,展开后,标题显示为展开后标题内容,内容为长文本内容。

ts 复制代码
let notificationRequest = {
      id: 1,
      content: {
        contentType: notificationManager.ContentType.NOTIFICATION_CONTENT_LONG_TEXT, // 长文本类型通知
        longText: {
          title: 'huxiubo title',
          text: 'huxiubo text',
          additionalText: 'huxiubo additionalText',
          longText: 'huxiubo longText',
          briefText: 'huxiubo briefText',
          expandedTitle: 'huxiubo expandedTitle',
        }
      }
    }

    notificationManager.publish(notificationRequest, (err) => {
      if (err) {
        console.error(`Failed, error[${err}]`);
        return;
      }
      console.info("Successed pushlish ");
    });

三 图片

图片类型通知继承了普通文本类型的字段,同时新增了图片内容、内容概要和通知展开时的标题,图片内容为PixelMap型对象,其大小不能超过2M。

ts 复制代码
// 图片构造
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}};
await image
  .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: notify.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;
  })

👀关注公众号:Android老皮!!!欢迎大家来找我探讨交流👀

相关推荐
coder_pig7 小时前
跟🤡杰哥一起学Flutter (三十四、玩转Flutter手势✋)
前端·flutter·harmonyos
simple丶7 小时前
【HarmonyOS】鸿蒙蓝牙连接与通信技术
harmonyos·arkts·arkui
前端世界9 小时前
HarmonyOS开发实战:鸿蒙分布式生态构建与多设备协同发布全流程详解
分布式·华为·harmonyos
Jalor10 小时前
Flutter + 鸿蒙 | Flutter 跳转鸿蒙原生界面
flutter·harmonyos
zhanshuo10 小时前
开发者必看!如何在HarmonyOS中快速调用摄像头功能
harmonyos
HMSCore11 小时前
借助HarmonyOS SDK,《NBA巅峰对决》实现“分钟级启动”到“秒级进场”
harmonyos
zhanshuo11 小时前
鸿蒙UI开发全解:JS与Java双引擎实战指南
前端·javascript·harmonyos
HarmonyOS小助手11 小时前
闯入鸿蒙:浪漫、理想与「草台班子」
harmonyos·鸿蒙·harmonyos next·鸿蒙生态
xq952711 小时前
flutter 鸿蒙化插件开发横空出世
harmonyos
HarmonyOS_SDK12 小时前
借助HarmonyOS SDK,《NBA巅峰对决》实现“分钟级启动”到“秒级进场”
harmonyos