Skip to content

Commit bf8238e

Browse files
feat(mobile): suppress only the on-screen thread's alert on Android
Android used to silence every agent alert while the app was foregrounded, while iOS (since the SDK 58 handler) only quiets the thread already on screen. The navigation hook now reports the thread route to the Kotlin FCM handler, which compares it with the alert path before suppressing, so both platforms behave the same. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 110cc68 commit bf8238e

6 files changed

Lines changed: 40 additions & 4 deletions

File tree

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ object AgentNotifications {
8888
.forEach { manager.cancel(it.tag, it.id) }
8989
}
9090

91+
/** Records the thread route the app is showing, or null when none is open. */
92+
fun setThreadOnScreen(context: Context, path: String?) {
93+
context.getSharedPreferences(STORE, Context.MODE_PRIVATE).edit()
94+
.putString("threadOnScreen", path).apply()
95+
}
96+
9197
@Synchronized
9298
fun dismiss(context: Context) {
9399
context.getSharedPreferences(
@@ -135,9 +141,13 @@ object AgentNotifications {
135141
val seen = prefs.getString("seenAlertsOrdered", null)?.split('\n')
136142
?: prefs.getStringSet("seenAlerts", emptySet()).orEmpty().toList()
137143
if (alertId != null && alertId !in seen) {
138-
// Match iOS foreground presentation. Consume suppressed alerts as well,
139-
// so a delivery retry cannot surface them after the app backgrounds.
140-
if (!ProcessLifecycleOwner.get().lifecycle.currentState.isAtLeast(Lifecycle.State.RESUMED)) {
144+
// Match iOS foreground presentation: only the thread already on screen
145+
// stays quiet, since its feed shows the same change. Consume suppressed
146+
// alerts as well, so a delivery retry cannot surface them after the app
147+
// backgrounds.
148+
val resumed = ProcessLifecycleOwner.get().lifecycle.currentState.isAtLeast(Lifecycle.State.RESUMED)
149+
val onScreen = resumed && data["alert_path"] == prefs.getString("threadOnScreen", null)
150+
if (!onScreen) {
141151
val title = data["alert_title"].orEmpty().take(120)
142152
// Grouped alerts list up to five 120-character thread titles.
143153
val body = data["alert_body"].orEmpty().take(608)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ class T3AgentNotificationsModule : Module() {
2626
appContext.reactContext?.let { AgentNotifications.clear(it) }
2727
}
2828

29+
Function("setThreadOnScreen") { path: String? ->
30+
appContext.reactContext?.let { AgentNotifications.setThreadOnScreen(it, path) }
31+
}
32+
2933
Function("openLiveUpdateSettings") {
3034
val context = appContext.reactContext
3135
if (context == null || Build.VERSION.SDK_INT < 36) {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class AgentNotificationsTest {
4949
)
5050
AgentNotifications.clear(context)
5151
AgentNotifications.configure(context, "device", "user", "t3code-dev", true)
52+
// The fixtures alert for this thread; a resumed app is showing it.
53+
AgentNotifications.setThreadOnScreen(context, "/threads/environment/thread")
5254
}
5355

5456
private fun update(alertId: String, active: Boolean) = mapOf(
@@ -93,6 +95,14 @@ class AgentNotificationsTest {
9395
)
9496
}
9597

98+
@Test
99+
fun foregroundAlertsForThreadsThatAreNotOnScreen() {
100+
lifecycle.currentState = Lifecycle.State.RESUMED
101+
AgentNotifications.setThreadOnScreen(context, "/threads/environment/other")
102+
AgentNotifications.receive(context, update("elsewhere", false))
103+
assertEquals("t3-agent-alert", manager.activeNotifications.single().tag)
104+
}
105+
96106
@Test
97107
fun foregroundSuppressesAlertsWhileOngoingActivityStillUpdatesAndClears() {
98108
lifecycle.currentState = Lifecycle.State.RESUMED

‎apps/mobile/src/features/agent-awareness/androidNotifications.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Linking, Platform } from "react-native";
55
interface AndroidAgentNotifications {
66
configure(deviceId: string, userId: string, scheme: string, ongoingEnabled: boolean): void;
77
clear(): void;
8+
setThreadOnScreen?(path: string | null): void;
89
openLiveUpdateSettings?(): boolean;
910
}
1011

@@ -35,6 +36,11 @@ export function clearAndroidAgentNotifications(): void {
3536
native?.clear?.();
3637
}
3738

39+
/** Tells the FCM handler which thread route is on screen so its alerts stay quiet. */
40+
export function setAndroidThreadOnScreen(path: string | null): void {
41+
native?.setThreadOnScreen?.(path);
42+
}
43+
3844
export function supportsAndroidLiveUpdateSettings(): boolean {
3945
return Platform.OS === "android" && Number(Platform.Version) >= 36;
4046
}

‎apps/mobile/src/features/agent-awareness/notificationNavigation.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useEffect, useRef } from "react";
22
import * as Notifications from "expo-notifications";
33
import { useLinkTo } from "@react-navigation/native";
44

5+
import { setAndroidThreadOnScreen } from "./androidNotifications";
56
import { foregroundNotificationBehavior } from "./foregroundNotificationBehavior";
67
import { routeAgentNotificationResponseOnce, threadDeepLinkOnScreen } from "./notificationPayload";
78
import { consumeLastAgentNotificationResponse } from "./notificationResponseConsumer";
@@ -13,6 +14,11 @@ export function useAgentNotificationNavigation(pathname: string): void {
1314
// current route without re-registering on every navigation.
1415
const deepLinkOnScreen = useRef<string | null>(null);
1516
deepLinkOnScreen.current = threadDeepLinkOnScreen(pathname);
17+
// Android alerts are built natively from FCM data, so the route travels
18+
// to Kotlin instead of through a JS handler.
19+
useEffect(() => {
20+
setAndroidThreadOnScreen(threadDeepLinkOnScreen(pathname));
21+
}, [pathname]);
1622

1723
useEffect(() => {
1824
Notifications.setNotificationHandler({

‎docs/operations/android-notifications.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ vp run push:android:smoke /path/service-account.json /path/device.json completed
6868

6969
Supported states are `running`, `approval`, `input`, `completed`, `failed`, and `end`. Firebase acceptance is not proof that a device displayed the message. Check the actual notification, background the app, and test a notification tap. Also test dismissal, disabling ongoing activity, sign-out, token rotation, and delivery after the app process has exited. Android Settings **Force stop** intentionally prevents delivery until the app is opened again.
7070

71-
Android suppresses ordinary alerts while the app is foregrounded, matching iOS notification presentation. Activity cards still update in the foreground and retain finished results silently. Check that completion stays quiet with the app open, that a later completion alerts after backgrounding, and that retrying a foreground-suppressed alert does not show it later. This uses the app lifecycle on the receiving phone, not thread visibility on other clients.
71+
With the app in the foreground, Android suppresses an alert only for the thread currently on screen, matching iOS notification presentation; alerts for other threads still show. Activity cards still update in the foreground and retain finished results silently. Check that completion stays quiet while its thread is open, alerts while another screen is open, alerts after backgrounding, and that retrying a suppressed alert does not show it later. This uses the app lifecycle and route on the receiving phone, not thread visibility on other clients.
7272

7373
With ongoing activity enabled, verify two threads entering approval/input together produce one `2 agents need attention` alert, and two observed active threads completing/failing together produce one `2 agents finished` alert. The body lists their titles. The relay shares iOS transition selection and retains its delivered baseline when work finishes; publishing the same states again must not produce another alert. Grouped alerts open the aggregate’s priority thread; individual alerts retain their thread link.
7474

0 commit comments

Comments
 (0)