refactor(servicing): 重构推送消息处理机制

- 移除 PushMessageLiveData,改为使用 PushListener 接口
- 在 ServiceManager 中实现消息分发逻辑- 更新 PushMessageActivity 以接收广播消息
- 优化 JPushReceiver 和 MyMqttClient 的消息处理
- 调整 GlobalData 中的 isLoginRecognition 默认值
- 重构 SpeechManager 中的语音播放逻辑
This commit is contained in:
songzhiling
2025-04-28 18:06:16 +08:00
parent bc4590755a
commit 2f57b3e238
9 changed files with 271 additions and 130 deletions

View File

@ -4,15 +4,21 @@ import android.app.Activity
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.content.IntentFilter
import android.media.RingtoneManager
import android.os.Build
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat
import com.blankj.utilcode.util.ActivityUtils
import com.google.gson.Gson
import com.za.base.BaseActivityLifecycleCallbacks.Companion.getCurrentActivity
import com.za.bean.JpushBean
import com.za.common.GlobalData
import com.za.common.log.LogUtil
@ -22,16 +28,103 @@ import com.za.ui.servicing.order_give_up.OrderGiveUpActivity
import com.za.ui.view.CommonDialogFragment
open class PushMessageActivity : AppCompatActivity() {
private var pushMessageReceiver : BroadcastReceiver? = null
private var currentDialog : AlertDialog? = null
override fun onCreate(savedInstanceState : Bundle?) {
super.onCreate(savedInstanceState)
if (! GlobalData.isMaster) {
PushMessageLiveData.pushMessage.observe(this) { message -> // 处理推送消息
LogUtil.print("PushMessageActivity", "Received push message: $message")
handlePushMessage(msg = message)
if (! GlobalData.isMaster && pushMessageReceiver == null) {
registerPushMessageReceiver(this)
}
}
private fun registerPushMessageReceiver(context : Context) {
pushMessageReceiver = object : BroadcastReceiver() {
override fun onReceive(context : Context?, intent : Intent?) {
if (intent?.action == "com.za.rescue.dealer.PUSH_MESSAGE") {
val type = intent.getStringExtra("type") ?: return
val message = intent.getStringExtra("message") ?: return
LogUtil.print("PushActivityLifecycleCallbacks", "收到来自远程进程的消息: $type")
when (type) {
"broadcast" -> handleBroadcast("broadcast:$message")
"giveUp" -> {
try {
val jpushBean = Gson().fromJson(message, JpushBean::class.java)
val activity =
getCurrentActivity() ?: ActivityUtils.getTopActivity()
if (activity is AppCompatActivity) {
handleGiveUpOrder(activity, jpushBean)
}
} catch (e : Exception) {
LogUtil.print("PushActivityLifecycleCallbacks",
"处理订单放弃消息失败: ${e.message}")
}
}
"importantTip" -> {
try {
val jpushBean = Gson().fromJson(message, JpushBean::class.java)
val activity =
getCurrentActivity() ?: ActivityUtils.getTopActivity()
if (activity is AppCompatActivity) {
handleImportantTip(activity, jpushBean)
}
} catch (e : Exception) {
LogUtil.print("PushActivityLifecycleCallbacks",
"处理重要提示消息失败: ${e.message}")
}
}
"reDispatch" -> {
try {
val jpushBean = Gson().fromJson(message, JpushBean::class.java)
val activity =
getCurrentActivity() ?: ActivityUtils.getTopActivity()
if (activity is AppCompatActivity) {
handleReDispatchOrder(activity, jpushBean)
}
} catch (e : Exception) {
LogUtil.print("PushActivityLifecycleCallbacks",
"处理订单重新派发消息失败: ${e.message}")
}
}
"revoke" -> {
try {
handleRevokeOrder()
} catch (e : Exception) {
LogUtil.print("PushActivityLifecycleCallbacks",
"处理订单撤回消息失败: ${e.message}")
}
}
}
}
}
}
ContextCompat.registerReceiver(context,
pushMessageReceiver,
IntentFilter("com.za.rescue.dealer.PUSH_MESSAGE"),
ContextCompat.RECEIVER_NOT_EXPORTED)
LogUtil.print("PushActivityLifecycleCallbacks", "注册推送消息接收器")
}
override fun onDestroy() {
super.onDestroy()
if (isLastActivity()) {
try {
this.unregisterReceiver(pushMessageReceiver)
pushMessageReceiver = null
LogUtil.print("PushActivityLifecycleCallbacks", "注销推送消息接收器")
} catch (e : Exception) {
LogUtil.print("PushActivityLifecycleCallbacks",
"注销推送消息接收器失败: ${e.message}")
}
}
}
private fun isLastActivity() : Boolean { // 检查是否是最后一个活动的Activity
return ActivityUtils.getActivityList().size <= 1
}
override fun onPause() {
@ -39,36 +132,6 @@ open class PushMessageActivity : AppCompatActivity() {
dismissCurrentDialog()
}
private fun handlePushMessage(msg : String) {
if (msg.startsWith("broadcast:")) {
handleBroadcast(msg)
return
}
try {
val jpushOrderInfoBean = Gson().fromJson(msg, JpushBean::class.java)
when (jpushOrderInfoBean.pushType) {
1 -> handleTypeOneMessage(jpushOrderInfoBean)
3 -> handleImportantTip(jpushOrderInfoBean)
else -> LogUtil.print("JpushMessage",
"Unknown push type: ${jpushOrderInfoBean.pushType}")
}
} catch (e : Exception) {
if (msg.startsWith("broadcast:")) {
handleBroadcast(msg)
}
LogUtil.print("JpushMessage", "Error handling message: ${e.message}")
}
}
private fun handleTypeOneMessage(jpushOrderBean : JpushBean) {
when (jpushOrderBean.typeDesc) {
"giveUp" -> handleGiveUpOrder(jpushOrderBean)
"revoke" -> handleRevokeOrder()
"reDispatch" -> handleReDispatchOrder(jpushOrderBean)
else -> LogUtil.print("JpushMessage", "Unknown typeDesc: ${jpushOrderBean.typeDesc}")
}
}
// Handle broadcast messages
private fun handleBroadcast(msg : String) {
try {
@ -90,7 +153,8 @@ open class PushMessageActivity : AppCompatActivity() {
}
}
private fun handleGiveUpOrder(jpushBean : JpushBean) { // 播放提示音
private fun handleGiveUpOrder(activity : AppCompatActivity,
jpushBean : JpushBean) { // 播放提示音
playNotificationSound(this)
if (GlobalData.currentOrder != null && GlobalData.currentOrder?.taskId == jpushBean.taskId) {
@ -115,17 +179,16 @@ open class PushMessageActivity : AppCompatActivity() {
this.finish()
}
private fun handleReDispatchOrder(jpushBean : JpushBean) {
private fun handleReDispatchOrder(activity : AppCompatActivity, jpushBean : JpushBean) {
playNotificationSound(this)
currentDialog = AlertDialog.Builder(this).setTitle("订单重新派发")
.setMessage(buildReDispatchMessage(jpushBean)).setCancelable(false)
.setPositiveButton("确定") { dialog, _ ->
dialog.dismiss()
}.show()
}
private fun handleImportantTip(jpushBean : JpushBean) {
private fun handleImportantTip(activity : AppCompatActivity, jpushBean : JpushBean) {
playNotificationSound(this)
SpeechManager.speech("重要提醒:${jpushBean.tipContent ?: ""}")
currentDialog =