From f0f6810efdde88bffd0cfe707f7b1c9620a95b24 Mon Sep 17 00:00:00 2001 From: songzhiling <17630035658@163.com> Date: Mon, 21 Apr 2025 11:36:48 +0800 Subject: [PATCH] =?UTF-8?q?fix(servicing):=20=E4=BC=98=E5=8C=96=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E6=B6=88=E6=81=AF=E5=88=A4=E6=96=AD=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改 LastJPushBean 数据类,将 msg 类型从 String 改为 Int,使用 hashCode() 进行比较 - 使用纳秒级时间戳替代毫秒级时间戳,提高精度- 添加 DUPLICATE_MSG_THRESHOLD常量,用于定义重复消息的时间阈值 - 在 handlerPushMsg 函数中实现新的重复消息判断逻辑,提高效率 - 使用 @Synchronized 注解确保线程安全 --- .../java/com/za/service/ServiceManager.kt | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/servicing/src/main/java/com/za/service/ServiceManager.kt b/servicing/src/main/java/com/za/service/ServiceManager.kt index bd8186e..afadb36 100644 --- a/servicing/src/main/java/com/za/service/ServiceManager.kt +++ b/servicing/src/main/java/com/za/service/ServiceManager.kt @@ -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() 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)