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)作不易,請給童長老點贊
審核編輯 黃宇
-
Harmony
+關(guān)注
關(guān)注
0文章
108瀏覽量
3014
發(fā)布評論請先 登錄
評論