一区二区三区三上|欧美在线视频五区|国产午夜无码在线观看视频|亚洲国产裸体网站|无码成年人影视|亚洲AV亚洲AV|成人开心激情五月|欧美性爱内射视频|超碰人人干人人上|一区二区无码三区亚洲人区久久精品

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

harmony-utils之NotificationUtil,通知工具類

童長老 ? 來源:jf_14594073 ? 作者:jf_14594073 ? 2025-07-03 18:09 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

harmony-utils之NotificationUtil,通知工具類

harmony-utils 簡介與說明


harmony-utils 一款功能豐富且極易上手的HarmonyOS工具庫,借助眾多實用工具類,致力于助力開發(fā)者迅速構(gòu)建鴻蒙應(yīng)用。其封裝的工具涵蓋了APP、設(shè)備、屏幕、授權(quán)、通知、線程間通信、彈框、吐司、生物認證、用戶首選項、拍照、相冊、掃碼、文件、日志、異常捕獲、字符、字符串、數(shù)字、集合、日期、隨機、base64、加密、解密、JSON等一系列的功能和作,能夠滿足各種不同的開發(fā)需求。
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 設(shè)置通知的默認統(tǒng)一配置
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 = "默認的統(tǒng)一配置"
  config.lockscreenPicture = lPicture
})
isNotificationEnabled 查詢通知是否授權(quán)
let isEnabled = await NotificationUtil.isNotificationEnabled();
ToastUtil.showToast(`查詢通知是否授權(quán): ${isEnabled}`);
authorizeNotification 請求通知授權(quán),第一次調(diào)用會彈窗讓用戶選擇。
NotificationUtil.authorizeNotification((grant) = > {
  ToastUtil.showToast(`授權(quán)通知服務(wù): ${grant ? '成功' : '失敗'}`);
  if (!grant) {
    WantUtil.toNotificationSetting(); //跳轉(zhuǎn)通知設(shè)置頁面
  }
});
isSupportTemplate 查詢模板是否存在,目前僅支持進度條模板。
let blTemplate = await NotificationUtil.isSupportTemplate();
ToastUtil.showToast(`查詢模板是否存在: ${blTemplate}`);
isDistributedEnabled 查詢設(shè)備是否支持分布式通知
let blDistributedEnabled = await NotificationUtil.isDistributedEnabled();
ToastUtil.showToast(`查詢設(shè)備是否支持分布式通知: ${blDistributedEnabled}`);
publishBasic 發(fā)布普通文本通知
let wantAgent = await NotificationUtil.getDefaultWantAgent();
let id = NotificationUtil.generateNotificationId(); //通知id
let basicOptions: NotificationBasicOptions = {
  id: id,
  title: "鴻蒙工具包",
  text: "HarmonyOS工具包,封裝了常用工具類,提供一系列簡單易用的方法。幫助開發(fā)者快速構(gòu)建鴻蒙應(yīng)用",
  wantAgent: wantAgent
}
NotificationUtil.publishBasic(basicOptions).then((id) = > {
  ToastUtil.showToast(`通知發(fā)送成功,id為:${id}`);
  this.notificationIds.push(id);
}).catch((err: BusinessError) = > {
  ToastUtil.showToast(`通知發(fā)送失敗,${err.message}`);
});
publishMultiLine 發(fā)布多文本通知
let multiLineOptions: NotificationMultiLineOptions = {
  title: "鴻蒙工具包",
  text: "HarmonyOS工具包,封裝了常用工具類,提供一系列簡單易用的方法。幫助開發(fā)者快速構(gòu)建鴻蒙應(yīng)用",
  briefText: "222222222222222222222222222222222",
  longTitle: "HarmonyOS工具包",
  lines: ["幫助初學者了解API", "封裝了常用工具類", "提供一系列簡單易用的方法",
    "幫助開發(fā)者快速構(gòu)建鴻蒙應(yīng)用"]
}
NotificationUtil.publishMultiLine(multiLineOptions).then((id) = > {
  ToastUtil.showToast(`通知發(fā)送成功,id為:${id}`);
  this.notificationIds.push(id);
}).catch((err: BusinessError) = > {
  ToastUtil.showToast(`通知發(fā)送失敗,${err.message}`);
});
publishLongText 發(fā)布長文本通知
NotificationUtil.publishLongText({
  title: "鴻蒙工具包",
  text: "HarmonyOS工具包,封裝了常用工具類,提供一系列簡單易用的方法。幫助開發(fā)者快速構(gòu)建鴻蒙應(yīng)用",
  longText: "harmony-utils 一款高效的OpenHarmony/HarmonyOS工具包,封裝了常用工具類,提供一系列簡單易用的方法。幫助初學者了解API,幫助開發(fā)者快速構(gòu)建鴻蒙應(yīng)用",
  briefText: "111111111111111111111111111111111111111111",
  expandedTitle: "OpenHarmony/HarmonyOS工具包",
}).then((id) = > {
  ToastUtil.showToast(`通知發(fā)送成功,id為:${id}`);
  this.notificationIds.push(id);
}).catch((err: BusinessError) = > {
  ToastUtil.showToast(`通知發(fā)送失敗,${err.message}`);
});
publishPicture 發(fā)布帶有圖片的通知
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工具包,封裝了常用工具類。幫助開發(fā)者快速構(gòu)建鴻蒙應(yīng)用",
  briefText: "33333333333333333333333333333333",
  expandedTitle: "OpenHarmony/HarmonyOS工具包",
  picture: pixelMap
}).then((id) = > {
  ToastUtil.showToast(`通知發(fā)送成功,id為:${id}`);
  this.notificationIds.push(id);
}).catch((err: BusinessError) = > {
  ToastUtil.showToast(`通知發(fā)送失敗,${err.message}`);
});
publishTemplate 發(fā)布模板通知
NotificationUtil.publishTemplate({
  title: "鴻蒙工具包",
  text: "HarmonyOS工具包,封裝了常用工具類,提供一系列簡單易用的方法。幫助開發(fā)者快速構(gòu)建鴻蒙應(yīng)用",
  fileName: "測試圖片.mp4",
  progressValue: 72,
}).then((id) = > {
  ToastUtil.showToast(`通知發(fā)送成功,id為:${id}`);
  this.notificationIds.push(id);
}).catch((err: BusinessError) = > {
  ToastUtil.showToast(`通知發(fā)送失敗,${err.message}`);
});
cancel 取消通知
NotificationUtil.cancel(1);
cancelGroup 取消本應(yīng)用指定組下的通知
NotificationUtil.cancelGroup("group_msg");
cancelAll 取消所有通知
NotificationUtil.cancelAll();
ToastUtil.showToast(`取消所有通知,成功!`);
setBadge 設(shè)置桌面角標個數(shù)
NotificationUtil.setBadge(96);
clearBadge 清空桌面角標
NotificationUtil.clearBadge();
setBadgeFromNotificationCount 設(shè)置桌面角標數(shù)量,來自于通知數(shù)量
NotificationUtil.setBadgeFromNotificationCount().then(() = > {
  ToastUtil.showToast("設(shè)置角標成功");
}).catch((err: BusinessError) = > {
  ToastUtil.showToast("設(shè)置角標失敗," + NotificationUtil.getErrorMsg(err.code, err.message));
  LogUtil.error(`設(shè)置角標異常:${NotificationUtil.getErrorMsg(err.code, err.code + " - " + err.message)}`);
});
getActiveNotificationCount 獲取當前應(yīng)用未刪除的通知數(shù)量
let count = await NotificationUtil.getActiveNotificationCount();
ToastUtil.showToast(`當前通知數(shù)量為:${count}`);
getActiveNotifications 獲取當前應(yīng)用未刪除的通知列表
let notifications = await NotificationUtil.getActiveNotifications();
generateNotificationId 生成通知id(用時間戳當id)
let id = NotificationUtil.generateNotificationId(); //通知id
getDefaultWantAgent 創(chuàng)建一個可拉起Ability的Want
let wantAgent = await NotificationUtil.getDefaultWantAgent();
getCompressedPicture 獲取壓縮通知的圖片(圖像像素的總字節(jié)數(shù)不能超過2MB)
let lPicture = await NotificationUtil.getCompressedPicture($r("app.media.test_as1"));
getCompressedIcon 獲取壓縮通知圖標(圖標像素的總字節(jié)數(shù)不超過192KB)
let smallIcon = await NotificationUtil.getCompressedIcon($r('app.media.test_as5'));
let largeIcon = await NotificationUtil.getCompressedIcon($r('app.media.test_as1'));

創(chuàng)作不易,請給童長老點贊

審核編輯 黃宇

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學習之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • Harmony
    +關(guān)注

    關(guān)注

    0

    文章

    108

    瀏覽量

    3014
收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評論

    相關(guān)推薦
    熱點推薦

    harmony-utilsArrayUtil,集合工具

    # harmony-utilsArrayUtil,集合工具 ## harmony-utils 簡介與說明 ------[
    的頭像 發(fā)表于 06-26 17:38 ?105次閱讀

    harmony-utilsAuthUtil,生物認證相關(guān)工具

    # harmony-utilsAuthUtil,生物認證相關(guān)工具 ## harmony-utils 簡介與說明 ------[
    的頭像 發(fā)表于 06-26 17:43 ?104次閱讀

    harmony-utilsCacheUtil,緩存工具

    harmony-utilsCacheUtil,緩存工具
    的頭像 發(fā)表于 07-04 16:36 ?115次閱讀

    harmony-utilsCharUtil,字符工具

    harmony-utilsCharUtil,字符工具
    的頭像 發(fā)表于 07-04 16:34 ?118次閱讀

    harmony-utilsCrashUtil,異常相關(guān)工具

    harmony-utilsCrashUtil,異常相關(guān)工具
    的頭像 發(fā)表于 07-04 16:33 ?118次閱讀

    harmony-utilsDeviceUtil,設(shè)備相關(guān)工具

    harmony-utilsDeviceUtil,設(shè)備相關(guān)工具
    的頭像 發(fā)表于 07-03 18:27 ?153次閱讀

    harmony-utilsDisplayUtil,屏幕相關(guān)工具

    harmony-utilsDisplayUtil,屏幕相關(guān)工具
    的頭像 發(fā)表于 07-03 18:26 ?126次閱讀

    harmony-utilsEmitterUtil,Emitter工具

    harmony-utilsEmitterUtil,Emitter工具
    的頭像 發(fā)表于 07-03 18:24 ?133次閱讀

    harmony-utilsFileUtil,文件相關(guān)工具

    harmony-utilsFileUtil,文件相關(guān)工具
    的頭像 發(fā)表于 07-03 18:23 ?130次閱讀

    harmony-utilsFormatUtil,格式化工具

    harmony-utilsFormatUtil,格式化工具
    的頭像 發(fā)表于 07-03 18:22 ?130次閱讀

    harmony-utilsImageUtil,圖片相關(guān)工具

    harmony-utilsImageUtil,圖片相關(guān)工具
    的頭像 發(fā)表于 07-03 18:22 ?164次閱讀

    harmony-utilsNumberUtil,Number工具

    harmony-utilsNumberUtil,Number工具 harmony-utils 簡介與說明
    的頭像 發(fā)表于 07-03 18:08 ?132次閱讀

    harmony-utilsPreviewUtil,文件預(yù)覽工具

    harmony-utilsPreviewUtil,文件預(yù)覽工具 harmony-utils 簡介與說明 [
    的頭像 發(fā)表于 07-03 11:40 ?115次閱讀

    harmony-utilsStrUtil,字符串工具

    harmony-utilsStrUtil,字符串工具 harmony-utils 簡介與說明 [ha
    的頭像 發(fā)表于 07-03 11:32 ?91次閱讀

    harmony-utilsTypeUtil,類型檢查工具

    harmony-utilsTypeUtil,類型檢查工具 harmony-utils 簡介與說明 [
    的頭像 發(fā)表于 06-30 17:35 ?107次閱讀