2026-04-21 22:07:29 +08:00
|
|
|
package com.xuqm.sdk.push
|
|
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import com.xuqm.sdk.XuqmSDK
|
2026-04-27 19:30:06 +08:00
|
|
|
import com.xuqm.sdk.core.ServiceEndpointRegistry
|
2026-04-21 22:07:29 +08:00
|
|
|
import com.xuqm.sdk.network.ApiClient
|
|
|
|
|
import com.xuqm.sdk.push.api.PushApi
|
|
|
|
|
import com.xuqm.sdk.utils.DeviceUtils
|
|
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
|
import kotlinx.coroutines.launch
|
2026-04-28 09:45:20 +08:00
|
|
|
import java.util.concurrent.atomic.AtomicReference
|
2026-04-21 22:07:29 +08:00
|
|
|
|
|
|
|
|
object PushSDK {
|
|
|
|
|
|
2026-04-27 19:30:06 +08:00
|
|
|
private val api: PushApi get() = ApiClient.create(PushApi::class.java, ServiceEndpointRegistry.pushBaseUrl)
|
2026-04-21 22:07:29 +08:00
|
|
|
private val scope = CoroutineScope(Dispatchers.IO)
|
2026-04-28 09:45:20 +08:00
|
|
|
private val registeredUserId = AtomicReference<String?>(null)
|
2026-04-21 22:07:29 +08:00
|
|
|
|
2026-04-27 17:18:55 +08:00
|
|
|
fun registerDevice(context: Context, userId: String) {
|
2026-04-21 22:07:29 +08:00
|
|
|
XuqmSDK.requireInit()
|
|
|
|
|
val vendor = DeviceUtils.getVendor()
|
2026-04-27 17:18:55 +08:00
|
|
|
val deviceId = DeviceUtils.getDeviceId(context)
|
2026-04-21 22:07:29 +08:00
|
|
|
scope.launch {
|
|
|
|
|
runCatching {
|
2026-04-27 17:18:55 +08:00
|
|
|
api.registerDevice(
|
2026-04-27 19:00:54 +08:00
|
|
|
appId = XuqmSDK.appId,
|
|
|
|
|
userId = userId,
|
|
|
|
|
vendor = vendor,
|
|
|
|
|
token = deviceId,
|
2026-04-27 17:18:55 +08:00
|
|
|
)
|
2026-04-28 09:45:20 +08:00
|
|
|
registeredUserId.set(userId)
|
2026-04-21 22:07:29 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-27 17:18:55 +08:00
|
|
|
fun unregisterDevice(userId: String) {
|
2026-04-21 22:07:29 +08:00
|
|
|
XuqmSDK.requireInit()
|
|
|
|
|
scope.launch {
|
2026-04-28 09:45:20 +08:00
|
|
|
runCatching {
|
|
|
|
|
api.unregisterDevice(XuqmSDK.appId, userId)
|
|
|
|
|
registeredUserId.compareAndSet(userId, null)
|
|
|
|
|
}
|
2026-04-21 22:07:29 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-04-28 09:45:20 +08:00
|
|
|
|
|
|
|
|
fun onSdkLogin(session: com.xuqm.sdk.XuqmLoginSession) {
|
|
|
|
|
val context = runCatching { XuqmSDK.appContext }.getOrNull() ?: return
|
|
|
|
|
if (registeredUserId.get() == session.userId) return
|
|
|
|
|
registerDevice(context, session.userId)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun onSdkLogout() {
|
|
|
|
|
val userId = registeredUserId.getAndSet(null) ?: return
|
|
|
|
|
unregisterDevice(userId)
|
|
|
|
|
}
|
2026-04-21 22:07:29 +08:00
|
|
|
}
|