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