fix: 去抖改为暂存合并,不丢弃消息

问题:多条MQTT消息同时到达(间隔<1s),去抖直接丢弃后续消息。
旧版是延迟处理而非丢弃。

修复:
- 去抖窗口内的消息暂存到pendingJsons
- 1s后延迟任务统一处理暂存消息(只加ID,不重复震动)
- 合并后统一发一次NewTaskArrived事件更新横幅
- MainActivity改为监听NewTaskArrived事件显示横幅

时序示例:
t=0ms:   消息1(A) → 立即处理, pendingTaskIds=[A], 横幅"1条"
t=100ms: 消息2(B) → 暂存
t=200ms: 消息3(C) → 暂存
t=1000ms: 延迟任务 → 处理B+C, pendingTaskIds=[A,B,C], 横幅"3条"

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
dongliang
2026-04-29 15:27:06 +09:30
parent 1ffdefc887
commit 379d784f70
3 changed files with 79 additions and 41 deletions

View File

@@ -82,22 +82,21 @@ class MainActivity : AppCompatActivity() {
// ===== MQTT 新任务 → 通知横幅 =====
/** 监听 MQTT 消息type=1 时通知横幅 */
/** 监听 MQTT 消息和通知事件 */
private fun observeMqttMessages() {
activityScope.launch {
eventBus.events.collect { event ->
when (event) {
is AppEvent.MqttMessageReceived -> {
if (event.type == 1) {
// 交给 NotificationManager 处理(去抖+震动+亮屏+事件
val handled = notificationManager.onNewTaskMessage(event.rawJson)
if (handled) {
// 显示横幅
notificationBanner.show(notificationManager.pendingCount)
}
// 交给 NotificationManager 处理(去抖+合并+震动+亮屏)
notificationManager.onNewTaskMessage(event.rawJson)
}
}
// 横幅点击由 HomeFragment 处理跳转
// NotificationManager 处理完后发出事件 → 显示/更新横幅
is AppEvent.NewTaskArrived -> {
notificationBanner.show(event.count)
}
else -> {}
}
}