2026-04-25 16:41:19 +08:00
|
|
|
import { apiRequest, getConfig, getDeviceInfo } from '@xuqm/rn-common'
|
|
|
|
|
import type { PushVendor } from '@xuqm/rn-common'
|
2026-04-24 16:16:31 +08:00
|
|
|
|
2026-04-25 16:41:19 +08:00
|
|
|
export type { PushVendor }
|
2026-04-24 16:16:31 +08:00
|
|
|
|
|
|
|
|
export const PushSDK = {
|
|
|
|
|
/**
|
|
|
|
|
* Register a push device token for the given user.
|
2026-04-25 16:41:19 +08:00
|
|
|
* If vendor is omitted, it is auto-detected from the device brand.
|
2026-04-24 16:16:31 +08:00
|
|
|
*
|
|
|
|
|
* @param userId - The logged-in user's ID
|
2026-04-25 16:41:19 +08:00
|
|
|
* @param token - The device push token from the vendor SDK
|
|
|
|
|
* @param vendor - Optional; auto-detected when not provided
|
2026-04-24 16:16:31 +08:00
|
|
|
*/
|
2026-04-25 16:41:19 +08:00
|
|
|
async registerToken(userId: string, token: string, vendor?: PushVendor): Promise<void> {
|
2026-04-24 16:16:31 +08:00
|
|
|
const config = getConfig()
|
2026-04-25 16:41:19 +08:00
|
|
|
const device = await getDeviceInfo()
|
2026-04-24 16:16:31 +08:00
|
|
|
await apiRequest('/api/push/register', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
params: {
|
2026-04-25 16:41:19 +08:00
|
|
|
appId: config.appId,
|
2026-04-24 16:16:31 +08:00
|
|
|
userId,
|
2026-04-25 16:41:19 +08:00
|
|
|
vendor: vendor ?? device.pushVendor,
|
2026-04-24 16:16:31 +08:00
|
|
|
token,
|
2026-04-25 16:41:19 +08:00
|
|
|
platform: device.platform,
|
|
|
|
|
deviceId: device.deviceId,
|
|
|
|
|
brand: device.brand,
|
|
|
|
|
model: device.model,
|
|
|
|
|
osVersion: device.osVersion,
|
2026-04-24 16:16:31 +08:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
async unregisterToken(userId: string): Promise<void> {
|
|
|
|
|
const config = getConfig()
|
2026-04-25 16:41:19 +08:00
|
|
|
const { deviceId } = await getDeviceInfo()
|
2026-04-24 16:16:31 +08:00
|
|
|
await apiRequest('/api/push/unregister', {
|
|
|
|
|
method: 'DELETE',
|
2026-04-25 16:41:19 +08:00
|
|
|
params: { appId: config.appId, userId, deviceId },
|
2026-04-24 16:16:31 +08:00
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
}
|