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老皮!!!欢迎大家来找我探讨交流👀

相关推荐
shr007_6 小时前
flutter 鸿蒙
flutter·华为·harmonyos
鼓掌MVP10 小时前
【案例实战】多维度视角:鸿蒙2048游戏开发的深度分析与感悟
华为·ai编程·harmonyos·arkts·游戏开发·ability
安卓开发者10 小时前
鸿蒙Next Performance Analysis Kit:打造极致流畅的应用体验
华为·harmonyos
Devil枫10 小时前
【案例实战】HarmonyOS应用性能优化实战案例
华为·性能优化·harmonyos
猫林老师10 小时前
HarmonyOS线程模型与性能优化实战
数据库·分布式·harmonyos
一路阳光85110 小时前
鸿蒙生态发力,鸿蒙智行引领智能产业新征程
华为·harmonyos
一路阳光85112 小时前
开源鸿蒙5.0正式发布 底座及配套能力快速稳定成熟
华为·开源·harmonyos
HMSCore13 小时前
碰一碰,秒更新!游戏近场快传助力多人联机无缝组队
harmonyos
HarmonyOS_SDK13 小时前
碰一碰,秒更新!游戏近场快传助力多人联机无缝组队
harmonyos
鸿蒙小白龙14 小时前
基于 OpenHarmony 6.0 的智能充电桩技术方案与实现
能源·harmonyos·鸿蒙·鸿蒙系统·open harmony