fix(servicing): 优化重复消息判断逻辑
- 修改 LastJPushBean 数据类,将 msg 类型从 String 改为 Int,使用 hashCode() 进行比较 - 使用纳秒级时间戳替代毫秒级时间戳,提高精度- 添加 DUPLICATE_MSG_THRESHOLD常量,用于定义重复消息的时间阈值 - 在 handlerPushMsg 函数中实现新的重复消息判断逻辑,提高效率 - 使用 @Synchronized 注解确保线程安全
This commit is contained in:
@ -27,11 +27,13 @@ interface PushListener {
|
||||
fun importantTip(jpushBean : JpushBean)
|
||||
}
|
||||
|
||||
data class LastJPushBean(val msg : String, val time : Long = System.currentTimeMillis())
|
||||
data class LastJPushBean(val msg : Int, val time : Long = System.nanoTime())
|
||||
|
||||
object ServiceManager {
|
||||
private val pushListener = AtomicReference<PushListener>()
|
||||
private var lastJPushBean : LastJPushBean? = null
|
||||
private const val DUPLICATE_MSG_THRESHOLD = 3000L // 3秒
|
||||
|
||||
|
||||
// Initialize SharedPreferences
|
||||
fun initialize(context : Context) {
|
||||
@ -61,22 +63,26 @@ object ServiceManager {
|
||||
}
|
||||
|
||||
// Handle incoming push messages
|
||||
@Synchronized
|
||||
fun handlerPushMsg(msg : String) {
|
||||
LogUtil.print("JpushMessage", "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.hashCode())
|
||||
handleBroadcast(msg)
|
||||
return
|
||||
}
|
||||
try {
|
||||
lastJPushBean = LastJPushBean(msg.hashCode())
|
||||
val jpushOrderInfoBean = Gson().fromJson(msg, JpushBean::class.java)
|
||||
if (lastJPushBean != null && (System.currentTimeMillis() - lastJPushBean !!.time < 3000) && lastJPushBean !!.msg == jpushOrderInfoBean.taskCode) {
|
||||
LogUtil.print("MessageHandler", "Duplicate message: " + lastJPushBean?.msg)
|
||||
return
|
||||
}
|
||||
|
||||
jpushOrderInfoBean?.taskCode?.let {
|
||||
lastJPushBean = LastJPushBean(it)
|
||||
}
|
||||
sendSystemNotificationFromMessage(jpushOrderInfoBean)
|
||||
when (jpushOrderInfoBean.pushType) {
|
||||
0 -> newOrderMsg(jpushOrderInfoBean)
|
||||
|
Reference in New Issue
Block a user