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