Skip to content

Commit c41af1a

Browse files
committed
feat(mobile): expose Android Live Update chips and settings
1 parent c542b78 commit c41af1a

9 files changed

Lines changed: 155 additions & 4 deletions

File tree

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,21 @@ object AgentNotifications {
212212
if (lines.isEmpty()) body else lines.joinToString("\n")
213213
)
214214
val notification = base(context, ACTIVITY_CHANNEL)
215-
.setContentTitle(data["activity_title"].orEmpty().take(120))
215+
.setContentTitle(
216+
data["activity_title"]?.takeIf {
217+
it.isNotBlank()
218+
}?.take(120) ?: "Agent activity"
219+
)
216220
.setContentText(body)
217221
.setStyle(style)
218222
.setOngoing(active).setOnlyAlertOnce(true).setSilent(true)
219223
.setTimeoutAfter(remainingMs)
220224
// Live Updates must remain uncolorized to qualify for promotion.
221225
.setColorized(false)
222226
.setRequestPromotedOngoing(active)
227+
.setShortCriticalText(
228+
if (active) data["activity_chip"]?.takeIf { it.isNotBlank() }?.take(7) ?: "Active" else null
229+
)
223230
.setContentIntent(contentIntent(context, scheme, data["activity_path"], ACTIVITY_ID))
224231
.setDeleteIntent(dismissIntent)
225232
.addAction(0, "Dismiss", dismissIntent)

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package expo.modules.t3agentnotifications
22

3+
import android.content.ActivityNotFoundException
4+
import android.content.Intent
5+
import android.os.Build
6+
import android.provider.Settings
37
import expo.modules.kotlin.modules.Module
48
import expo.modules.kotlin.modules.ModuleDefinition
59

@@ -21,5 +25,23 @@ class T3AgentNotificationsModule : Module() {
2125
Function("clear") {
2226
appContext.reactContext?.let { AgentNotifications.clear(it) }
2327
}
28+
29+
Function("openLiveUpdateSettings") {
30+
val context = appContext.reactContext
31+
if (context == null || Build.VERSION.SDK_INT < 36) {
32+
false
33+
} else {
34+
try {
35+
context.startActivity(
36+
Intent(Settings.ACTION_APP_NOTIFICATION_PROMOTION_SETTINGS)
37+
.putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
38+
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
39+
)
40+
true
41+
} catch (_: ActivityNotFoundException) {
42+
false
43+
}
44+
}
45+
}
2446
}
2547
}

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,46 @@ class AgentNotificationsTest {
399399
assertEquals(Notification.VISIBILITY_PRIVATE, card.visibility)
400400
}
401401

402+
@Test
403+
fun liveUpdateChipChangesWithActivityAndClearsOnCompletion() {
404+
lifecycle.currentState = Lifecycle.State.RESUMED
405+
AgentNotifications.receive(context, update("work", true))
406+
assertEquals(
407+
"Active",
408+
manager.activeNotifications.single().notification.extras
409+
.getString(NotificationCompat.EXTRA_SHORT_CRITICAL_TEXT)
410+
)
411+
AgentNotifications.receive(context, update("input", true) + ("activity_chip" to "Review"))
412+
val activeCard = manager.activeNotifications.single().notification
413+
assertEquals(
414+
"Review",
415+
activeCard.extras.getString(NotificationCompat.EXTRA_SHORT_CRITICAL_TEXT)
416+
)
417+
assertTrue(NotificationCompat.isRequestPromotedOngoing(activeCard))
418+
419+
AgentNotifications.receive(
420+
context,
421+
update("done", false) + mapOf(
422+
"activity_chip" to "Review",
423+
"activity_expires_at" to (System.currentTimeMillis() + 900000).toString()
424+
)
425+
)
426+
val finishedCard = manager.activeNotifications.single().notification
427+
assertEquals(null, finishedCard.extras.getString(NotificationCompat.EXTRA_SHORT_CRITICAL_TEXT))
428+
assertFalse(NotificationCompat.isRequestPromotedOngoing(finishedCard))
429+
assertFalse(finishedCard.flags and Notification.FLAG_ONGOING_EVENT != 0)
430+
}
431+
432+
@Test
433+
fun blankTitleCannotMakeAnActivityIneligibleForPromotion() {
434+
lifecycle.currentState = Lifecycle.State.RESUMED
435+
AgentNotifications.receive(context, update("work", true) + ("activity_title" to " "))
436+
assertEquals(
437+
"Agent activity",
438+
manager.activeNotifications.single().notification.extras.getString(Notification.EXTRA_TITLE)
439+
)
440+
}
441+
402442
@Test
403443
fun expiredMalformedAndFutureMessagesCannotDisplayOrPoisonLaterUpdates() {
404444
val invalid = update("invalid", true)

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,25 @@ import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
22

33
const mocks = vi.hoisted(() => ({
44
os: "android",
5-
native: null as { configure?: ReturnType<typeof vi.fn>; clear?: ReturnType<typeof vi.fn> } | null,
5+
version: 36,
6+
openSettings: vi.fn(),
7+
native: null as {
8+
configure?: ReturnType<typeof vi.fn>;
9+
clear?: ReturnType<typeof vi.fn>;
10+
openLiveUpdateSettings?: ReturnType<typeof vi.fn>;
11+
} | null,
612
config: { scheme: ["t3code-preview"], extra: { iosPersonalTeamBuild: false } },
713
requireModule: vi.fn(),
814
}));
915

1016
vi.mock("expo", () => ({ requireOptionalNativeModule: mocks.requireModule }));
1117
vi.mock("expo-constants", () => ({ default: { expoConfig: mocks.config } }));
1218
vi.mock("react-native", () => ({
19+
Linking: { openSettings: mocks.openSettings },
1320
Platform: {
21+
get Version() {
22+
return mocks.version;
23+
},
1424
get OS() {
1525
return mocks.os;
1626
},
@@ -20,12 +30,39 @@ vi.mock("react-native", () => ({
2030
beforeEach(() => {
2131
vi.resetModules();
2232
mocks.os = "android";
33+
mocks.version = 36;
34+
mocks.openSettings.mockReset().mockResolvedValue(undefined);
2335
mocks.native = { configure: vi.fn(), clear: vi.fn() };
2436
mocks.config.extra.iosPersonalTeamBuild = false;
2537
mocks.requireModule.mockReset().mockImplementation(() => mocks.native);
2638
});
2739

2840
describe("Android native notification capability", () => {
41+
it("opens the Live Update controls on supported Android builds", async () => {
42+
mocks.native!.openLiveUpdateSettings = vi.fn(() => true);
43+
const { openAndroidLiveUpdateSettings, supportsAndroidLiveUpdateSettings } =
44+
await import("./androidNotifications");
45+
expect(supportsAndroidLiveUpdateSettings()).toBe(true);
46+
await openAndroidLiveUpdateSettings();
47+
expect(mocks.native!.openLiveUpdateSettings).toHaveBeenCalledOnce();
48+
expect(mocks.openSettings).not.toHaveBeenCalled();
49+
mocks.version = 35;
50+
expect(supportsAndroidLiveUpdateSettings()).toBe(false);
51+
mocks.os = "ios";
52+
mocks.version = 36;
53+
expect(supportsAndroidLiveUpdateSettings()).toBe(false);
54+
});
55+
56+
it.each([undefined, vi.fn(() => false)])(
57+
"falls back to app settings for older binaries or missing system activities (%j)",
58+
async (openLiveUpdateSettings) => {
59+
if (openLiveUpdateSettings) mocks.native!.openLiveUpdateSettings = openLiveUpdateSettings;
60+
const { openAndroidLiveUpdateSettings } = await import("./androidNotifications");
61+
await openAndroidLiveUpdateSettings();
62+
expect(mocks.openSettings).toHaveBeenCalledOnce();
63+
},
64+
);
65+
2966
it("uses the installed module and the build variant's deep-link scheme", async () => {
3067
const { configureAndroidAgentNotifications, clearAndroidAgentNotifications } =
3168
await import("./androidNotifications");

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import Constants from "expo-constants";
22
import { requireOptionalNativeModule } from "expo";
3-
import { Platform } from "react-native";
3+
import { Linking, Platform } from "react-native";
44

55
interface AndroidAgentNotifications {
66
configure(deviceId: string, userId: string, scheme: string, ongoingEnabled: boolean): void;
77
clear(): void;
8+
openLiveUpdateSettings?(): boolean;
89
}
910

1011
const native =
@@ -33,3 +34,13 @@ export function configureAndroidAgentNotifications(
3334
export function clearAndroidAgentNotifications(): void {
3435
native?.clear?.();
3536
}
37+
38+
export function supportsAndroidLiveUpdateSettings(): boolean {
39+
return Platform.OS === "android" && Number(Platform.Version) >= 36;
40+
}
41+
42+
export async function openAndroidLiveUpdateSettings(): Promise<void> {
43+
if (!native?.openLiveUpdateSettings?.()) {
44+
await Linking.openSettings();
45+
}
46+
}

‎apps/mobile/src/features/settings/SettingsRouteScreen.tsx‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ import {
2121
import { AndroidScreenHeader } from "../../components/AndroidScreenHeader";
2222
import { AppText as Text, AppTextInput as TextInput } from "../../components/AppText";
2323
import { supportsAgentAwarenessPush } from "../agent-awareness/capabilities";
24+
import {
25+
openAndroidLiveUpdateSettings,
26+
supportsAndroidLiveUpdateSettings,
27+
} from "../agent-awareness/androidNotifications";
2428
import { setLiveActivityUpdatesEnabled } from "../agent-awareness/liveActivityPreferences";
2529
import { requestAgentNotificationPermission } from "../agent-awareness/notificationPermissions";
2630
import {
@@ -541,7 +545,13 @@ function ConfiguredSettingsRouteScreen() {
541545
liveActivityStatus === "linking"
542546
}
543547
icon="bolt.circle"
544-
label={Platform.OS === "android" ? "Ongoing Agent Activity" : "Live Activity Updates"}
548+
label={
549+
Platform.OS === "android"
550+
? supportsAndroidLiveUpdateSettings()
551+
? "Agent Live Updates"
552+
: "Ongoing Agent Activity"
553+
: "Live Activity Updates"
554+
}
545555
subtitle={agentAwarenessSubtitle}
546556
// Same gate: a saved preference is meaningless until the device
547557
// registration the relay needs to push updates has succeeded.
@@ -552,6 +562,20 @@ function ConfiguredSettingsRouteScreen() {
552562
}
553563
onValueChange={handleLiveActivitiesChange}
554564
/>
565+
{supportsAndroidLiveUpdateSettings() ? (
566+
<SettingsRow
567+
icon="bolt.circle"
568+
label="Live Update Settings"
569+
onPress={() => {
570+
void openAndroidLiveUpdateSettings().catch(() => {
571+
Alert.alert(
572+
"Couldn't open Settings",
573+
"Open Android Settings, select T3 Code, then enable Live Updates in Notifications.",
574+
);
575+
});
576+
}}
577+
/>
578+
) : null}
555579
</SettingsSection>
556580

557581
<GeneralSettingsSection />

‎docs/operations/android-notifications.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ The Android app receives Firebase Cloud Messaging (FCM) data messages. The relay
66

77
The app's minimum is Android 7.0 (API 24), declared in `app.config.ts` and enforced by the relay's device-registration schema. Compile/target SDK versions follow the locked Expo/React Native toolchain (currently API 36). Notification channels begin at API 26; the notification permission prompt begins at API 33. Live Update promotion requires API 36 and remains subject to system settings and device support. Alerts and ordinary activity cards work below API 36.
88

9+
On Android 16+, open T3 Code Settings → Live Update Settings to allow status bar chips. Android controls this separately from notification permission. The chip reads `Active` during work and `Review` when an agent needs input or approval; completed work returns to a normal notification. Use an Android 16 QPR2 or newer emulator image to verify the shipped promotion behavior.
10+
911
API 24–25 use a single inexact system alarm to expire cards after process exit, with no exact-alarm permission. Android can delay that alarm in power-saving modes. API 26+ use notification timeouts. Disabling activity, dismissal, account changes and sign-out cancel the legacy alarm. A stale expiry broadcast cannot remove a newer run's card.
1012

1113
The native notification tests cover API 24, 26, 33 and 36 (plus API 25 for legacy expiry) using Robolectric. To compile the module, run these tests and run Android lint, use the following command from a generated `apps/mobile/android` project with JDK 21 available. No Firebase, signing or relay secrets are required:

‎infra/relay/src/agentActivity/FcmDeliveries.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,12 @@ describe("Android delivery routing", () => {
616616
]);
617617
const data = androidActivityData(aggregate);
618618
expect(data.activity_title).toBe("3 active agents · 2 need attention");
619+
expect(data.activity_chip).toBe("Review");
620+
expect(androidActivityData(aggregateFor([state])).activity_chip).toBe("Active");
621+
expect(
622+
androidActivityData(aggregateFor([{ ...state, phase: "completed" }])).activity_chip,
623+
).toBe("");
624+
expect(androidActivityData(null).activity_chip).toBe("");
619625
expect(
620626
Object.entries(data)
621627
.filter(([key]) => key.startsWith("activity_line_"))

‎infra/relay/src/agentActivity/fcmPayloads.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export function androidActivityData(aggregate: RelayAgentActivityAggregateState
4040
);
4141
return {
4242
active: String(activeCount > 0),
43+
// Keep the status bar chip short enough to display alongside the app icon.
44+
activity_chip: activeCount > 0 ? (attentionCount > 0 ? "Review" : "Active") : "",
4345
activity_title: title,
4446
activity_body: hero
4547
? `${hero.status}: ${clean(hero.threadTitle)} · ${clean(hero.projectTitle)}`

0 commit comments

Comments
 (0)