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-utils之ArrayUtil,集合工具類
# harmony-utils之ArrayUtil,集合工具類 ## harmony-utils 簡介與說明 ------[
harmony-utils之AuthUtil,生物認證相關工具類
# harmony-utils之AuthUtil,生物認證相關工具類 ## harmony-utils 簡介與說明 ------[
harmony-utils之NumberUtil,Number工具類
harmony-utils之NumberUtil,Number工具類 harmony-utils 簡介與說明
harmony-utils之PreviewUtil,文件預覽工具類
harmony-utils之PreviewUtil,文件預覽工具類 harmony-utils 簡介與說明 [
harmony-utils之StrUtil,字符串工具類
harmony-utils之StrUtil,字符串工具類 harmony-utils 簡介與說明 [ha
harmony-utils之TypeUtil,類型檢查工具類
harmony-utils之TypeUtil,類型檢查工具類 harmony-utils 簡介與說明 [
評論