0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

harmony-utils之NotificationUtil,通知工具类

童长老 来源:jf_14594073 作者:jf_14594073 2025-07-03 18:09 次阅读
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

harmony-utils之NotificationUtil,通知工具类

harmony-utils 简介与说明


harmony-utils 一款功能丰富且极易上手的HarmonyOS工具库,借助众多实用工具类,致力于助力开发者迅速构建鸿蒙应用。其封装的工具涵盖了APP、设备、屏幕、授权、通知、线程间通信、弹框、吐司、生物认证、用户首选项、拍照、相册、扫码、文件、日志、异常捕获、字符、字符串、数字、集合、日期、随机、base64、加密、解密、JSON等一系列的功能和作,能够满足各种不同的开发需求。
picker_utils 是harmony-utils拆分出来的一个子库,包含 PickerUtil、PhotoHelper、ScanUtil。

下载安装
ohpm i @pura/harmony-utils
ohpm i @pura/picker_utils

//全局初始化方法,在UIAbility的onCreate方法中初始化 AppUtil.init()
 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
   AppUtil.init(this.context);
 }

API方法与使用


setDefaultConfig 设置通知的默认统一配置
let wantAgent = await NotificationUtil.getDefaultWantAgent();
let smallIcon = await NotificationUtil.getCompressedIcon($r('app.media.test_as5'));
let largeIcon = await NotificationUtil.getCompressedIcon($r('app.media.test_as1'));
let lPicture = await NotificationUtil.getCompressedPicture($r("app.media.test_as1"));

NotificationUtil.setDefaultConfig((config) = > {
  config.wantAgent = wantAgent
  config.removalWantAgent = wantAgent
  config.smallIcon = smallIcon
  config.largeIcon = largeIcon
  config.isFloatingIcon = true
  config.tapDismissed = true
  config.additionalText = "默认的统一配置"
  config.lockscreenPicture = lPicture
})
isNotificationEnabled 查询通知是否授权
let isEnabled = await NotificationUtil.isNotificationEnabled();
ToastUtil.showToast(`查询通知是否授权: ${isEnabled}`);
authorizeNotification 请求通知授权,第一次调用会弹窗让用户选择。
NotificationUtil.authorizeNotification((grant) = > {
  ToastUtil.showToast(`授权通知服务: ${grant ? '成功' : '失败'}`);
  if (!grant) {
    WantUtil.toNotificationSetting(); //跳转通知设置页面
  }
});
isSupportTemplate 查询模板是否存在,目前仅支持进度条模板。
let blTemplate = await NotificationUtil.isSupportTemplate();
ToastUtil.showToast(`查询模板是否存在: ${blTemplate}`);
isDistributedEnabled 查询设备是否支持分布式通知
let blDistributedEnabled = await NotificationUtil.isDistributedEnabled();
ToastUtil.showToast(`查询设备是否支持分布式通知: ${blDistributedEnabled}`);
publishBasic 发布普通文本通知
let wantAgent = await NotificationUtil.getDefaultWantAgent();
let id = NotificationUtil.generateNotificationId(); //通知id
let basicOptions: NotificationBasicOptions = {
  id: id,
  title: "鸿蒙工具包",
  text: "HarmonyOS工具包,封装了常用工具类,提供一系列简单易用的方法。帮助开发者快速构建鸿蒙应用",
  wantAgent: wantAgent
}
NotificationUtil.publishBasic(basicOptions).then((id) = > {
  ToastUtil.showToast(`通知发送成功,id为:${id}`);
  this.notificationIds.push(id);
}).catch((err: BusinessError) = > {
  ToastUtil.showToast(`通知发送失败,${err.message}`);
});
publishMultiLine 发布多文本通知
let multiLineOptions: NotificationMultiLineOptions = {
  title: "鸿蒙工具包",
  text: "HarmonyOS工具包,封装了常用工具类,提供一系列简单易用的方法。帮助开发者快速构建鸿蒙应用",
  briefText: "222222222222222222222222222222222",
  longTitle: "HarmonyOS工具包",
  lines: ["帮助初学者了解API", "封装了常用工具类", "提供一系列简单易用的方法",
    "帮助开发者快速构建鸿蒙应用"]
}
NotificationUtil.publishMultiLine(multiLineOptions).then((id) = > {
  ToastUtil.showToast(`通知发送成功,id为:${id}`);
  this.notificationIds.push(id);
}).catch((err: BusinessError) = > {
  ToastUtil.showToast(`通知发送失败,${err.message}`);
});
publishLongText 发布长文本通知
NotificationUtil.publishLongText({
  title: "鸿蒙工具包",
  text: "HarmonyOS工具包,封装了常用工具类,提供一系列简单易用的方法。帮助开发者快速构建鸿蒙应用",
  longText: "harmony-utils 一款高效的OpenHarmony/HarmonyOS工具包,封装了常用工具类,提供一系列简单易用的方法。帮助初学者了解API,帮助开发者快速构建鸿蒙应用",
  briefText: "111111111111111111111111111111111111111111",
  expandedTitle: "OpenHarmony/HarmonyOS工具包",
}).then((id) = > {
  ToastUtil.showToast(`通知发送成功,id为:${id}`);
  this.notificationIds.push(id);
}).catch((err: BusinessError) = > {
  ToastUtil.showToast(`通知发送失败,${err.message}`);
});
publishPicture 发布带有图片的通知
let media = await ImageUtil.getPixelMapFromMedia($r('app.media.test_as5'));
let pixelMap = await NotificationUtil.getCompressedPicture(media);
LogUtil.error("图片大小3:" + (pixelMap.getPixelBytesNumber() / 1024))
NotificationUtil.publishPicture({
  title: "鸿蒙工具包",
  text: "HarmonyOS工具包,封装了常用工具类。帮助开发者快速构建鸿蒙应用",
  briefText: "33333333333333333333333333333333",
  expandedTitle: "OpenHarmony/HarmonyOS工具包",
  picture: pixelMap
}).then((id) = > {
  ToastUtil.showToast(`通知发送成功,id为:${id}`);
  this.notificationIds.push(id);
}).catch((err: BusinessError) = > {
  ToastUtil.showToast(`通知发送失败,${err.message}`);
});
publishTemplate 发布模板通知
NotificationUtil.publishTemplate({
  title: "鸿蒙工具包",
  text: "HarmonyOS工具包,封装了常用工具类,提供一系列简单易用的方法。帮助开发者快速构建鸿蒙应用",
  fileName: "测试图片.mp4",
  progressValue: 72,
}).then((id) = > {
  ToastUtil.showToast(`通知发送成功,id为:${id}`);
  this.notificationIds.push(id);
}).catch((err: BusinessError) = > {
  ToastUtil.showToast(`通知发送失败,${err.message}`);
});
cancel 取消通知
NotificationUtil.cancel(1);
cancelGroup 取消本应用指定组下的通知
NotificationUtil.cancelGroup("group_msg");
cancelAll 取消所有通知
NotificationUtil.cancelAll();
ToastUtil.showToast(`取消所有通知,成功!`);
setBadge 设置桌面角标个数
NotificationUtil.setBadge(96);
clearBadge 清空桌面角标
NotificationUtil.clearBadge();
setBadgeFromNotificationCount 设置桌面角标数量,来自于通知数量
NotificationUtil.setBadgeFromNotificationCount().then(() = > {
  ToastUtil.showToast("设置角标成功");
}).catch((err: BusinessError) = > {
  ToastUtil.showToast("设置角标失败," + NotificationUtil.getErrorMsg(err.code, err.message));
  LogUtil.error(`设置角标异常:${NotificationUtil.getErrorMsg(err.code, err.code + " - " + err.message)}`);
});
getActiveNotificationCount 获取当前应用未删除的通知数量
let count = await NotificationUtil.getActiveNotificationCount();
ToastUtil.showToast(`当前通知数量为:${count}`);
getActiveNotifications 获取当前应用未删除的通知列表
let notifications = await NotificationUtil.getActiveNotifications();
generateNotificationId 生成通知id(用时间戳当id)
let id = NotificationUtil.generateNotificationId(); //通知id
getDefaultWantAgent 创建一个可拉起Ability的Want
let wantAgent = await NotificationUtil.getDefaultWantAgent();
getCompressedPicture 获取压缩通知的图片(图像像素的总字节数不能超过2MB)
let lPicture = await NotificationUtil.getCompressedPicture($r("app.media.test_as1"));
getCompressedIcon 获取压缩通知图标(图标像素的总字节数不超过192KB)
let smallIcon = await NotificationUtil.getCompressedIcon($r('app.media.test_as5'));
let largeIcon = await NotificationUtil.getCompressedIcon($r('app.media.test_as1'));

创作不易,请给童长老点赞

审核编辑 黄宇

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • Harmony
    +关注

    关注

    0

    文章

    109

    浏览量

    3643
收藏 人收藏
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

    评论

    相关推荐
    热点推荐

    harmony-utilsArrayUtil,集合工具

    # harmony-utilsArrayUtil,集合工具 ## harmony-utils 简介与说明 ------[
    的头像 发表于 06-26 17:38 629次阅读

    harmony-utilsAuthUtil,生物认证相关工具

    # harmony-utilsAuthUtil,生物认证相关工具 ## harmony-utils 简介与说明 ------[
    的头像 发表于 06-26 17:43 620次阅读

    harmony-utilsCacheUtil,缓存工具

    harmony-utilsCacheUtil,缓存工具
    的头像 发表于 07-04 16:36 667次阅读

    harmony-utilsCharUtil,字符工具

    harmony-utilsCharUtil,字符工具
    的头像 发表于 07-04 16:34 648次阅读

    harmony-utilsCrashUtil,异常相关工具

    harmony-utilsCrashUtil,异常相关工具
    的头像 发表于 07-04 16:33 745次阅读

    harmony-utilsDeviceUtil,设备相关工具

    harmony-utilsDeviceUtil,设备相关工具
    的头像 发表于 07-03 18:27 832次阅读

    harmony-utilsDisplayUtil,屏幕相关工具

    harmony-utilsDisplayUtil,屏幕相关工具
    的头像 发表于 07-03 18:26 650次阅读

    harmony-utilsEmitterUtil,Emitter工具

    harmony-utilsEmitterUtil,Emitter工具
    的头像 发表于 07-03 18:24 593次阅读

    harmony-utilsFileUtil,文件相关工具

    harmony-utilsFileUtil,文件相关工具
    的头像 发表于 07-03 18:23 752次阅读

    harmony-utilsFormatUtil,格式化工具

    harmony-utilsFormatUtil,格式化工具
    的头像 发表于 07-03 18:22 735次阅读

    harmony-utilsImageUtil,图片相关工具

    harmony-utilsImageUtil,图片相关工具
    的头像 发表于 07-03 18:22 1140次阅读

    harmony-utilsNumberUtil,Number工具

    harmony-utilsNumberUtil,Number工具 harmony-utils 简介与说明
    的头像 发表于 07-03 18:08 631次阅读

    harmony-utilsPreviewUtil,文件预览工具

    harmony-utilsPreviewUtil,文件预览工具 harmony-utils 简介与说明 [
    的头像 发表于 07-03 11:40 631次阅读

    harmony-utilsStrUtil,字符串工具

    harmony-utilsStrUtil,字符串工具 harmony-utils 简介与说明 [ha
    的头像 发表于 07-03 11:32 914次阅读

    harmony-utilsTypeUtil,类型检查工具

    harmony-utilsTypeUtil,类型检查工具 harmony-utils 简介与说明 [
    的头像 发表于 06-30 17:35 684次阅读