refactor(servicing): 重构推送消息处理机制
- 新增 ZdPushServiceReceive 类用于集中处理推送消息 - 优化了消息处理逻辑,提高了代码的可维护性和可扩展性 -改进了消息在主进程中的广播发送方式 - 优化了语音播放逻辑,提高了播放的稳定性和流畅性 - 调整了通知渠道的创建和通知的发送方式
This commit is contained in:
@ -1,130 +1,58 @@
|
||||
package com.za.base
|
||||
|
||||
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
|
||||
import com.za.common.speech.SpeechManager
|
||||
import com.za.servicing.R
|
||||
import com.za.ui.servicing.order_give_up.OrderGiveUpActivity
|
||||
import com.za.ui.view.CommonDialogFragment
|
||||
import com.za.service.PushListener
|
||||
import com.za.service.ServiceManager
|
||||
import com.za.service.ZdPushServiceReceive
|
||||
|
||||
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 && pushMessageReceiver == null) {
|
||||
registerPushMessageReceiver(this)
|
||||
}
|
||||
setupPushMessageReceiver()
|
||||
}
|
||||
|
||||
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}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
private fun setupPushMessageReceiver() { // 注册推送消息接收器
|
||||
ServiceManager.registerPushListener(object : PushListener {
|
||||
override fun broadcast(msg : String) {
|
||||
sendMessageToMainProcess("broadcast", msg)
|
||||
}
|
||||
}
|
||||
|
||||
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}")
|
||||
override fun giveUpOrder(jpushBean : JpushBean) {
|
||||
sendMessageToMainProcess("giveUp", Gson().toJson(jpushBean))
|
||||
}
|
||||
}
|
||||
|
||||
override fun importantTip(jpushBean : JpushBean) {
|
||||
sendMessageToMainProcess("importantTip", Gson().toJson(jpushBean))
|
||||
}
|
||||
|
||||
override fun newOrderMsg(jpushBean : JpushBean) {
|
||||
sendMessageToMainProcess("newOrder", Gson().toJson(jpushBean))
|
||||
}
|
||||
|
||||
override fun reDispatchOrder(jpushBean : JpushBean) {
|
||||
sendMessageToMainProcess("reDispatch", Gson().toJson(jpushBean))
|
||||
}
|
||||
|
||||
override fun revokeOrder(jpushBean : JpushBean) {
|
||||
sendMessageToMainProcess("revoke", Gson().toJson(jpushBean))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private fun isLastActivity() : Boolean { // 检查是否是最后一个活动的Activity
|
||||
return ActivityUtils.getActivityList().size <= 1
|
||||
private fun sendMessageToMainProcess(type : String, message : String) { // 使用广播将消息发送到主进程
|
||||
val intent = Intent(ZdPushServiceReceive.RECEIVE_ACTION).setPackage(packageName)
|
||||
intent.putExtra("type", type)
|
||||
intent.putExtra("message", message)
|
||||
sendBroadcast(intent)
|
||||
LogUtil.print("KeepAliveService", "发送消息到主进程: $type")
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
@ -132,18 +60,6 @@ open class PushMessageActivity : AppCompatActivity() {
|
||||
dismissCurrentDialog()
|
||||
}
|
||||
|
||||
// Handle broadcast messages
|
||||
private fun handleBroadcast(msg : String) {
|
||||
try {
|
||||
val content = msg.substring(10)
|
||||
sendNotification(GlobalData.application, content)
|
||||
LogUtil.print("JpushMessage", "Broadcast content: $content")
|
||||
} catch (e : Exception) {
|
||||
LogUtil.print("JpushMessage", "Broadcast failed: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun dismissCurrentDialog() {
|
||||
try {
|
||||
currentDialog?.dismiss()
|
||||
@ -153,116 +69,6 @@ open class PushMessageActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleGiveUpOrder(activity : AppCompatActivity,
|
||||
jpushBean : JpushBean) { // 播放提示音
|
||||
playNotificationSound(this)
|
||||
|
||||
if (GlobalData.currentOrder != null && GlobalData.currentOrder?.taskId == jpushBean.taskId) {
|
||||
SpeechManager.playCurrentOrderCanceled()
|
||||
CommonDialogFragment(title = "订单放弃",
|
||||
message = buildGiveUpMessage(jpushBean),
|
||||
confirmText = "去拍照",
|
||||
cancelText = "我已了解",
|
||||
confirm = {
|
||||
OrderGiveUpActivity.goOrderGiveUpActivity(this,
|
||||
giveUpType = GIVE_UP_TYPE_NORMAL,
|
||||
taskId = jpushBean.taskId)
|
||||
}).show(this.supportFragmentManager, DIALOG_TAG_GIVE_UP)
|
||||
} else {
|
||||
SpeechManager.playOrderCanceled()
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleRevokeOrder() {
|
||||
playNotificationSound(this)
|
||||
SpeechManager.speech("订单被撤回") // 获取当前Activity进行处理
|
||||
this.finish()
|
||||
}
|
||||
|
||||
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(activity : AppCompatActivity, jpushBean : JpushBean) {
|
||||
playNotificationSound(this)
|
||||
SpeechManager.speech("重要提醒:${jpushBean.tipContent ?: ""}")
|
||||
currentDialog =
|
||||
AlertDialog.Builder(this).setTitle("重要提醒").setMessage(jpushBean.tipContent)
|
||||
.setNegativeButton("我已了解") { dialog : DialogInterface, _ : Int -> dialog.dismiss() }
|
||||
.show()
|
||||
}
|
||||
|
||||
private fun buildGiveUpMessage(jpushBean : JpushBean) : String {
|
||||
return buildString {
|
||||
append("该工单已放弃")
|
||||
jpushBean.taskCode?.let { append("\n\n订单号:$it") }
|
||||
jpushBean.address?.let { append("\n地址:$it") }
|
||||
append("\n\n是否需要拍放空照片?")
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildReDispatchMessage(jpushBean : JpushBean) : String {
|
||||
return buildString {
|
||||
append("该订单已重新派发")
|
||||
jpushBean.taskCode?.let { append("\n\n订单号:$it") }
|
||||
jpushBean.address?.let { append("\n地址:$it") }
|
||||
jpushBean.addressRemark?.let {
|
||||
if (it.isNotBlank()) {
|
||||
append("\n\n备注:$it")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun playNotificationSound(activity : Activity) {
|
||||
try {
|
||||
val notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
|
||||
RingtoneManager.getRingtone(activity, notification)?.play()
|
||||
} catch (e : Exception) {
|
||||
LogUtil.print("PushActivityLifecycleCallbacks", "播放提示音失败: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
private val CHANNEL_ID = "ImportantMessagesChannel"
|
||||
private val NOTIFICATION_ID = 1003
|
||||
|
||||
// Initialize notification channel
|
||||
private fun createNotificationChannel(context : Context) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
val channel = NotificationChannel(CHANNEL_ID,
|
||||
"订单通知",
|
||||
NotificationManager.IMPORTANCE_HIGH).apply {
|
||||
description = "用于接收重要消息通知"
|
||||
setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION),
|
||||
Notification.AUDIO_ATTRIBUTES_DEFAULT)
|
||||
enableVibration(true)
|
||||
}
|
||||
val notificationManager =
|
||||
context.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
|
||||
notificationManager.createNotificationChannel(channel)
|
||||
}
|
||||
}
|
||||
|
||||
// Send notification
|
||||
private fun sendNotification(context : Context, message : String) {
|
||||
createNotificationChannel(context)
|
||||
val notification =
|
||||
NotificationCompat.Builder(context, CHANNEL_ID).setContentTitle("重要通知")
|
||||
.setContentText(message).setSmallIcon(R.mipmap.ic_launcher) // 替换为你的应用图标
|
||||
.setPriority(NotificationCompat.PRIORITY_HIGH).setAutoCancel(true) // 点击后自动取消通知
|
||||
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
|
||||
.setVibrate(longArrayOf(0, 100, 200, 300)).build()
|
||||
|
||||
val notificationManager =
|
||||
context.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
|
||||
notificationManager.notify(NOTIFICATION_ID, notification)
|
||||
}
|
||||
|
||||
companion object {
|
||||
internal const val GIVE_UP_TYPE_NORMAL = 1
|
||||
internal const val DIALOG_TAG_GIVE_UP = "giveUp"
|
||||
|
Reference in New Issue
Block a user