feat(servicing): 添加客户语音通知功能

- 新增 CustomerSpeechManager 对象,用于处理文本转语音功能
- 添加 AppForegroundListener 接口和 BaseActivityLifecycleCallbacks 类,用于监听应用前后台切换- 更新 BaseActivity,使其支持推送消息
- 新增 ServicePeopleConfirmActivity 活动
- 优化订单处理逻辑,过滤掉已接受的订单
- 更新版本号至 1.0.1.9.9.12
This commit is contained in:
songzhiling
2025-04-27 17:49:05 +08:00
parent b0c2f7352d
commit 863329d107
32 changed files with 1505 additions and 223 deletions

View File

@ -0,0 +1,207 @@
package com.za.base
import android.app.Activity
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.DialogInterface
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 com.google.gson.Gson
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
open class PushMessageActivity : AppCompatActivity() {
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)
}
}
}
override fun onPause() {
super.onPause()
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 {
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()
currentDialog = null
} catch (e : Exception) {
LogUtil.print("PushActivityLifecycleCallbacks", "关闭对话框失败: ${e.message}")
}
}
private fun handleGiveUpOrder(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(jpushBean : JpushBean) {
playNotificationSound(this)
currentDialog = AlertDialog.Builder(this).setTitle("订单重新派发")
.setMessage(buildReDispatchMessage(jpushBean)).setCancelable(false)
.setPositiveButton("确定") { dialog, _ ->
dialog.dismiss()
}.show()
}
private fun handleImportantTip(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"
}
}