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(); //通知idlet 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'));
复制代码
创作不易,请给童长老点赞👍
https://github.com/787107497/harmony-utils
https://gitee.com/tongyuyan/harmony-utils
OpenHarmony三方库
童长老CSDN博客
评论