Skip to content

Commit b5b375e

Browse files
feat(notifications): stack agent alerts by thread on both platforms
The relay tags every APNs alert with a thread-id and every FCM alert with an alert_group of environment/thread, and the Android handler passes it to setGroup. Notification Center and the Android shade now stack alerts from one thread instead of letting a chatty thread crowd out the others. expo-notifications 58 added threadIdentifier to local notifications, but these alerts are remote, so the grouping key rides in the push payload. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 169a7a7 commit b5b375e

6 files changed

Lines changed: 21 additions & 0 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ object AgentNotifications {
161161
.setContentTitle(title).setContentText(body)
162162
.setStyle(NotificationCompat.BigTextStyle().bigText(body))
163163
.setAutoCancel(true)
164+
.setGroup(data["alert_group"]?.takeIf { it.isNotBlank() } ?: ALERT_TAG)
164165
.setContentIntent(contentIntent(context, scheme, data["alert_path"], id))
165166
.build()
166167
manager(context).notify(ALERT_TAG, id, notification)

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class AgentNotificationsTest {
6060
"activity_body" to "Test thread · Working",
6161
"activity_path" to "/threads/environment/thread",
6262
"alert_id" to alertId,
63+
"alert_group" to "environment/thread",
6364
"alert_title" to "Test thread",
6465
"alert_body" to "Done: Test project",
6566
"alert_path" to "/threads/environment/thread",
@@ -76,6 +77,12 @@ class AgentNotificationsTest {
7677
assertEquals("alert-0".hashCode(), manager.activeNotifications.single().id)
7778
}
7879

80+
@Test
81+
fun alertsStackByThreadGroup() {
82+
AgentNotifications.receive(context, update("grouped", false))
83+
assertEquals("environment/thread", manager.activeNotifications.single().notification.group)
84+
}
85+
7986
@Test
8087
fun missingLauncherDoesNotDiscardTheAlert() {
8188
shadowOf(context.packageManager).removeActivity(ComponentName(context, Activity::class.java))

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ describe("ApnsClient", () => {
187187
body: "Input: Project",
188188
},
189189
sound: "default",
190+
"thread-id": "env/thread",
190191
},
191192
environmentId: "env",
192193
threadId: "thread",

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ function makeLiveActivityRequest(input: MakeLiveActivityRequestInput): ApnsLiveA
168168
};
169169
}
170170

171+
function notificationThreadId(notification: ApnsNotificationPayload): string {
172+
return notification.threadId.length > 0
173+
? `${notification.environmentId}/${notification.threadId}`
174+
: "t3-agent-alerts";
175+
}
176+
171177
function makePushNotificationRequest(input: {
172178
readonly token: string;
173179
readonly notification: ApnsNotificationPayload;
@@ -182,6 +188,9 @@ function makePushNotificationRequest(input: {
182188
body: input.notification.body,
183189
},
184190
sound: "default",
191+
// Notification Center stacks alerts by thread so a chatty thread does
192+
// not bury the others; a grouped alert for several threads stays alone.
193+
"thread-id": notificationThreadId(input.notification),
185194
},
186195
environmentId: input.notification.environmentId,
187196
threadId: input.notification.threadId,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ describe("Android delivery routing", () => {
454454
).toMatchObject({
455455
alert_title: `${"T".repeat(117)}...`,
456456
alert_body: `Done: ${"P".repeat(111)}...`,
457+
alert_group: `${state.environmentId}/${state.threadId}`,
457458
});
458459
});
459460

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export function androidAlertForState(
6767
const notification = notificationForActivity({ ...state, status: statusForPhase(state.phase) });
6868
return {
6969
alert_id: JSON.stringify([state.environmentId, state.threadId, state.phase, state.updatedAt]),
70+
alert_group: `${state.environmentId}/${state.threadId}`,
7071
alert_title: notification.title,
7172
alert_body: notification.body,
7273
alert_path: notification.deepLink,
@@ -92,6 +93,7 @@ export function androidAlertForAggregate(input: {
9293
const notification = notificationForActivity(first);
9394
return {
9495
alert_id: JSON.stringify([first.environmentId, first.threadId, first.phase, first.updatedAt]),
96+
alert_group: `${first.environmentId}/${first.threadId}`,
9597
alert_title: notification.title,
9698
alert_body: notification.body,
9799
alert_path: notification.deepLink,

0 commit comments

Comments
 (0)