女人自慰AV免费观看内涵网,日韩国产剧情在线观看网址,神马电影网特片网,最新一级电影欧美,在线观看亚洲欧美日韩,黄色视频在线播放免费观看,ABO涨奶期羡澄,第一导航fulione,美女主播操b

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

    文章

    108

    瀏覽量

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

掃碼添加小助手

加入工程師交流群

    評論

    相關推薦
    熱點推薦

    harmony-utilsArrayUtil,集合工具

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

    harmony-utilsAuthUtil,生物認證相關工具

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

    harmony-utilsCacheUtil,緩存工具

    harmony-utilsCacheUtil,緩存工具
    的頭像 發表于 07-04 16:36 ?131次閱讀

    harmony-utilsCharUtil,字符工具

    harmony-utilsCharUtil,字符工具
    的頭像 發表于 07-04 16:34 ?129次閱讀

    harmony-utilsCrashUtil,異常相關工具

    harmony-utilsCrashUtil,異常相關工具
    的頭像 發表于 07-04 16:33 ?127次閱讀

    harmony-utilsDeviceUtil,設備相關工具

    harmony-utilsDeviceUtil,設備相關工具
    的頭像 發表于 07-03 18:27 ?164次閱讀

    harmony-utilsDisplayUtil,屏幕相關工具

    harmony-utilsDisplayUtil,屏幕相關工具
    的頭像 發表于 07-03 18:26 ?145次閱讀

    harmony-utilsEmitterUtil,Emitter工具

    harmony-utilsEmitterUtil,Emitter工具
    的頭像 發表于 07-03 18:24 ?144次閱讀

    harmony-utilsFileUtil,文件相關工具

    harmony-utilsFileUtil,文件相關工具
    的頭像 發表于 07-03 18:23 ?141次閱讀

    harmony-utilsFormatUtil,格式化工具

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

    harmony-utilsImageUtil,圖片相關工具

    harmony-utilsImageUtil,圖片相關工具
    的頭像 發表于 07-03 18:22 ?175次閱讀

    harmony-utilsNumberUtil,Number工具

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

    harmony-utilsPreviewUtil,文件預覽工具

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

    harmony-utilsStrUtil,字符串工具

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

    harmony-utilsTypeUtil,類型檢查工具

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