import { TIMPushManager, TIMPushListener, TIMPushMessage, TIMPushResult } from '@tencentcloud/timpush'; import { DisablePostNotificationInForeground, RegisterPush, SetRegistrationID, GetRegistrationID AddPushListener, GetNotificationExtInfo, CreateNotificationChannel, UnRegisterPush, RemovePushListener, } from '../interface.uts' const Push = TIMPushManager.getInstance(); const LOG_PREFIX = 'Push |'; export enum EVENT { MESSAGE_RECEIVED = 'message_received', MESSAGE_REVOKED = 'message_revoked', NOTIFICATION_CLICKED = 'notification_clicked' } function setRunningPlatform(): void { console.log(LOG_PREFIX, 'setRunningPlatform'); const param = { "runningPlatform": 2 }; Push.callExperimentalAPI('setPushConfig', JSON.stringify(param)).then((res: TIMPushResult) => { console.log(LOG_PREFIX, 'setRunningPlatform ok.', res); }).catch((error: TIMPushResult) => { console.log(LOG_PREFIX, `setRunningPlatform fail. code: ${error.code}, desc: ${error?.message || ''}`); }) ); } let disableNotification = false; export const disablePostNotificationInForeground: DisablePostNotificationInForeground = function (disable: boolean): void { console.log(LOG_PREFIX, 'disablePostNotificationInForeground', disable); disableNotification = disable; Push.disablePostNotificationInForeground(disableNotification); } export const registerPush: RegisterPush = function (SDKAppID: number, appKey: string, onSuccess: (data: string) => void, onError?: (errCode: number, errMsg: string) => void): void { setRunningPlatform(); const context: Context = getContext(); Push.registerPush(context, SDKAppID, appKey).then((res: TIMPushResult) => { onSuccess?.(JSON.stringify(res?.data) || ''); disablePostNotificationInForeground(disableNotification); }).catch((error: TIMPushResult) => { onError?.(error.code, error?.message || ''); }) } export const setRegistrationID:SetRegistrationID = function (registrationID: string, onSuccess: () => void): void { Push.setRegistrationID(registrationID).then((res: TIMPushResult) => { console.info('setRegistrationID ok', res); onSuccess?.(); }); } export const getRegistrationID: GetRegistrationID = function (onSuccess: (registrationID: string) => void): void { Push.getRegistrationID().then((res: TIMPushResult) => { onSuccess?.(JSON.stringify(res)); }); } export const unRegisterPush: UnRegisterPush = function (onSuccess: () => void, onError?: (errCode: number, errMsg: string) => void): void { Push.unRegisterPush().then(() => { onSuccess?.(); }).catch((error: TIMPushResult) => { onError?.(error.code, error?.message || ''); }) } export const createNotificationChannel: CreateNotificationChannel = function (options: any, onSuccess: (extInfo: string) => void): void { // 空实现 } export const getNotificationExtInfo: GetNotificationExtInfo = function (onSuccess: (extInfo: string) => void): void { Push.callExperimentalAPI('getNotificationExtInfo', {}).then((res: TIMPushResult) => { onSuccess?.(JSON.stringify(res.data)); }) } const listenerMap = new Map void>>(); const pushListener: TIMPushListener = { onNotificationClicked: (ext: string) => { listenerMap.get(EVENT.NOTIFICATION_CLICKED)?.forEach(item => { item({ data: ext }); }); }, onRevokePushMessage: (messageID: string) => { listenerMap.get(EVENT.MESSAGE_REVOKED)?.forEach(item => { item({ data: messageID }); }); }, onMessageReceived: (message: TIMPushMessage) => { listenerMap.get(EVENT.MESSAGE_RECEIVED)?.forEach(item => { item({ data: message }); }); }, }; export const addPushListener: AddPushListener = function (eventName: string, listener: (res: any) => void): void { if(listenerMap.size === 0) { Push.addPushListener(pushListener); } const listeners:Array<(res: any) => void> = [listener]; listenerMap.get(eventName)?.forEach(item => { listeners.push(item); }) listenerMap.set(eventName, listeners); } export const removePushListener:RemovePushListener = function (eventName: string, listener?: (res: any) => void): void { listenerMap.delete(eventName); if(listenerMap.size === 0) { Push.removePushListener(pushListener); } }