uniapp开发纯血鸿蒙应用获取消息推送 pushToken

/**
 * interface.uts
 * uts插件接口定义文件,按规范定义接口文件可以在HBuilderX中更好的做到语法提示
 */
export interface Uni {
	/**
		* getHarmonyPushToken()
		* @description
		* 获取纯血鸿蒙系统消息推送pushToken
		* @param {	getHarmonyPushToken(options : GetHarmonyPushTokenOptions) : void;}  options
		* @return {void}
		* @example
		 ```typescript
			uni.getHarmonyPushToken({});
		 ```
		*/
	getHarmonyPushToken(options : GetHarmonyPushTokenOptions) : void;
}

export type GetHarmonyPushToken = (options : GetHarmonyPushTokenOptions) => void;
export type GetHarmonyPushTokenSuccess = {
	/**
	 * 错误信息
	 */
	errMsg : string,
	/**
	 * 推送token
	 */
	pushToken : string
};
export type GetHarmonyPushTokenSuccessCallback = (result : GetHarmonyPushTokenSuccess) => void;
export type GetHarmonyPushTokenFail = {
	/**
	 * 错误信息
	 */
	errMsg : string
};
export type GetHarmonyPushTokenFailCallback = (result : GetHarmonyPushTokenFail) => void;
export type GetHarmonyPushTokenComplete = {
	/**
	 * 错误信息
	 */
	errMsg : string
};
export type GetHarmonyPushTokenCompleteCallback = (result : GetHarmonyPushTokenComplete) => void;
export type GetHarmonyPushTokenOptions = {
	/**
	 * 接口调用成功的回调函数
	 * @defaultValue null
	 */
	success ?: GetHarmonyPushTokenSuccessCallback | null,
	/**
	 * 接口调用失败的回调函数
	 * @defaultValue null
	 */
	fail ?: GetHarmonyPushTokenFailCallback | null,
	/**
	 * 接口调用结束的回调函数(调用成功、失败都会执行)
	 * @defaultValue null
	 */
	complete ?: GetHarmonyPushTokenCompleteCallback | null
};
import {
	GetHarmonyPushToken,
	GetHarmonyPushTokenOptions,
	GetHarmonyPushTokenSuccess,
	GetHarmonyPushTokenFail,
	GetHarmonyPushTokenComplete
} from '../interface.uts'

export {
	GetHarmonyPushToken,
	GetHarmonyPushTokenOptions,
	GetHarmonyPushTokenSuccess,
	GetHarmonyPushTokenFail,
	GetHarmonyPushTokenComplete
}

import { pushService } from '@kit.PushKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { BusinessError } from '@kit.BasicServicesKit';

export async function getHarmonyPushToken(options : GetHarmonyPushTokenOptions) {
	let isSuccess = true;
	let pushToken : string = "";
	try {
		pushToken = await pushService.getToken();
		hilog.info(0, 'TAG', 'Succeeded in getting push token');
	} catch (err) {
		isSuccess = false;
		hilog.error(0, 'TAG', `getPushToken failed. code is ${err.code}, message is ${err.message}`);
		let result : GetHarmonyPushTokenFail = {
			errMsg: err.message ?? ""
		};
		let completeResult : GetHarmonyPushTokenComplete = {
			errMsg: err.message ?? ""
		}
		options?.fail?.(result);
		options?.complete?.(completeResult);
	}
	if (isSuccess) {
		let result : GetHarmonyPushTokenSuccess = {
			errMsg: "ok",
			pushToken: pushToken
		};
		let completeResult : GetHarmonyPushTokenComplete = {
			errMsg: "ok"
		}
		options?.success?.(result);
		options?.complete?.(completeResult);
	}

}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值