refactor(push): 重构小米推送集成实现
- 移除轮询注册ID的逻辑,改用系统回调机制获取推送令牌 - 将广播接收器替换为小米官方PushMessageReceiver实现 - 添加完整的推送消息处理方法(注册结果、透传消息、通知点击等) - 删除硬编码的应用ID和密钥配置 - 移除过时的小米推送相关类引用和方法调用 - 更新应用配置以匹配生产环境参数
这个提交包含在:
父节点
ea693d5c66
当前提交
e87b1b0af2
@ -8,7 +8,7 @@ android {
|
||||
compileSdk = libs.versions.compileSdk.get().toInt()
|
||||
|
||||
defaultConfig {
|
||||
applicationId = "com.xuqm.demo"
|
||||
applicationId = "cn.org.bjca.wcert.ywq"
|
||||
minSdk = libs.versions.minSdk.get().toInt()
|
||||
targetSdk = libs.versions.targetSdk.get().toInt()
|
||||
versionCode = 1
|
||||
@ -16,11 +16,24 @@ android {
|
||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||
}
|
||||
|
||||
signingConfigs {
|
||||
create("ywq") {
|
||||
storeFile = file("/Users/xuqinmin/Projects/TrustProjects/YiwangxinApp/android/keystores/wsecx.keystore")
|
||||
storePassword = "bjca123456"
|
||||
keyAlias = "bjcawsecx"
|
||||
keyPassword = "bjca123456"
|
||||
}
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
isMinifyEnabled = false
|
||||
signingConfig = signingConfigs.getByName("ywq")
|
||||
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
|
||||
}
|
||||
debug {
|
||||
signingConfig = signingConfigs.getByName("ywq")
|
||||
}
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
|
||||
@ -13,7 +13,7 @@ class XuqmSampleApp : Application() {
|
||||
AppDependencies.init(this)
|
||||
XuqmSDK.initialize(
|
||||
context = this,
|
||||
appKey = "ak_demo_chat",
|
||||
appKey = "ak_c6fce237cae94ef5ab71fda6",
|
||||
logLevel = if (BuildConfig.DEBUG) LogLevel.DEBUG else LogLevel.WARN,
|
||||
)
|
||||
runBlocking {
|
||||
|
||||
@ -12,7 +12,7 @@ import retrofit2.http.POST
|
||||
import retrofit2.http.PUT
|
||||
import retrofit2.http.Query
|
||||
|
||||
const val DEMO_APP_ID = "ak_demo_chat"
|
||||
const val DEMO_APP_ID = "ak_c6fce237cae94ef5ab71fda6"
|
||||
|
||||
data class DemoResponse<T>(
|
||||
val code: Int = 0,
|
||||
|
||||
@ -4,7 +4,6 @@ import android.content.Context
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import com.xuqm.sdk.push.PushSDK
|
||||
import com.xuqm.sdk.push.model.PushVendor
|
||||
import com.xuqm.sdk.push.model.PushVendorConfig
|
||||
|
||||
@ -41,7 +40,6 @@ class XiaomiPushService : PushVendorInterface {
|
||||
String::class.java,
|
||||
).invoke(null, context, appId, appKey)
|
||||
Log.i(TAG, "Xiaomi push registration requested")
|
||||
pollRegId(context, miPushClass)
|
||||
} else {
|
||||
Log.w(TAG, "Xiaomi appId/appKey not configured, skipping registration")
|
||||
}
|
||||
@ -61,24 +59,6 @@ class XiaomiPushService : PushVendorInterface {
|
||||
}
|
||||
}
|
||||
|
||||
private fun pollRegId(context: Context, miPushClass: Class<*>) {
|
||||
Thread {
|
||||
repeat(8) {
|
||||
val regId = runCatching {
|
||||
miPushClass.getMethod("getRegId", Context::class.java)
|
||||
.invoke(null, context) as? String
|
||||
}.getOrNull()
|
||||
if (!regId.isNullOrBlank()) {
|
||||
PushSDK.updateNativePushToken(context, vendor, regId)
|
||||
Log.i(TAG, "Xiaomi push token acquired")
|
||||
return@Thread
|
||||
}
|
||||
Thread.sleep(1000)
|
||||
}
|
||||
Log.w(TAG, "Xiaomi push token not ready after registration")
|
||||
}.start()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val TAG = "XiaomiPushService"
|
||||
}
|
||||
|
||||
@ -1,57 +1,39 @@
|
||||
package com.xuqm.sdk.push.xiaomi
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.util.Log
|
||||
import com.xiaomi.mipush.sdk.MiPushCommandMessage
|
||||
import com.xiaomi.mipush.sdk.MiPushMessage
|
||||
import com.xiaomi.mipush.sdk.PushMessageReceiver
|
||||
import com.xuqm.sdk.push.PushSDK
|
||||
import com.xuqm.sdk.push.model.PushVendor
|
||||
|
||||
class XuqmXiaomiPushReceiver : BroadcastReceiver() {
|
||||
class XuqmXiaomiPushReceiver : PushMessageReceiver() {
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent?) {
|
||||
if (intent == null) return
|
||||
val appContext = context.applicationContext
|
||||
val regId = extractRegId(intent)
|
||||
if (!regId.isNullOrBlank()) {
|
||||
override fun onReceiveRegisterResult(context: Context, message: MiPushCommandMessage) {
|
||||
if (message.resultCode != 0L) {
|
||||
Log.w(TAG, "Xiaomi push registration failed: code=${message.resultCode} reason=${message.reason}")
|
||||
return
|
||||
}
|
||||
val regId = message.commandArguments?.firstOrNull()?.takeIf { it.isNotBlank() } ?: return
|
||||
Log.i(TAG, "Xiaomi push registered: regId=$regId")
|
||||
runCatching {
|
||||
PushSDK.updateNativePushToken(appContext, PushVendor.XIAOMI, regId)
|
||||
PushSDK.updateNativePushToken(context.applicationContext, PushVendor.XIAOMI, regId)
|
||||
}.onFailure { error ->
|
||||
Log.w(TAG, "Unable to persist Xiaomi push token: ${error.message}")
|
||||
}
|
||||
return
|
||||
}
|
||||
runCatching {
|
||||
PushSDK.refreshNativePushToken(appContext, PushVendor.XIAOMI)
|
||||
}.onFailure { error ->
|
||||
Log.d(TAG, "Xiaomi push callback ignored before SDK init: ${error.message}")
|
||||
}
|
||||
}
|
||||
|
||||
private fun extractRegId(intent: Intent): String? {
|
||||
val extras = intent.extras ?: return null
|
||||
for (key in extras.keySet()) {
|
||||
val value = extras.get(key) ?: continue
|
||||
if (value is String && key.contains("reg", ignoreCase = true)) {
|
||||
return value.takeIf { it.isNotBlank() }
|
||||
override fun onReceivePassThroughMessage(context: Context, message: MiPushMessage) {
|
||||
Log.d(TAG, "Xiaomi pass-through message received: ${message.content}")
|
||||
}
|
||||
if (value.javaClass.name == "com.xiaomi.mipush.sdk.MiPushCommandMessage") {
|
||||
val resultCode = runCatching {
|
||||
value.javaClass.getMethod("getResultCode").invoke(value) as? Long
|
||||
}.getOrNull()
|
||||
val command = runCatching {
|
||||
value.javaClass.getMethod("getCommand").invoke(value) as? String
|
||||
}.getOrNull()
|
||||
val arguments = runCatching {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
value.javaClass.getMethod("getCommandArguments").invoke(value) as? List<String>
|
||||
}.getOrNull()
|
||||
if ((resultCode == null || resultCode == 0L) && command.equals("register", ignoreCase = true)) {
|
||||
return arguments?.firstOrNull { it.isNotBlank() }
|
||||
|
||||
override fun onNotificationMessageClicked(context: Context, message: MiPushMessage) {
|
||||
Log.d(TAG, "Xiaomi notification clicked: ${message.title}")
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
|
||||
override fun onCommandResult(context: Context, message: MiPushCommandMessage) {
|
||||
Log.d(TAG, "Xiaomi command result: ${message.command} code=${message.resultCode}")
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
正在加载...
在新工单中引用
屏蔽一个用户