Skip to content

Commit 5c3f3d0

Browse files
committed
fix(mobile): retain Done in Android live activity
1 parent e4be495 commit 5c3f3d0

4 files changed

Lines changed: 70 additions & 20 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ internal class ActivityPresentation(data: Map<String, String>, private val activ
115115
}
116116

117117
val chip = when {
118-
!active -> null
118+
!active -> if (phase == ActivityPhase.FAILED) "Failed" else "Done"
119119
phase == null -> data["activity_chip"]?.takeIf { it.isNotBlank() }?.take(7) ?: "Active"
120120
phase == ActivityPhase.RUNNING && activeCount > 1 ->
121121
"${if (activeCount > 9) "9+" else activeCount} live"

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,7 @@ object AgentNotifications {
229229
val remainingMs = (expiresAt - System.currentTimeMillis()).coerceAtMost(MAX_LIFETIME_MS)
230230
val wasActive = prefs.getBoolean("lastActive", false)
231231
prefs.edit().putBoolean("lastActive", active).apply()
232-
val alertsEnabled = Build.VERSION.SDK_INT < Build.VERSION_CODES.O ||
233-
manager(context).getNotificationChannel(ALERT_CHANNEL)?.importance !=
234-
NotificationManager.IMPORTANCE_NONE
235-
val activityAllowed = prefs.getBoolean("ongoing", false) && (active || alertsEnabled)
236-
if (remainingMs <= 0 || !activityAllowed) {
232+
if (remainingMs <= 0 || !prefs.getBoolean("ongoing", false)) {
237233
cancelActivity(context)
238234
prefs.edit().putBoolean("dismissed", false).apply()
239235
return
@@ -261,13 +257,15 @@ object AgentNotifications {
261257
)
262258
val presentation = ActivityPresentation(data, active)
263259
val openThread = contentIntent(context, scheme, data["activity_path"], ACTIVITY_ID)
260+
// Retained results still need Android's ongoing flag to remain promoted.
261+
val ongoing = active || Build.VERSION.SDK_INT >= 36
264262
val builder = base(context, ACTIVITY_CHANNEL)
265-
.setOngoing(active).setOnlyAlertOnce(true).setSilent(true)
263+
.setOngoing(ongoing).setOnlyAlertOnce(true).setSilent(true)
266264
.setTimeoutAfter(remainingMs)
267265
// Live Updates must remain uncolorized to qualify for promotion.
268266
.setColorized(false)
269-
.setRequestPromotedOngoing(active)
270-
.setShortCriticalText(presentation.chip)
267+
.setRequestPromotedOngoing(ongoing)
268+
.setShortCriticalText(if (ongoing) presentation.chip else null)
271269
.setContentIntent(openThread)
272270
.setDeleteIntent(dismissIntent)
273271
presentation.applyTo(builder, context)

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

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ class AgentNotificationsTest {
277277
}
278278

279279
@Test
280-
fun finishedCardIsRetainedSilentlyWithoutOngoingFlagAndExpiresAtTheOriginalDeadline() {
280+
fun finishedCardIsRetainedSilentlyAndExpiresAtTheOriginalDeadline() {
281281
lifecycle.currentState = Lifecycle.State.RESUMED
282282
val expiresAt = System.currentTimeMillis() + 15 * 60 * 1000L
283283
val finished = update("finished", false) + mapOf(
@@ -288,7 +288,7 @@ class AgentNotificationsTest {
288288
AgentNotifications.receive(context, finished)
289289
val card = manager.activeNotifications.single().notification
290290
assertEquals("Agent work failed", card.extras.getString(Notification.EXTRA_TITLE))
291-
assertFalse(card.flags and Notification.FLAG_ONGOING_EVENT != 0)
291+
assertEquals(Build.VERSION.SDK_INT >= 36, card.flags and Notification.FLAG_ONGOING_EVENT != 0)
292292
assertTimeout(card, 1..15 * 60 * 1000L)
293293
AgentNotifications.receive(
294294
context,
@@ -441,9 +441,21 @@ class AgentNotificationsTest {
441441
"android.app.Notification\$BigTextStyle",
442442
card.extras.getString(Notification.EXTRA_TEMPLATE)
443443
)
444-
assertEquals(active, NotificationCompat.isRequestPromotedOngoing(card))
444+
assertEquals(
445+
active || Build.VERSION.SDK_INT >= 36,
446+
NotificationCompat.isRequestPromotedOngoing(card)
447+
)
445448
if (!active) {
446-
assertEquals(null, card.extras.getString(NotificationCompat.EXTRA_SHORT_CRITICAL_TEXT))
449+
assertEquals(
450+
if (Build.VERSION.SDK_INT < 36) {
451+
null
452+
} else if (phase == "failed") {
453+
"Failed"
454+
} else {
455+
"Done"
456+
},
457+
card.extras.getString(NotificationCompat.EXTRA_SHORT_CRITICAL_TEXT)
458+
)
447459
assertEquals(0, card.actions?.size ?: 0)
448460
}
449461
}
@@ -594,7 +606,7 @@ class AgentNotificationsTest {
594606
}
595607

596608
@Test
597-
fun liveUpdateChipChangesWithActivityAndClearsOnCompletion() {
609+
fun liveUpdateChipChangesWithActivityAndShowsDoneOnCompletion() {
598610
lifecycle.currentState = Lifecycle.State.RESUMED
599611
AgentNotifications.receive(context, update("work", true))
600612
assertEquals(
@@ -618,9 +630,13 @@ class AgentNotificationsTest {
618630
)
619631
)
620632
val finishedCard = manager.activeNotifications.single().notification
621-
assertEquals(null, finishedCard.extras.getString(NotificationCompat.EXTRA_SHORT_CRITICAL_TEXT))
622-
assertFalse(NotificationCompat.isRequestPromotedOngoing(finishedCard))
623-
assertFalse(finishedCard.flags and Notification.FLAG_ONGOING_EVENT != 0)
633+
val promoted = Build.VERSION.SDK_INT >= 36
634+
assertEquals(
635+
if (promoted) "Done" else null,
636+
finishedCard.extras.getString(NotificationCompat.EXTRA_SHORT_CRITICAL_TEXT)
637+
)
638+
assertEquals(promoted, NotificationCompat.isRequestPromotedOngoing(finishedCard))
639+
assertEquals(promoted, finishedCard.flags and Notification.FLAG_ONGOING_EVENT != 0)
624640
}
625641

626642
@Test
@@ -667,7 +683,7 @@ class AgentNotificationsTest {
667683
@Config(sdk = [26, 33, 36], manifest = Config.NONE)
668684
class AgentNotificationChannelsTest {
669685
@Test
670-
fun mutedAlertChannelKeepsRegisteredActivityUpdatingAndClearsItOnCompletion() {
686+
fun mutedAlertChannelKeepsTheSameActivityThroughCompletionUntilDismissedOrExpired() {
671687
val context = RuntimeEnvironment.getApplication()
672688
val manager = context.getSystemService(NotificationManager::class.java)
673689
shadowOf(manager).setNotificationsEnabled(true)
@@ -684,7 +700,8 @@ class AgentNotificationChannelsTest {
684700
NotificationChannel("agent-alerts", "Agent alerts", NotificationManager.IMPORTANCE_NONE)
685701
)
686702
AgentNotifications.receive(context, running)
687-
assertEquals("t3-agent-activity", manager.activeNotifications.single().tag)
703+
val initialCard = manager.activeNotifications.single()
704+
assertEquals("t3-agent-activity", initialCard.tag)
688705

689706
AgentNotifications.configure(context, "device", "user", "t3code-dev", true)
690707
AgentNotifications.receive(
@@ -699,10 +716,45 @@ class AgentNotificationChannelsTest {
699716

700717
val finished = running + mapOf(
701718
"active" to "false",
719+
"activity_phase" to "completed",
720+
"activity_line_0" to "Done\tTest thread\tTest project",
702721
"activity_expires_at" to (System.currentTimeMillis() + 900000).toString()
703722
)
704723
AgentNotifications.receive(context, finished)
705724
AgentNotifications.receive(context, finished)
725+
val completedCard = manager.activeNotifications.single()
726+
assertEquals(initialCard.id, completedCard.id)
727+
assertEquals(initialCard.tag, completedCard.tag)
728+
assertEquals("agent-activity", completedCard.notification.channelId)
729+
assertEquals(
730+
"Test thread",
731+
completedCard.notification.extras.getString(Notification.EXTRA_TITLE)
732+
)
733+
assertEquals(
734+
"Done Test project",
735+
completedCard.notification.extras.getCharSequence(Notification.EXTRA_TEXT).toString()
736+
)
737+
val promoted = Build.VERSION.SDK_INT >= 36
738+
assertEquals(promoted, NotificationCompat.isRequestPromotedOngoing(completedCard.notification))
739+
assertEquals(
740+
promoted,
741+
completedCard.notification.flags and Notification.FLAG_ONGOING_EVENT != 0
742+
)
743+
assertEquals(
744+
if (promoted) "Done" else null,
745+
completedCard.notification.extras.getString(NotificationCompat.EXTRA_SHORT_CRITICAL_TEXT)
746+
)
747+
748+
AgentNotifications.dismiss(context)
749+
AgentNotifications.receive(context, finished)
750+
assertTrue(manager.activeNotifications.isEmpty())
751+
752+
AgentNotifications.receive(context, running)
753+
assertEquals("t3-agent-activity", manager.activeNotifications.single().tag)
754+
AgentNotifications.receive(
755+
context,
756+
finished + ("activity_expires_at" to (System.currentTimeMillis() - 1).toString())
757+
)
706758
assertTrue(manager.activeNotifications.isEmpty())
707759
}
708760
}

‎docs/user/mobile-notifications.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Sign in to T3 Connect, link your environments, and enable **Device Notifications
44

55
Enable **Ongoing Agent Activity** on Android or **Live Activity Updates** on iOS to follow work without opening the app. Finished results remain visible for up to 15 minutes. You can dismiss an Android activity card without disabling alerts; turn off ongoing activity in Settings to stop future cards.
66

7-
For activity without ordinary alerts on Android 8 or newer, leave **Ongoing agent activity** enabled and turn off **Agent alerts** in Android's notification settings for T3 Code. Push delivery stays registered, and the activity card disappears when work finishes instead of leaving a finished notification. Keep app-wide notification permission enabled so Android can display the activity.
7+
For activity without ordinary alerts on Android 8 or newer, leave **Ongoing agent activity** enabled and turn off **Agent alerts** in Android's notification settings for T3 Code. Push delivery stays registered, and the same activity card shows the finished result without a separate alert. On Android 16 and newer, the finished result remains a Live Update until dismissed or expired. Keep app-wide notification permission and **Live updates** enabled so Android can display it.
88

99
Ordinary alerts stay quiet while the mobile app is in the foreground. Ongoing activity continues to update. Viewing a thread on another device does not silence your phone's alerts.
1010

0 commit comments

Comments
 (0)