uniapp开发纯血鸿蒙应用请求通知权限

通知权限功能官方已支持,无需手动编写代码了

/**
 * 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);
	}

}

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值