bug修复

This commit is contained in:
xiaoxiao
2025-08-01 10:18:52 +08:00
parent 7618c0337d
commit 0253493285
19 changed files with 1340 additions and 32 deletions
@@ -27,6 +27,7 @@ export struct ChangePhotoGrids {
GridItem() {
Stack() {
Image(item.url ? (BasicConstant.urlHtml + item.url) : item.uri)
.alt($r('app.media.default_video'))
.width(80)
.height(80)
.objectFit(ImageFit.Fill)
@@ -0,0 +1,33 @@
import { notificationManager } from '@kit.NotificationKit';
import { WantAgent } from '@kit.AbilityKit';
import { image } from '@kit.ImageKit';
export class NotificationConfig {
notificationSlotType: notificationManager.SlotType = notificationManager.SlotType.SERVICE_INFORMATION; //通道类型,默认服务类型。
deliveryTime?: number; //通知发送时间。
tapDismissed: boolean = false; //通知是否自动清除。
autoDeletedTime?: number; //自动清除的时间(大于0,否者不显示角标)。
wantAgent?: WantAgent; //WantAgent封装了应用的行为意图,点击通知时触发该行为。
extraInfo?: Record<string, Object>; //扩展参数
isAlertOnce: boolean = false; //设置是否仅有一次此通知提醒。
isStopwatch: boolean = false; //是否显示已用时间。
isCountDown: boolean = false; //是否显示倒计时时间。
isFloatingIcon: boolean = true; //是否显示状态栏图标。
label?: string; //通知标签。
badgeIconStyle?: number; //通知角标类型。
showDeliveryTime: boolean = false; //是否显示分发时间。
actionButtons?: Array<notificationManager.NotificationActionButton>; //通知按钮,最多三个按钮。
smallIcon?: image.PixelMap; //通知小图标。可选字段,图像像素的总字节数不超过100KB。实际显示效果依赖于设备能力和通知中心UI样式。
largeIcon?: image.PixelMap; //通知大图标。可选字段,图像像素的总字节数不超过100KB。实际显示效果依赖于设备能力和通知中心UI样式。
groupName?: string; //组通知名称。
template?: notificationManager.NotificationTemplate; //通知模板。
distributedOption?: notificationManager.DistributedOptions; //分布式通知的选项。
removalWantAgent?: WantAgent; //当移除通知时,通知将被重定向到的WantAgent实例。当前不支持跳转UIAbility,只支持发布公共事件及跳转系统Service能力(WantAgent的OperationType >= 3)。
badgeNumber?: number = 1; //应用程序图标上显示的通知数。
appMessageId?: string; //应用发送通知携带的唯一标识字段, 用于通知去重。如果同一应用通过本地和云端等不同途径发布携带相同appMessageId的通知,设备只展示一条消息,之后收到的重复通知会被静默去重,不展示、不提醒。去重标识仅在通知发布的24小时内有效,超过24小时或者设备重启失效。
sound?: string; //应用通知自定义铃声,需要通过Push云侧获取自定义发送铃声权限后,该字段才会生效
additionalText?: string; //通知附加内容,是对通知内容的补充。
lockscreenPicture?: image.PixelMap; //通知在锁屏界面显示的图片。
}
@@ -0,0 +1,80 @@
import { WantAgent } from '@kit.AbilityKit';
import { notificationManager } from '@kit.NotificationKit';
import { image } from '@kit.ImageKit';
export interface NotificationOptions {
id?: number; //通知ID。
notificationSlotType?: notificationManager.SlotType; //通道类型。
showDeliveryTime?: boolean; //是否显示分发时间。
deliveryTime?: number; //通知发送时间。
tapDismissed?: boolean; //通知是否自动清除。
autoDeletedTime?: number; //自动清除的时间。
wantAgent?: WantAgent; //WantAgent封装了应用的行为意图,点击通知时触发该行为。
extraInfo?: Record<string, Object>; //扩展参数
isAlertOnce?: boolean; //设置是否仅有一次此通知提醒。
isStopwatch?: boolean; //是否显示已用时间。
isCountDown?: boolean; //是否显示倒计时时间。
isFloatingIcon?: boolean; //是否显示状态栏图标。
label?: string; //通知标签。
actionButtons?: Array<notificationManager.NotificationActionButton>; //通知按钮,最多三个按钮。
smallIcon?: image.PixelMap; //通知小图标。可选字段,图像像素的总字节数不超过100KB。实际显示效果依赖于设备能力和通知中心UI样式。
largeIcon?: image.PixelMap; //通知大图标。可选字段,图像像素的总字节数不超过100KB。实际显示效果依赖于设备能力和通知中心UI样式。
groupName?: string; //组通知名称。
template?: notificationManager.NotificationTemplate; //通知模板。
distributedOption?: notificationManager.DistributedOptions; //分布式通知的选项。
removalWantAgent?: WantAgent; //当移除通知时,通知将被重定向到的WantAgent实例。当前不支持跳转UIAbility,只支持发布公共事件及跳转系统Service能力(WantAgent的OperationType >= 3)。
badgeNumber?: number; //应用程序图标上显示的通知数。
appMessageId?: string; //应用发送通知携带的唯一标识字段, 用于通知去重。如果同一应用通过本地和云端等不同途径发布携带相同appMessageId的通知,设备只展示一条消息,之后收到的重复通知会被静默去重,不展示、不提醒。去重标识仅在通知发布的24小时内有效,超过24小时或者设备重启失效。
sound?: string; //应用通知自定义铃声,需要通过Push云侧获取自定义发送铃声权限后,该字段才会生效
}
/**
* 描述普通文本通知
*/
export interface NotificationBasicOptions extends NotificationOptions {
title: string; //通知标题(不可为空字符串)。
text: string; //通知内容(不可为空字符串)。
additionalText?: string; //通知附加内容,是对通知内容的补充。
lockscreenPicture?: image.PixelMap; //通知在锁屏界面显示的图片。
}
/**
* 描述长文本通知
*/
export interface NotificationLongTextOptions extends NotificationBasicOptions {
briefText: string; //通知概要内容,是对通知内容的总结(不可为空字符串)。
longText: string; //通知的长文本(不可为空字符串)。
expandedTitle: string; //通知展开时的标题(不可为空字符串)
}
/**
* 描述多行文本通知。
*/
export interface NotificationMultiLineOptions extends NotificationBasicOptions {
briefText: string; //通知概要内容,是对通知内容的总结(不可为空字符串)。
longTitle: string; //通知展开时的标题(不可为空字符串)。
lines: Array<string>; //通知的多行文本。
}
/**
* 描述附有图片的通知。
*/
export interface NotificationPictureOptions extends NotificationBasicOptions {
briefText: string; //通知概要内容,是对通知内容的总结(不可为空字符串)。
expandedTitle: string; //通知展开时的标题(不可为空字符串)。
picture: image.PixelMap; //通知的图片内容(图像像素的总字节数不能超过2MB)。
}
/**
* 描述模板的通知。
*/
export interface NotificationTemplateOptions extends NotificationBasicOptions {
// title: string; //通知标题 和 下载标题,共用一个字段。
fileName: string; //表示下载文件名。必填字段,值为字符串类型。
progressValue: number; //表示下载进度,值为数值类型。
}
@@ -0,0 +1,347 @@
import bundleManager from '@ohos.bundle.bundleManager'
import { BusinessError } from '@ohos.base'
import { KeyboardAvoidMode, window } from '@kit.ArkUI'
import { resourceManager } from '@kit.LocalizationKit'
import { common } from '@kit.AbilityKit'
export class AppUtil {
private static context: common.UIAbilityContext; //common.UIAbilityContext,上下文
/**
* 初始化方法,缓存全局变量,在UIAbility的onCreate方法中初始化该方法。
* @param windowStage 窗口管理器
*/
static init(context: common.UIAbilityContext) {
AppUtil.context = context;
}
/**
* 获取上下文,common.UIAbilityContext
* @returns
*/
static getContext(): common.UIAbilityContext {
if (!AppUtil.context) {
AppUtil.context = getContext() as common.UIAbilityContext; //兜底
console.error("请在UIAbility的onCreate方法中调用AppUtil的init方法初始化!");
}
return AppUtil.context;
}
/**
* 获取WindowStage
* @returns
*/
static getWindowStage(): window.WindowStage {
return AppUtil.getContext().windowStage;
}
/**
* 获取主窗口
*/
static getMainWindow(): window.Window {
return AppUtil.getContext().windowStage.getMainWindowSync();
}
/**
* 获取UIContext
* @returns
*/
static getUIContext(): UIContext {
return AppUtil.getMainWindow().getUIContext();
}
/**
* 设置灰阶,APP一键置灰。
* @param grayScale 该参数为浮点数,取值范围为[0.0, 1.0]。
* @param onlyMainWindow 是否只置灰主窗口,默认false。
* @returns
*/
static async setGrayScale(grayScale: number = 1.0, onlyMainWindow: boolean = false): Promise<void> {
AppUtil.getMainWindow().setWindowGrayScale(grayScale);
if (!onlyMainWindow) {
let subWindows = await AppUtil.getContext().windowStage.getSubWindow();
if (subWindows && subWindows.length > 0) {
subWindows.forEach((subWindow) => subWindow.setWindowGrayScale(grayScale));
}
}
}
/**
* 获取当前窗口的属性
* @param windowClass 不传该值,获取主窗口的属性
* @returns
*/
static getWindowProperties(windowClass: window.Window = AppUtil.getMainWindow()): window.WindowProperties {
return windowClass.getWindowProperties();
}
/**
* 获取虚拟键盘抬起时的页面避让模式(OFFSET-上抬模式、RESIZE-压缩模式)。
*/
static getKeyboardAvoidMode(): KeyboardAvoidMode {
let mode = AppUtil.getUIContext().getKeyboardAvoidMode();
if (typeof mode === 'string') {
if ('KeyBoardAvoidMode.RESIZE' === mode) {
return KeyboardAvoidMode.RESIZE;
} else {
return KeyboardAvoidMode.OFFSET;
}
}
return mode;
}
/**
* 设置虚拟键盘弹出时,页面的避让模式。
* @param value OFFSET-上抬模式、RESIZE-压缩模式)
*/
static setKeyboardAvoidMode(value: KeyboardAvoidMode): boolean {
try {
AppUtil.getUIContext().setKeyboardAvoidMode(value);
} catch (err) {
let error = err as BusinessError;
console.error(`AppUtil-setKeyboardAvoidMode-异常 ~ code: ${error.code} -·- message: ${error.message}`);
return false;
}
return true;
}
/**
* 设置窗口的显示方向属性,使用Promise异步回调。
* Orientation 窗口显示方向类型枚举:
* UNSPECIFIED 0 表示未定义方向模式,由系统判定。
* PORTRAIT 1 表示竖屏显示模式。
* LANDSCAPE 2 表示横屏显示模式。
* PORTRAIT_INVERTED 3 表示反向竖屏显示模式。
* LANDSCAPE_INVERTED 4 表示反向横屏显示模式。
* AUTO_ROTATION 5 表示传感器自动旋转模式。
* AUTO_ROTATION_PORTRAIT 6 表示传感器自动竖向旋转模式。
* AUTO_ROTATION_LANDSCAPE 7 表示传感器自动横向旋转模式。
* AUTO_ROTATION_RESTRICTED 8 表示受开关控制的自动旋转模式。
* AUTO_ROTATION_PORTRAIT_RESTRICTED 9 表示受开关控制的自动竖向旋转模式。
* AUTO_ROTATION_LANDSCAPE_RESTRICTED 10 表示受开关控制的自动横向旋转模式。
* LOCKED 11 表示锁定模式。
*/
static async setPreferredOrientation(orientation: window.Orientation,
windowClass: window.Window = AppUtil.getMainWindow()): Promise<void> {
return windowClass.setPreferredOrientation(orientation);
}
/**
* 设置屏幕亮度值,使用Promise异步回调。
* @param brightness 屏幕亮度值。该参数为浮点数,取值范围为[0.0, 1.0]或-1.0。1.0表示最亮,-1.0表示默认亮度。
* @returns
*/
static async setWindowBrightness(brightness: number,
windowClass: window.Window = AppUtil.getMainWindow()): Promise<void> {
return windowClass.setWindowBrightness(brightness);
}
/**
* 设置屏幕是否为常亮状态,使用Promise异步回调。
* @param isKeepScreenOn true表示常亮;false表示不常亮。
* @returns
*/
static async setWindowKeepScreenOn(isKeepScreenOn: boolean,
windowClass: window.Window = AppUtil.getMainWindow()): Promise<void> {
return windowClass.setWindowKeepScreenOn(isKeepScreenOn);
}
/**
* 设置窗口是否为隐私模式。设置为隐私模式的窗口,窗口内容将无法被截屏或录屏。
* @param isPrivacyMode 窗口是否为隐私模式。true表示模式开启;false表示模式关闭。
* @returns
*/
static async setWindowPrivacyMode(isPrivacyMode: boolean,
windowClass: window.Window = AppUtil.getMainWindow()): Promise<void> {
return windowClass.setWindowPrivacyMode(isPrivacyMode);
}
/**
* 设置窗口的背景色。Stage模型下,该接口需要在loadContent()或setUIContent()调用生效后使用。
* @param color 需要设置的背景色,为十六进制RGB或ARGB颜色,不区分大小写,例如#00FF00或#FF00FF00。
* @returns
*/
static setWindowBackgroundColor(color: string, windowClass: window.Window = AppUtil.getMainWindow()) {
try {
windowClass.setWindowBackgroundColor(color);
} catch (err) {
let error = err as BusinessError;
console.error(`AppUtil-setWindowBackgroundColor-异常 ~ code: ${error.code} -·- message: ${error.message}`);
}
}
/**
* 设置点击时是否支持切换焦点窗口,使用Promise异步回调。
* @param isFocusable 点击时是否支持切换焦点窗口。true表示支持;false表示不支持。
* @returns
*/
static async setWindowFocusable(isFocusable: boolean,
windowClass: window.Window = AppUtil.getMainWindow()): Promise<void> {
return windowClass.setWindowFocusable(isFocusable);
}
/**
* 设置窗口是否为可触状态,使用Promise异步回调。
* @param isTouchable 窗口是否为可触状态。true表示可触;false表示不可触。
* @returns
*/
static async setWindowTouchable(isTouchable: boolean,
windowClass: window.Window = AppUtil.getMainWindow()): Promise<void> {
return windowClass.setWindowTouchable(isTouchable);
}
/**
* 获取状态栏的高度,单位为px。
* @returns
*/
static getStatusBarHeight(): number {
try {
const windowClass = AppUtil.getMainWindow();
const avoidArea = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);
return avoidArea.topRect.height;
} catch (err) {
let error = err as BusinessError;
console.error(`AppUtil-getStatusBarHeight-异常 ~ code: ${error.code} -·- message: ${error.message}`);
return 0;
}
}
/**
* 获取底部导航条的高度,单位为px。
* @returns
*/
static getNavigationIndicatorHeight(): number {
try {
const windowClass = AppUtil.getMainWindow();
const avoidArea = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR);
return avoidArea.bottomRect.height;
} catch (err) {
let error = err as BusinessError;
console.error(`AppUtil-getNavigationIndicatorHeight-异常 ~ code: ${error.code} -·- message: ${error.message}`);
return 0;
}
}
/**
* 设置沉浸式状态栏
* @param isLayoutFullScreen 窗口的布局是否为沉浸式布局(该沉浸式布局状态栏、导航栏仍然显示)。true表示沉浸式布局;false表示非沉浸式布局。
* @param enable 设置窗口全屏模式时状态栏、导航栏或底部导航条是否显示,true表示显示 false表示隐藏。
* @param color 设置窗口的背景颜色。
* @param systemBarProperties 状态栏、导航栏的属性:
* statusBarColor 状态栏背景颜色,为十六进制RGB或ARGB颜色,不区分大小写,例如#00FF00或#FF00FF00。默认值:#0x66000000。
* statusBarContentColor 状态栏文字颜色。当设置此属性后, isStatusBarLightIcon属性设置无效。默认值:#0xE5FFFFFF。
* isStatusBarLightIcon 状态栏图标是否为高亮状态。true表示高亮;false表示不高亮。默认值:false。
* navigationBarColor 导航栏背景颜色,为十六进制RGB或ARGB颜色,不区分大小写,例如#00FF00或#FF00FF00。默认值:#0x66000000。
* navigationBarContentColor 导航栏文字颜色。当设置此属性后, isNavigationBarLightIcon属性设置无效。默认值:#0xE5FFFFFF。
* isNavigationBarLightIcon 导航栏图标是否为高亮状态。true表示高亮;false表示不高亮。默认值:false。
*/
static setStatusBar(isLayoutFullScreen: boolean = true, enable: boolean = true, color: string = '#FFFFFF', systemBarProperties?: window.SystemBarProperties) {
try {
const windowClass = AppUtil.getMainWindow();
windowClass.setWindowLayoutFullScreen(isLayoutFullScreen).then(() => {
windowClass.setWindowBackgroundColor(color);
}).catch((error: BusinessError) => {
console.error(`AppUtil-setStatusBar-异常 ~ code: ${error.code} -·- message: ${error.message}`);
});
windowClass.setWindowSystemBarEnable(enable ? ['status', 'navigation'] : []).then(() => {
windowClass.setSpecificSystemBarEnabled("navigationIndicator", enable); //底部导航条。
}).catch((error: BusinessError) => {
console.error(`AppUtil-setStatusBar-异常 ~ code: ${error.code} -·- message: ${error.message}`);
});
if (systemBarProperties) {
windowClass.setWindowSystemBarProperties(systemBarProperties).catch((error: BusinessError) => {
console.error(`AppUtil-setStatusBar-异常 ~ code: ${error.code} -·- message: ${error.message}`);
});
}
} catch (err) {
let error = err as BusinessError;
console.error(`AppUtil-setStatusBar-异常 ~ code: ${error.code} -·- message: ${error.message}`);
}
}
/**
* 获取当前应用的BundleInfo
*/
static async getBundleInfo(): Promise<bundleManager.BundleInfo> {
return bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION);
}
/**
* 获取当前应用的BundleInfo
*/
static getBundleInfoSync(): bundleManager.BundleInfo {
return bundleManager.getBundleInfoForSelfSync(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION);
}
/**
* 获取应用包的名称。
*/
static getBundleName(): string {
return AppUtil.getBundleInfoSync().name;
}
/**
* 获取应用版本号。
*/
static getVersionCode(): number {
return AppUtil.getBundleInfoSync().versionCode;
}
/**
* 获取应用版本名。
*/
static getVersionName(): string {
return AppUtil.getBundleInfoSync().versionName;
}
/**
* 获取运行应用包所需要最高SDK版本号。
*/
static getTargetVersion(): number {
return AppUtil.getBundleInfoSync().targetVersion;
}
/**
* 获取应用程序的配置信息
* @returns
*/
static async getAppInfo(): Promise<bundleManager.ApplicationInfo> {
return (await AppUtil.getBundleInfo()).appInfo;
}
/**
* 获取应用程序的配置信息
* @returns
*/
static getAppInfoSync(): bundleManager.ApplicationInfo {
return AppUtil.getBundleInfoSync().appInfo;
}
/**
* 主动退出整个应用;调用该方法后,任务中心的任务默认不会清理,如需清理,需要配置removeMissionAfterTerminate为true。
*/
static exit() {
AppUtil.getContext().terminateSelf();
AppUtil.getContext().getApplicationContext().killAllProcesses();
}
}
@@ -259,7 +259,7 @@ export class ChangeUtil {
return new Promise((resolve, reject) => {
const httpRequest = http.createHttp();
httpRequest.request(
BasicConstant.urlImage+url,
url,
{
method: http.RequestMethod.GET
},
@@ -0,0 +1,258 @@
import fs from '@ohos.file.fs';
import { image } from '@kit.ImageKit';
import { resourceManager } from '@kit.LocalizationKit';
import { Base64Util } from './Base64Util';
export class ImageUtil {
/**
* 图片base64字符串转PixelMap
* @param base64 图片base64字符串
* @returns
*/
static base64ToPixelMap(base64: string): Promise<image.PixelMap> {
//将原始图片base64字符串转变为通过base64字符串
const reg = new RegExp('data:image/\\w+;base64,');
const base64Str = base64.replace(reg, '');
//将通用base64字符串转变为arrayBuffer
let arrayBuffer = Base64Util.decodeSync(base64Str).buffer as ArrayBuffer;
//将arrayBuffer转变为pixelMap
let imageSource = image.createImageSource(arrayBuffer);
let opts: image.DecodingOptions = { editable: false }
//注意:这里return的是Promise,因此使用时需要在业务侧拿到最终的PixelMap
return imageSource.createPixelMap(opts);
}
/**
* 创建图片源实例
* @param src(联合类型: string、number、ArrayBuffer、resourceManager.RawFileDescriptor
* path string 图片路径,当前仅支持应用沙箱路径。当前支持格式有:.jpg .png .gif .bmp .webp RAW SVG10+ .ico11+。
* fd number 文件描述符fd。
* buf ArrayBuffer 图像缓冲区数组。
* rawfile resourceManager.RawFileDescriptor 图像资源文件的RawFileDescriptor。
* options SourceOptions 图片属性,包括图片像素密度、像素格式和图片尺寸。
* sourceDensity number ImageSource的密度。
* sourcePixelFormat PixelMapFormat 图片像素格式。
* sourceSize Size 图像像素大小。
* @returns
*/
static createImageSource(src: string | number | ArrayBuffer | resourceManager.RawFileDescriptor, options?: image.SourceOptions): image.ImageSource {
if (typeof src === 'string') {
if (options) {
return image.createImageSource(src, options);
} else {
return image.createImageSource(src);
}
} else if (typeof src === 'number') {
if (options) {
return image.createImageSource(src, options);
} else {
return image.createImageSource(src);
}
} else if (src instanceof ArrayBuffer) {
if (options) {
return image.createImageSource(src, options);
} else {
return image.createImageSource(src);
}
} else {
if (options) {
return image.createImageSource(src, options);
} else {
return image.createImageSource(src);
}
}
}
/**
* 以增量的方式创建图片源实例
* @param buf ArrayBuffer 增量数据
* @param options SourceOptions 图片属性,包括图片像素密度、像素格式和图片尺寸。
* sourceDensity number ImageSource的密度。
* sourcePixelFormat PixelMapFormat 图片像素格式。
* sourceSize Size 图像像素大小。
* @returns
*/
static createIncrementalSource(buf: ArrayBuffer, options?: image.SourceOptions): image.ImageSource {
if (options) {
return image.CreateIncrementalSource(buf, options);
} else {
return image.CreateIncrementalSource(buf);
}
}
/**
* 图片压缩或重新打包,使用Promise形式返回结果。
* @param source PixelMap-打包的PixelMap资源。
* @param options 设置打包参数:
* format 目标格式。当前只支持"image/jpeg"、"image/webp"、"image/png"和"image/heif"12+(不同硬件设备支持情况不同)。
* quality JPEG编码中设定输出图片质量的参数,取值范围为0-100。
* bufferSize 接收编码数据的缓冲区大小,单位为Byte。默认为10MB。bufferSize需大于编码后图片大小。
* @returns
*/
static packingFromPixelMap(source: image.PixelMap, options: image.PackingOption): Promise<ArrayBuffer> {
const imagePacker: image.ImagePacker = image.createImagePacker();
return imagePacker.packing(source, options).finally(() => {
imagePacker.release(); //释放
});
}
/**
* 图片压缩或重新打包,使用Promise形式返回结果。
* @param source ImageSource-打包的图片源。
* @param options 设置打包参数:
* format 目标格式。当前只支持"image/jpeg"、"image/webp"、"image/png"和"image/heif"12+(不同硬件设备支持情况不同)。
* quality JPEG编码中设定输出图片质量的参数,取值范围为0-100。
* bufferSize 接收编码数据的缓冲区大小,单位为Byte。默认为10MB。bufferSize需大于编码后图片大小。
* @returns
*/
static packingFromImageSource(source: image.ImageSource, options: image.PackingOption): Promise<ArrayBuffer> {
const imagePacker: image.ImagePacker = image.createImagePacker();
return imagePacker.packing(source, options).finally(() => {
imagePacker.release(); //释放
});
}
/**
* 将PixelMap图片源编码后直接打包进文件。
* @param source PixelMap-打包的PixelMap资源。
* @param fd 文件描述符。
* @param option 设置打包参数:
* format 目标格式。当前只支持"image/jpeg"、"image/webp"、"image/png"和"image/heif"12+(不同硬件设备支持情况不同)。
* quality JPEG编码中设定输出图片质量的参数,取值范围为0-100。
* bufferSize 接收编码数据的缓冲区大小,单位为Byte。默认为10MB。bufferSize需大于编码后图片大小。
* @returns
*/
static packToFileFromPixelMap(source: image.PixelMap, fd: number, options: image.PackingOption): Promise<void> {
const imagePacker: image.ImagePacker = image.createImagePacker();
return imagePacker.packToFile(source, fd, options).finally(() => {
imagePacker.release(); //释放
});
}
/**
* 将ImageSource图片源编码后直接打包进文件。
* @param source ImageSource-打包的图片源。
* @param fd 文件描述符。
* @param option 设置打包参数:
* format 目标格式。当前只支持"image/jpeg"、"image/webp"、"image/png"和"image/heif"12+(不同硬件设备支持情况不同)。
* quality JPEG编码中设定输出图片质量的参数,取值范围为0-100。
* bufferSize 接收编码数据的缓冲区大小,单位为Byte。默认为10MB。bufferSize需大于编码后图片大小。
* @returns
*/
static packToFileFromImageSource(source: image.ImageSource, fd: number, options: image.PackingOption): Promise<void> {
const imagePacker: image.ImagePacker = image.createImagePacker();
return imagePacker.packToFile(source, fd, options).finally(() => {
imagePacker.release(); //释放
});
}
/**
* 图片压缩
* @param sourcePixelMap:原始待压缩图片的PixelMap对象
* @param maxSize:指定图片的压缩目标大小,单位kb
* @param imageFormat:当前只支持"image/jpeg"、"image/webp"、"image/png"和"image/heif"12+(不同硬件设备支持情况不同)。
* @returns compressedImageData:返回压缩后的图片数据
*/
static async compressedImage(sourcePixelMap: image.PixelMap, maxImgSize: number, imageFormat: string = "image/jpeg"): Promise<ArrayBuffer> {
const imagePackerApi = image.createImagePacker(); //创建图像编码ImagePacker对象
const IMAGE_QUALITY = 0;
const packOpts: image.PackingOption = { format: imageFormat, quality: IMAGE_QUALITY };
//通过PixelMap进行编码。compressedImageData为打包获取到的图片文件流。
let compressedImageData: ArrayBuffer = await imagePackerApi.packing(sourcePixelMap, packOpts);
const maxCompressedImageByte = maxImgSize * 1024; //压缩目标图像字节长度
//图片压缩。先判断设置图片质量参数quality为0时,packing能压缩到的图片最小字节大小是否满足指定的图片压缩大小。如果满足,则使用packing方式二分查找最接近指定图片压缩目标大小的quality来压缩图片。如果不满足,则使用scale对图片先进行缩放,采用while循环每次递减0.4倍缩放图片,再用packing(图片质量参数quality设置0)获取压缩图片大小,最终查找到最接近指定图片压缩目标大小的缩放倍数的图片压缩数据。
if (maxCompressedImageByte > compressedImageData.byteLength) {
//使用packing二分压缩获取图片文件流
compressedImageData = await ImageUtil.packingImage(compressedImageData, sourcePixelMap, maxCompressedImageByte, IMAGE_QUALITY, imageFormat);
} else {
//使用scale对图片先进行缩放,采用while循环每次递减0.4倍缩放图片,再用packing(图片质量参数quality设置0)获取压缩图片大小,最终查找到最接近指定图片压缩目标大小的缩放倍数的图片压缩数据
let imageScale = 1;
const REDUCE_SCALE = 0.4;
//判断压缩后的图片大小是否大于指定图片的压缩目标大小,如果大于,继续降低缩放倍数压缩。
while (compressedImageData.byteLength > maxCompressedImageByte) {
if (imageScale > 0) {
//性能知识点: 由于scale会直接修改图片PixelMap数据,所以不适用二分查找scale缩放倍数。这里采用循环递减0.4倍缩放图片,来查找确定最适合的缩放倍数。如果对图片压缩质量要求不高,建议调高每次递减的缩放倍数reduceScale,减少循环,提升scale压缩性能。
imageScale = imageScale - REDUCE_SCALE;
await sourcePixelMap.scale(imageScale, imageScale);
compressedImageData = await ImageUtil.packing(sourcePixelMap, IMAGE_QUALITY, imageFormat);
} else {
//imageScale缩放小于等于0时,没有意义,结束压缩。这里不考虑图片缩放倍数小于reduceScale的情况。
break;
}
}
}
imagePackerApi.release();
return compressedImageData;
}
/**
* packing压缩
* @param sourcePixelMap:原始待压缩图片的PixelMap
* @param imageQuality:图片质量参数
* @param imageFormat:当前只支持"image/jpeg"、"image/webp"、"image/png"和"image/heif"12+(不同硬件设备支持情况不同)。
* @returns data:返回压缩后的图片数据
*/
static async packing(sourcePixelMap: image.PixelMap, imageQuality: number, imageFormat: string = "image/jpeg"): Promise<ArrayBuffer> {
const imagePackerApi = image.createImagePacker();
const packOpts: image.PackingOption = { format: imageFormat, quality: imageQuality };
const data: ArrayBuffer = await imagePackerApi.packing(sourcePixelMap, packOpts);
imagePackerApi.release()
return data;
}
/**
* packing二分方式循环压缩
* @param compressedImageData:图片压缩的ArrayBuffer
* @param sourcePixelMap:原始待压缩图片的PixelMap
* @param maxCompressedImageByte:压缩目标图像字节长度
* @param imageQuality:图片质量参数
* @param imageFormat:当前只支持"image/jpeg"、"image/webp"、"image/png"和"image/heif"12+(不同硬件设备支持情况不同)。
* @returns compressedImageData:返回二分packing压缩后的图片数据
*/
static async packingImage(compressedImageData: ArrayBuffer, sourcePixelMap: image.PixelMap, maxCompressedImageByte: number,
imageQuality: number, imageFormat: string = "image/jpeg"): Promise<ArrayBuffer> {
//图片质量参数范围为0-100,这里以10为最小二分单位创建用于packing二分图片质量参数的数组。
const packingArray: number[] = [];
const DICHOTOMY_ACCURACY = 10;
//性能知识点: 如果对图片压缩质量要求不高,建议调高最小二分单位dichotomyAccuracy,减少循环,提升packing压缩性能。
for (let i = 0; i <= 100; i += DICHOTOMY_ACCURACY) {
packingArray.push(i);
}
let left = 0;
let right = packingArray.length - 1;
//二分压缩图片
while (left <= right) {
const mid = Math.floor((left + right) / 2);
imageQuality = packingArray[mid];
//根据传入的图片质量参数进行packing压缩,返回压缩后的图片文件流数据。
compressedImageData = await ImageUtil.packing(sourcePixelMap, imageQuality, imageFormat);
//判断查找一个尽可能接近但不超过压缩目标的压缩大小
if (compressedImageData.byteLength <= maxCompressedImageByte) {
left = mid + 1;
if (mid === packingArray.length - 1) {
break;
}
//获取下一次二分的图片质量参数(mid+1)压缩的图片文件流数据
compressedImageData = await ImageUtil.packing(sourcePixelMap, packingArray[mid + 1], imageFormat);
//判断用下一次图片质量参数(mid+1)压缩的图片大小是否大于指定图片的压缩目标大小。如果大于,说明当前图片质量参数(mid)压缩出来的图片大小最接近指定图片的压缩目标大小。传入当前图片质量参数mid,得到最终目标图片压缩数据。
if (compressedImageData.byteLength > maxCompressedImageByte) {
compressedImageData = await ImageUtil.packing(sourcePixelMap, packingArray[mid], imageFormat);
break;
}
} else {
//目标值不在当前范围的右半部分,将搜索范围的右边界向左移动,以缩小搜索范围并继续在下一次迭代中查找左半部分。
right = mid - 1;
}
}
return compressedImageData;
}
}
@@ -0,0 +1,549 @@
import notificationManager from '@ohos.notificationManager';
import common from '@ohos.app.ability.common';
import wantAgent from '@ohos.app.ability.wantAgent';
import { WantAgent } from '@ohos.wantAgent';
import {
NotificationBasicOptions,
NotificationLongTextOptions,
NotificationMultiLineOptions,
NotificationOptions,
NotificationPictureOptions,
NotificationTemplateOptions
} from '../pushnotification/NotificationOptions';
import { NotificationConfig } from '../pushnotification/NotificationConfig';
import { image } from '@kit.ImageKit';
import { ImageUtil } from './ImageUtil';
import { AppUtil } from './AppUtil';
import { systemDateTime } from '@kit.BasicServicesKit';
export class NotificationUtil {
private static defaultConfig: NotificationConfig = new NotificationConfig(); //默认配置
/**
* 设置通知的默认统一配置
* @param configs
*/
static setDefaultConfig(configs: (config: NotificationConfig) => void): void {
configs(NotificationUtil.defaultConfig);
}
/**
* 查询通知是否授权
*/
static async isNotificationEnabled(): Promise<boolean> {
return notificationManager.isNotificationEnabled(); //查询通知是否授权。
}
/**
* 查询通知是否授权
*/
static isNotificationEnabledSync(): boolean {
return notificationManager.isNotificationEnabledSync(); //查询通知是否授权。
}
/**
* 请求通知授权,第一次调用会弹窗让用户选择。
* @returns
*/
static async authorizeNotification(callBack?: (grant: boolean) => void): Promise<boolean> {
let isEnabled = await NotificationUtil.isNotificationEnabled(); //查询通知是否授权
if (!isEnabled) { //未授权,拉起授权
try {
await notificationManager.requestEnableNotification(AppUtil.getContext());
callBack?.(true);
return true;
} catch (e) {
callBack?.(false);
return false;
}
} else {
callBack?.(true);
return true;
}
}
/**
* 查询模板是否存在,目前仅支持进度条模板。
* @param templateName 模板名称。当前仅支持'downloadTemplate'。
* @returns
*/
static async isSupportTemplate(templateName: string = 'downloadTemplate'): Promise<boolean> {
return notificationManager.isSupportTemplate(templateName);
}
/**
* 查询设备是否支持分布式通知
* @returns
*/
static async isDistributedEnabled(): Promise<boolean> {
return notificationManager.isDistributedEnabled()
}
/**
* 发布普通文本通知
* @param options 通知实体
* @returns
*/
static async publishBasic(options: NotificationBasicOptions): Promise<number> {
const notificationId: number = options.id ?? NotificationUtil.generateNotificationId(); //通知ID。
const basicContent: notificationManager.NotificationBasicContent = {
title: options.title,
text: options.text
}
if (options.additionalText || NotificationUtil.defaultConfig.additionalText) {
basicContent.additionalText = options.additionalText ?? NotificationUtil.defaultConfig.additionalText;
}
if (options.lockscreenPicture || NotificationUtil.defaultConfig.lockscreenPicture) {
basicContent.lockscreenPicture = options.lockscreenPicture ?? NotificationUtil.defaultConfig.lockscreenPicture;
}
let notificationContent: notificationManager.NotificationContent = {
notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
normal: basicContent
}
//通知Request对象
const notificationRequest = NotificationUtil.getNotificationRequest(notificationId, options, notificationContent);
await notificationManager.publish(notificationRequest); //发送通知
return notificationId
}
/**
* 发布长文本通知
* @param options 通知实体
* @returns
*/
static async publishLongText(options: NotificationLongTextOptions): Promise<number> {
const notificationId: number = options.id ?? NotificationUtil.generateNotificationId(); //通知ID。
const longTextContent: notificationManager.NotificationLongTextContent = {
title: options.title,
text: options.text,
briefText: options.briefText,
longText: options.longText,
expandedTitle: options.expandedTitle
}
if (options.additionalText || NotificationUtil.defaultConfig.additionalText) {
longTextContent.additionalText = options.additionalText ?? NotificationUtil.defaultConfig.additionalText;
}
if (options.lockscreenPicture || NotificationUtil.defaultConfig.lockscreenPicture) {
longTextContent.lockscreenPicture = options.lockscreenPicture ?? NotificationUtil.defaultConfig.lockscreenPicture;
}
let notificationContent: notificationManager.NotificationContent = {
notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_LONG_TEXT,
longText: longTextContent
}
//通知Request对象
const notificationRequest = NotificationUtil.getNotificationRequest(notificationId, options, notificationContent);
await notificationManager.publish(notificationRequest); //发送通知
return notificationId;
}
/**
* 发布多文本通知
* @param options 通知实体
* @returns
*/
static async publishMultiLine(options: NotificationMultiLineOptions): Promise<number> {
const notificationId: number = options.id ?? NotificationUtil.generateNotificationId(); //通知ID。
const multiLineContent: notificationManager.NotificationMultiLineContent = {
title: options.title,
text: options.text,
briefText: options.briefText,
longTitle: options.longTitle,
lines: options.lines
}
if (options.additionalText || NotificationUtil.defaultConfig.additionalText) {
multiLineContent.additionalText = options.additionalText ?? NotificationUtil.defaultConfig.additionalText;
}
if (options.lockscreenPicture || NotificationUtil.defaultConfig.lockscreenPicture) {
multiLineContent.lockscreenPicture = options.lockscreenPicture ?? NotificationUtil.defaultConfig.lockscreenPicture;
}
let notificationContent: notificationManager.NotificationContent = {
notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_MULTILINE,
multiLine: multiLineContent
}
//通知Request对象
const notificationRequest = NotificationUtil.getNotificationRequest(notificationId, options, notificationContent);
await notificationManager.publish(notificationRequest); //发送通知
return notificationId;
}
/**
* 发布带有图片的通知
* @param options 通知实体
* @returns
*/
static async publishPicture(options: NotificationPictureOptions): Promise<number> {
const notificationId: number = options.id ?? NotificationUtil.generateNotificationId(); //通知ID。
const pictureContent: notificationManager.NotificationPictureContent = {
title: options.title,
text: options.text,
briefText: options.briefText,
expandedTitle: options.expandedTitle,
picture: options.picture
}
if (options.additionalText || NotificationUtil.defaultConfig.additionalText) {
pictureContent.additionalText = options.additionalText ?? NotificationUtil.defaultConfig.additionalText;
}
if (options.lockscreenPicture || NotificationUtil.defaultConfig.lockscreenPicture) {
pictureContent.lockscreenPicture = options.lockscreenPicture ?? NotificationUtil.defaultConfig.lockscreenPicture;
}
let notificationContent: notificationManager.NotificationContent = {
notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_PICTURE,
picture: pictureContent
}
//通知Request对象
const notificationRequest = NotificationUtil.getNotificationRequest(notificationId, options, notificationContent);
await notificationManager.publish(notificationRequest); //发送通知
return notificationId;
}
/**
* 发布模板的通知
* @param options
*/
static async publishTemplate(options: NotificationTemplateOptions): Promise<number> {
const notificationId: number = options.id ?? NotificationUtil.generateNotificationId(); //通知ID。
const basicContent: notificationManager.NotificationBasicContent = {
title: options.title,
text: options.text
}
if (options.additionalText || NotificationUtil.defaultConfig.additionalText) {
basicContent.additionalText = options.additionalText ?? NotificationUtil.defaultConfig.additionalText;
}
if (options.lockscreenPicture || NotificationUtil.defaultConfig.lockscreenPicture) {
basicContent.lockscreenPicture = options.lockscreenPicture ?? NotificationUtil.defaultConfig.lockscreenPicture;
}
let notificationContent: notificationManager.NotificationContent = {
notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
normal: basicContent
}
//模板对象
let template: notificationManager.NotificationTemplate = {
name: 'downloadTemplate',
data: { title: options.title, fileName: options.fileName, progressValue: options.progressValue }
}
//通知Request对象
const notificationRequest = NotificationUtil.getNotificationRequest(notificationId, options, notificationContent, template);
await notificationManager.publish(notificationRequest); //发送通知
return notificationId;
}
/**
* 取消通知,通过通知ID和通知标签取消已发布的通知,若label为空表示取消与指定通知ID相匹配的已发布通知。
* @param id 通知id
* @param label 通知标签,默认为空。
* @returns
*/
static async cancel(id: number, label?: string): Promise<void> {
return notificationManager.cancel(id, label);
}
/**
* 取消本应用指定组下的通知
* @param groupName 通知组名称,此名称需要在发布通知时通过NotificationRequest对象指定。
* @returns
*/
static async cancelGroup(groupName: string): Promise<void> {
return notificationManager.cancelGroup(groupName);
}
/**
* 取消所有通知,取消当前应用所有已发布的通知。
* @returns
*/
static async cancelAll(): Promise<void> {
return notificationManager.cancelAll()
}
/**
* 设置桌面角标个数,在应用的桌面图标上呈现。
*/
static async setBadge(badgeNumber: number): Promise<void> {
return notificationManager.setBadgeNumber(badgeNumber);
}
/**
* 清空桌面角标,在应用的桌面图标上呈现。
*/
static async clearBadge(): Promise<void> {
return notificationManager.setBadgeNumber(0);
}
/**
* 获取当前应用未删除的通知数量。
*/
static async getActiveNotificationCount(): Promise<number> {
return notificationManager.getActiveNotificationCount();
}
/**
* 设置桌面角标数量,来自于通知数量。
*/
static async setBadgeFromNotificationCount(): Promise<void> {
let count = await NotificationUtil.getActiveNotificationCount();
return notificationManager.setBadgeNumber(count); //设置角标
}
/**
* 获取当前应用未删除的通知列表。
*/
static async getActiveNotifications(): Promise<Array<notificationManager.NotificationRequest>> {
return notificationManager.getActiveNotifications();
}
/**
* 创建指定类型的通知渠道。
* @param type 要创建的通知渠道的类型
*/
static async addSlot(type: notificationManager.SlotType) {
return notificationManager.addSlot(type);
}
/**
* 获取一个指定类型的通知渠道。
* @param type 要创建的通知渠道的类型
*/
static async getSlot(type: notificationManager.SlotType): Promise<notificationManager.NotificationSlot> {
return notificationManager.getSlot(type);
}
/**
* 获取此应用程序的所有通知渠道
* @returns
*/
static async getSlots(): Promise<Array<notificationManager.NotificationSlot>> {
return notificationManager.getSlots();
}
/**
* 删除此应用程序指定类型的通知渠道
* @param type 通知渠道类型,例如社交通信、服务提醒、内容咨询等类型。
* @returns
*/
static async removeSlot(type: notificationManager.SlotType) {
return notificationManager.removeSlot(type);
}
/**
* 删除此应用程序所有通知渠道
* @returns
*/
static async removeAllSlots() {
return notificationManager.removeAllSlots();
}
/**
* 生成通知id(用时间戳当id)
* @returns
*/
static generateNotificationId(): number {
return systemDateTime.getTime(true);
}
/**
* 获取压缩通知的图片(图像像素的总字节数不能超过2MB)
* @param pixelMap:原始待压缩图片的PixelMap对象
* @returns 返回压缩后的图片数据
*/
static async getCompressedPicture(src: Resource | image.PixelMap): Promise<PixelMap> {
if (typeof (src as Resource).bundleName == 'string') {
// src = await ImageUtil.getPixelMapFromMedia((src as Resource));
}
let pixelMap = src as image.PixelMap;
let pictureMaxSize = 2 * 1024 * 1024; //通知的图片内容(图像像素的总字节数不能超过2MB)。
let pixelBytes = pixelMap.getPixelBytesNumber(); //图像像素的总字节数
while (pixelBytes > pictureMaxSize) {
await pixelMap.scale(0.7, 0.7, image.AntiAliasingLevel.LOW); //缩放
pixelBytes = pixelMap.getPixelBytesNumber(); //图像像素的总字节数
}
return pixelMap;
}
/**
* 获取压缩通知图标(图标像素的总字节数不超过192KB)
* @param pixelMap:原始待压缩图片的PixelMap对象
* @returns
*/
static async getCompressedIcon(src: Resource | image.PixelMap): Promise<PixelMap> {
if (typeof (src as Resource).bundleName == 'string') {
// src = await ImageUtil.getPixelMapFromMedia((src as Resource));
}
let pixelMap = src as image.PixelMap;
let pictureMaxSize = 192 * 1024; //通知图标(图标像素的总字节数不超过192KB)。
let pixelBytes = pixelMap.getPixelBytesNumber(); //图像像素的总字节数
while (pixelBytes > pictureMaxSize) {
await pixelMap.scale(0.7, 0.7, image.AntiAliasingLevel.LOW); //缩放
pixelBytes = pixelMap.getPixelBytesNumber(); //图像像素的总字节数
}
return pixelMap;
}
/**
* 创建一个可拉起Ability的Want
* @returns
*/
static async getDefaultWantAgent(): Promise<WantAgent> {
const context = getContext() as common.UIAbilityContext; //获取当前上下文对象
const wantAgentInfo: wantAgent.WantAgentInfo = {
wants: [
{
deviceId: '',
bundleName: context.abilityInfo.bundleName,
moduleName: context.abilityInfo.moduleName,
abilityName: context.abilityInfo.name,
action: 'action_notice',
entities: [],
uri: '',
}
],
actionType: wantAgent.OperationType.START_ABILITY | wantAgent.OperationType.SEND_COMMON_EVENT,
requestCode: 0,
actionFlags: [wantAgent.WantAgentFlags.CONSTANT_FLAG]
};
return wantAgent.getWantAgent(wantAgentInfo);
}
/**
* 构建NotificationRequest对象
* @param notificationId 通知ID
* @param options 描述通知的请求参数
* @param content 通知内容
* @param template 模板
* @returns
*/
private static getNotificationRequest(notificationId: number, options: NotificationOptions,
content: notificationManager.NotificationContent, template?: notificationManager.NotificationTemplate): notificationManager.NotificationRequest {
const request: notificationManager.NotificationRequest = { content: content };
request.id = notificationId; //通知ID。
if (template) {
request.template = template; //模板
}
if (options.notificationSlotType || NotificationUtil.defaultConfig.notificationSlotType) {
request.notificationSlotType = options.notificationSlotType ?? NotificationUtil.defaultConfig.notificationSlotType; //通道类型。
}
if (options.deliveryTime || NotificationUtil.defaultConfig.deliveryTime) {
request.deliveryTime = options.deliveryTime ?? NotificationUtil.defaultConfig.deliveryTime; //通知发送时间。
}
request.showDeliveryTime = options.showDeliveryTime ?? NotificationUtil.defaultConfig.showDeliveryTime; //是否显示分发时间。
request.tapDismissed = options.tapDismissed ?? NotificationUtil.defaultConfig.tapDismissed; //通知是否自动清除。
if (options.autoDeletedTime || NotificationUtil.defaultConfig.autoDeletedTime) {
request.autoDeletedTime = options.autoDeletedTime ?? NotificationUtil.defaultConfig.autoDeletedTime; //自动清除的时间。
}
request.isAlertOnce = options.isAlertOnce ?? NotificationUtil.defaultConfig.isAlertOnce; //设置是否仅有一次此通知提醒。
request.isStopwatch = options.isStopwatch ?? NotificationUtil.defaultConfig.isStopwatch; //是否显示已用时间。
request.isCountDown = options.isCountDown ?? NotificationUtil.defaultConfig.isCountDown; //是否显示倒计时时间。
request.isFloatingIcon = options.isFloatingIcon ?? NotificationUtil.defaultConfig.isFloatingIcon; //是否显示状态栏图标。
if (options.label || NotificationUtil.defaultConfig.label) {
request.label = options.label ?? NotificationUtil.defaultConfig.label; //通知标签。
}
if (options.actionButtons || NotificationUtil.defaultConfig.actionButtons) {
request.actionButtons = options.actionButtons ?? NotificationUtil.defaultConfig.actionButtons; //通知按钮,最多三个按钮。
}
if (options.smallIcon || NotificationUtil.defaultConfig.smallIcon) {
request.smallIcon = options.smallIcon ?? NotificationUtil.defaultConfig.smallIcon; //通知小图标。可选字段,图像像素的总字节数不超过100KB。实际显示效果依赖于设备能力和通知中心UI样式。
}
if (options.largeIcon || NotificationUtil.defaultConfig.largeIcon) {
request.largeIcon = options.largeIcon ?? NotificationUtil.defaultConfig.largeIcon; //通知大图标。可选字段,图像像素的总字节数不超过100KB。实际显示效果依赖于设备能力和通知中心UI样式。
}
if (options.groupName || NotificationUtil.defaultConfig.groupName) {
request.groupName = options.groupName ?? NotificationUtil.defaultConfig.groupName; //组通知名称。
}
if (options.template || NotificationUtil.defaultConfig.template) {
request.template = options.template ?? NotificationUtil.defaultConfig.template; //通知模板。
}
if (options.distributedOption || NotificationUtil.defaultConfig.distributedOption) {
request.distributedOption = options.distributedOption ?? NotificationUtil.defaultConfig.distributedOption; //分布式通知的选项。
}
if (options.appMessageId || NotificationUtil.defaultConfig.appMessageId) {
request.appMessageId = options.appMessageId ?? NotificationUtil.defaultConfig.appMessageId; //应用发送通知携带的唯一标识字段, 用于通知去重。如果同一应用通过本地和云端等不同途径发布携带相同appMessageId的通知,设备只展示一条消息,之后收到的重复通知会被静默去重,不展示、不提醒。去重标识仅在通知发布的24小时内有效,超过24小时或者设备重启失效。。
}
if (options.sound || NotificationUtil.defaultConfig.sound) {
request.sound = options.sound ?? NotificationUtil.defaultConfig.sound; //应用通知自定义铃声,需要通过Push云侧获取自定义发送铃声权限后,该字段才会生效。
}
if (options.badgeNumber || NotificationUtil.defaultConfig.badgeNumber) {
request.badgeNumber = options.badgeNumber ?? NotificationUtil.defaultConfig.badgeNumber; //应用程序图标上显示的通知数。
}
if (options.extraInfo || NotificationUtil.defaultConfig.extraInfo) {
request.extraInfo = options.extraInfo ?? NotificationUtil.defaultConfig.extraInfo; //扩展参数。
}
if (options.wantAgent || NotificationUtil.defaultConfig.wantAgent) {
request.wantAgent = options.wantAgent ?? NotificationUtil.defaultConfig.wantAgent; //WantAgent封装了应用的行为意图,点击通知时触发该行为。
}
if (options.removalWantAgent || NotificationUtil.defaultConfig.removalWantAgent) {
request.removalWantAgent = options.removalWantAgent ?? NotificationUtil.defaultConfig.removalWantAgent; //当移除通知时,通知将被重定向到的WantAgent实例。
}
return request;
}
/**
* 获取错误msg
* @param code
* @param defaultMsg
*/
public static getErrorMsg(code: number, defaultMsg: string) {
if (201 == code) {
return '权限校验失败!'
} else if (202 == code) {
return '系统API权限校验失败!'
} else if (401 == code) {
return '参数检查失败!'
} else if (801 == code) {
return '该设备不支持此API'
} else if (1600001 == code) {
return '应用通知,内部错误!'
} else if (1600002 == code) {
return '序列化或反序列化错误!'
} else if (1600003 == code) {
return '应用连接通知服务失败!'
} else if (1600004 == code) {
return '请开启应用通知开关!'
} else if (1600005 == code) {
return '通知渠道关闭!'
} else if (1600006 == code) {
return '通知删除失败!'
} else if (1600007 == code) {
return '通知不存在!'
} else if (1600008 == code) {
return '用户不存在!'
} else if (1600009 == code) {
return '通知发布频度超过限制!'
} else if (1600010 == code) {
return '分布式操作失败!'
} else if (1600011 == code) {
return '读模板配置文件错误!'
} else if (1600012 == code) {
return '内存空间不够!'
} else if (17700001 == code) {
return '包名不存在!'
} else {
return defaultMsg
}
}
}