通知权限功能官方已支持,无需手动编写代码了
/**
* interface.uts
* uts插件接口定义文件,按规范定义接口文件可以在HBuilderX中更好的做到语法提示
*/
export interface Uni {
/**
* requestEnableNotification
* @description
* 请求鸿蒙系统通知权限
* @param {RequestEnableNotificationOptions} options
* @return {void}
* @example
```typescript
uni.requestEnableNotification({});
```
*/
requestEnableNotification(options : RequestEnableNotificationOptions) : void;
}
export type RequestEnableNotification = (options : RequestEnableNotificationOptions) => void;
export type RequestEnableNotificationSuccess = {
/**
* 错误信息
*/
errMsg : string
};
export type RequestEnableNotificationSuccessCallback = (result : RequestEnableNotificationSuccess) => void;
export type RequestEnableNotificationFail = {
/**
* 错误信息
*/
errMsg : string
};
export type RequestEnableNotificationFailCallback = (result : RequestEnableNotificationFail) => void;
export type RequestEnableNotificationComplete = {
/**
* 错误信息
*/
errMsg : string
};
export type RequestEnableNotificationCompleteCallback = (result : RequestEnableNotificationComplete) => void;
export type RequestEnableNotificationOptions = {
/**
* 接口调用成功的回调函数
* @defaultValue null
*/
success ?: RequestEnableNotificationSuccessCallback | null,
/**
* 接口调用失败的回调函数
* @defaultValue null
*/
fail ?: RequestEnableNotificationFailCallback | null,
/**
* 接口调用结束的回调函数(调用成功、失败都会执行)
* @defaultValue null
*/
complete ?: RequestEnableNotificationCompleteCallback | null
};
import {
RequestEnableNotification,
RequestEnableNotificationOptions,
RequestEnableNotificationSuccess,
RequestEnableNotificationFail,
RequestEnableNotificationComplete
} from '../interface.uts'
export {
RequestEnableNotification,
RequestEnableNotificationOptions,
RequestEnableNotificationSuccess,
RequestEnableNotificationFail,
RequestEnableNotificationComplete
}
import { notificationManager } from '@kit.NotificationKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { common } from '@kit.AbilityKit';
export function requestEnableNotification(options : RequestEnableNotificationOptions) {
let isSuccess = true;
try {
let context = getContext() as common.UIAbilityContext;
notificationManager.isNotificationEnabled().then((data : boolean) => {
if (!data) {
notificationManager.requestEnableNotification(context).then(() => {
//授权成功
let result : RequestEnableNotificationSuccess = {
errMsg: "ok",
};
let completeResult : RequestEnableNotificationComplete = {
errMsg: "ok"
}
options?.success?.(result);
options?.complete?.(completeResult);
}).catch((err : BusinessError) => {
let result : RequestEnableNotificationFail = {
errMsg: err.message ?? ""
};
let completeResult : RequestEnableNotificationComplete = {
errMsg: err.message ?? ""
}
options?.fail?.(result);
options?.complete?.(completeResult);
// if (1600004 == err.code) {//用户拒绝
// } else {//授权失败
// }
});
}
}).catch((err : BusinessError) => {
});
} catch (err) {
isSuccess = false;
let result : RequestEnableNotificationFail = {
errMsg: err.message ?? ""
};
let completeResult : RequestEnableNotificationComplete = {
errMsg: err.message ?? ""
}
options?.fail?.(result);
options?.complete?.(completeResult);
}
}