Files
zd_servicing/servicing/src/main/java/com/za/service/ServiceManager.kt
songzhiling 863329d107 feat(servicing): 添加客户语音通知功能
- 新增 CustomerSpeechManager 对象,用于处理文本转语音功能
- 添加 AppForegroundListener 接口和 BaseActivityLifecycleCallbacks 类,用于监听应用前后台切换- 更新 BaseActivity,使其支持推送消息
- 新增 ServicePeopleConfirmActivity 活动
- 优化订单处理逻辑,过滤掉已接受的订单
- 更新版本号至 1.0.1.9.9.12
2025-04-27 17:49:05 +08:00

151 lines
5.1 KiB
Kotlin

package com.za.service
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.media.RingtoneManager
import android.os.Build
import androidx.core.app.NotificationCompat
import cn.jiguang.api.utils.JCollectionAuth
import cn.jpush.android.api.JPushInterface
import com.google.gson.Gson
import com.za.base.PushMessageLiveData
import com.za.bean.JpushBean
import com.za.common.GlobalData
import com.za.common.log.LogUtil
import com.za.common.util.DeviceUtil
import com.za.service.mqtt.MyMqttClient
import com.za.servicing.R
data class LastJPushBean(val msg : Int, val time : Long = System.nanoTime())
object ServiceManager {
private var lastJPushBean : LastJPushBean? = null
private const val DUPLICATE_MSG_THRESHOLD = 3000L // 3秒
// Initialize SharedPreferences
fun initialize(context : Context) {
LogUtil.print("ServiceManager", "Initializing ServiceManager")
jpushRegister(context)
MyMqttClient.initialize(deviceId = DeviceUtil.getAndroidId(context))
}
// Register JPush
private fun jpushRegister(context : Context) {
try {
JCollectionAuth.setAuth(context, true)
JPushInterface.setDebugMode(false)
JPushInterface.init(context)
LogUtil.print("ServiceManager", "JPush initialized successfully")
} catch (e : Exception) {
LogUtil.print("ServiceManager", "JPush initialization failed: ${e.message}")
}
}
// Handle incoming push messages
@Synchronized
fun handlerPushMsg(msg : String) {
LogUtil.print("handlerPushMsg", "Received push message: $msg")
// 优化后的重复消息判断
lastJPushBean?.let {
if (System.nanoTime() - it.time < DUPLICATE_MSG_THRESHOLD && it.msg == msg.hashCode()) {
LogUtil.print("MessageHandler", "Duplicate message detected (hash: ${msg})")
return
}
}
if (msg.startsWith("broadcast:")) {
lastJPushBean = LastJPushBean(msg = msg.hashCode())
PushMessageLiveData.postPushMessage(msg)
return
}
try {
lastJPushBean = LastJPushBean(msg = msg.hashCode())
val jpushOrderInfoBean = Gson().fromJson(msg, JpushBean::class.java)
sendSystemNotificationFromMessage(jpushOrderInfoBean)
PushMessageLiveData.postPushMessage(msg)
} catch (e : Exception) {
if (msg.startsWith("broadcast:")) {
PushMessageLiveData.postPushMessage(msg)
}
LogUtil.print("JpushMessage", "Error handling message: ${e.message}")
}
}
private const val CHANNEL_ID = "ImportantMessagesChannel"
private const 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(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
}
private fun sendSystemNotificationFromMessage(jpushOrderInfoBean : JpushBean) {
when (jpushOrderInfoBean.pushType) {
0 -> sendNotification(GlobalData.application,
"您有新的 ${jpushOrderInfoBean.addressProperty ?: ""} ${jpushOrderInfoBean.serviceTypeName ?: ""}: ${jpushOrderInfoBean.taskCode ?: ""}")
1 -> {
when (jpushOrderInfoBean.typeDesc) {
"giveUp" -> sendNotification(GlobalData.application,
"订单:${jpushOrderInfoBean.taskCode ?: ""}已被放弃!")
"revoke" -> sendNotification(GlobalData.application,
"订单:${jpushOrderInfoBean.taskCode ?: ""}已被撤回!")
"reDispatch" -> sendNotification(GlobalData.application,
"订单:${jpushOrderInfoBean.taskCode ?: ""}被改派!")
else -> {}
}
}
3 -> sendNotification(GlobalData.application,
"重要提醒:${jpushOrderInfoBean.tipContent ?: ""}")
else -> {}
}
}
// 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(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(NOTIFICATION_ID, notification)
}
// Disconnect from JPush and MQTT
fun disconnect(context : Context) {
try {
JPushInterface.stopPush(context) // Stop JPush
MyMqttClient.disconnect() // Disconnect MQTT
LogUtil.print("ServiceManager", "Disconnected from JPush and MQTT successfully")
} catch (e : Exception) {
LogUtil.print("ServiceManager", "Error during disconnection: ${e.message}")
}
}
}