Skip to content

Commit 00b6fd5

Browse files
nicofishmanclaude
andcommitted
fix(mobile): keep late Android alerts hidden after their dismissal
FCM does not preserve order, so an alert sent before a thread's dismissal could arrive after it and post anyway. Remember each thread's latest dismissal send time and drop alerts sent earlier; newer runs still alert. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 688025f commit 00b6fd5

2 files changed

Lines changed: 47 additions & 4 deletions

File tree

‎apps/mobile/modules/t3-agent-notifications/android/src/main/java/expo/modules/t3agentnotifications/AgentNotifications.kt‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,30 +127,51 @@ object AgentNotifications {
127127
val fresh = System.currentTimeMillis() - updatedAt in -MAX_MESSAGE_AGE_MS..MAX_MESSAGE_AGE_MS
128128
if (registered && fresh) {
129129
// Needs no permission, so it also clears alerts posted before notifications were disabled.
130-
data["dismiss_group"]?.let { dismissAlerts(context, it) }
130+
data["dismiss_group"]?.let { dismissAlerts(context, prefs, it, updatedAt) }
131131
if (NotificationManagerCompat.from(context).areNotificationsEnabled()) {
132132
channels(context)
133133
val scheme = prefs.getString("scheme", "t3code") ?: "t3code"
134-
showAlert(context, prefs, scheme, data)
134+
showAlert(context, prefs, scheme, data, updatedAt)
135135
updateActivity(context, prefs, scheme, data, updatedAt)
136136
}
137137
}
138138
}
139139

140140
/** Removes a thread's alerts once it has been read on any client. */
141-
private fun dismissAlerts(context: Context, group: String) {
141+
private fun dismissAlerts(
142+
context: Context,
143+
prefs: SharedPreferences,
144+
group: String,
145+
updatedAt: Long
146+
) {
142147
val manager = manager(context)
143148
manager.activeNotifications.filter { it.tag == ALERT_TAG && it.notification.group == group }
144149
.forEach { manager.cancel(it.tag, it.id) }
145150
manager.cancel("$ALERT_TAG-summary:$group", 0)
151+
// FCM does not keep order, so an alert sent before this dismissal can still arrive after it.
152+
val marks = dismissals(prefs).filterKeys { it != group }.map { (g, at) -> "$at\t$g" }
153+
prefs.edit().putString(
154+
"dismissedGroups",
155+
(marks.takeLast(63) + "$updatedAt\t$group").joinToString("\n")
156+
).apply()
146157
}
147158

159+
/** Relay send time of each thread's latest dismissal, oldest first. */
160+
private fun dismissals(prefs: SharedPreferences): Map<String, Long> =
161+
prefs.getString("dismissedGroups", null).orEmpty().lineSequence().mapNotNull { line ->
162+
line.split('\t', limit = 2).takeIf { it.size == 2 }
163+
?.let { (at, group) -> at.toLongOrNull()?.let { group to it } }
164+
}.toMap()
165+
148166
private fun showAlert(
149167
context: Context,
150168
prefs: SharedPreferences,
151169
scheme: String,
152-
data: Map<String, String>
170+
data: Map<String, String>,
171+
updatedAt: Long
153172
) {
173+
val dismissedAt = data["alert_group"]?.let { dismissals(prefs)[it] }
174+
if (dismissedAt != null && updatedAt <= dismissedAt) return
154175
// Queue retries carry the same alert id. Keep a bounded history even when
155176
// notification A is retried after notification B has already arrived.
156177
val alertId = data["alert_id"]

‎apps/mobile/modules/t3-agent-notifications/android/src/test/java/expo/modules/t3agentnotifications/AgentNotificationsTest.kt‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,28 @@ class AgentNotificationsTest {
111111
assertEquals("environment/other", remaining.single().notification.group)
112112
}
113113

114+
@Test
115+
fun alertSentBeforeADismissalStaysHiddenWhenItArrivesLate() {
116+
val sentAt = System.currentTimeMillis()
117+
val dismissal = mapOf(
118+
"device_id" to "device",
119+
"user_id" to "user",
120+
"updated_at" to (sentAt + 1).toString(),
121+
"dismiss_group" to "environment/thread",
122+
)
123+
AgentNotifications.receive(context, dismissal)
124+
// A cold start reconfigures the same account and must keep the mark.
125+
AgentNotifications.configure(context, "device", "user", "t3code-dev", true)
126+
AgentNotifications.receive(context, update("late", false) + ("updated_at" to sentAt.toString()))
127+
assertTrue(manager.activeNotifications.none { it.tag == "t3-agent-alert" })
128+
129+
AgentNotifications.receive(
130+
context,
131+
update("next-run", false) + ("updated_at" to (sentAt + 2).toString())
132+
)
133+
assertEquals(1, manager.activeNotifications.count { it.tag == "t3-agent-alert" })
134+
}
135+
114136
@Test
115137
fun dismissClearsAlertsEvenAfterNotificationsAreDisabled() {
116138
AgentNotifications.receive(context, update("before-disable", false))

0 commit comments

Comments
 (0)