From 322217fc20560b60e0d656d9d1fa7517418f3f85 Mon Sep 17 00:00:00 2001 From: Henry-Hiles Date: Wed, 25 Nov 2026 15:14:52 -0500 Subject: [PATCH] try to dismiss notifications when read --- lib/controllers/notification.dart | 86 +++++++++++++++++++++++++++++++ lib/controllers/unified_push.dart | 5 +- 2 files changed, 90 insertions(+), 1 deletion(-) diff --git a/lib/controllers/notification.dart b/lib/controllers/notification.dart index fd3d0c1..eff818c 100644 --- a/lib/controllers/notification.dart +++ b/lib/controllers/notification.dart @@ -2,10 +2,14 @@ import "dart:async"; import "dart:convert"; import "dart:io"; +import "package:collection/collection.dart"; +import "package:fast_immutable_collections/fast_immutable_collections.dart"; import "package:flutter/services.dart"; import "package:material_ui/material_ui.dart"; import "package:nexus/controllers/active_account.dart"; import "package:nexus/controllers/portal.dart"; +import "package:nexus/controllers/rooms.dart"; +import "package:nexus/controllers/shared_prefs.dart"; import "package:nexus/controllers/sidebar_page.dart"; import "package:nexus/main.dart"; import "package:flutter_local_notifications/flutter_local_notifications.dart"; @@ -58,13 +62,92 @@ class NotificationController }); } + // Reading a room, here or on another client, dismisses its notifications + if (!isInBackground) { + ref.listen( + RoomsController.provider.select( + (rooms) => rooms + .where((_, room) => room.metadata?.unreadNotifications == 0) + .keys + .toISet(), + ), + (previous, read) { + final newlyRead = read.difference(previous ?? const ISetConst({})); + if (newlyRead.isEmpty) return; + + final accountId = ref.read(ActiveAccountController.provider); + dismiss(newlyRead.map((roomId) => threadIdFor(accountId, roomId))) + .onError(showError); + }, + fireImmediately: true, + ); + } + return notifications; } + static String threadIdFor(String accountId, String roomId) => + "$accountId/$roomId"; + static String payloadFor(String accountId, int rowId) => "$accountId:$rowId"; static const _androidGroup = "nexus.federated.nexus.MESSAGES"; static const _summaryPayload = "summary"; + static const _shownKey = "shownNotifications"; + + /// IDs of the notifications shown, by thread. Stored, since they're often + /// shown by a background instance. + Future>> _shown() async => IMap( + json.decode( + await ref.read(SharedPrefsController.provider).getString(_shownKey) ?? + "{}", + ), + ).map((threadId, ids) => .new(threadId, IList((ids as List).cast()))); + + Future _setShown(IMap> shown) => ref + .read(SharedPrefsController.provider) + .setString( + _shownKey, + json.encode( + shown.map((threadId, ids) => .new(threadId, ids.unlock)).unlock, + ), + ); + + Future _track(String threadId, int id) async { + final shown = await _shown(); + final ids = shown[threadId] ?? const IListConst([]); + if (!ids.contains(id)) await _setShown(shown.add(threadId, ids.add(id))); + } + + /// Dismisses the notifications shown for [threadIds]. + Future dismiss(Iterable threadIds) async { + final shown = await _shown(); + final ids = threadIds.map((threadId) => shown[threadId]).nonNulls.flattened; + if (ids.isEmpty) return; + + await _setShown( + shown.removeWhere((threadId, _) => threadIds.contains(threadId)), + ); + + if (Platform.isLinux) { + final portal = await ref.read(PortalController.provider.future); + await Future.wait( + ids.map((id) => portal.notification.removeNotification(id.toString())), + ); + return; + } + + final controller = await future; + await Future.wait(ids.map((id) => controller.cancel(id: id))); + + // The summary would otherwise outlive its group + if (Platform.isAndroid && + (await controller.getActiveNotifications()).every( + (notification) => notification.id == 0, + )) { + await controller.cancel(id: 0); + } + } void _open(String payload) { if (payload == _summaryPayload) { @@ -208,6 +291,7 @@ class NotificationController // ), ], ); + if (threadId != null) await _track(threadId, id); return; } @@ -293,6 +377,8 @@ class NotificationController ), ); + if (threadId != null) await _track(threadId, id); + if (Platform.isAndroid) { await controller.show( id: 0, diff --git a/lib/controllers/unified_push.dart b/lib/controllers/unified_push.dart index 25675e2..ec1e7e1 100644 --- a/lib/controllers/unified_push.dart +++ b/lib/controllers/unified_push.dart @@ -143,7 +143,10 @@ class UnifiedPushController extends AsyncNotifier { "$accountId/${Platform.isAndroid ? notification.roomId : notification.eventId}" .hashCode & 0x7fffffff, - threadId: "$accountId/${notification.roomId}", + threadId: NotificationController.threadIdFor( + accountId, + notification.roomId, + ), title: notification.roomName, subtitle: hasOtherAccounts ? notification.self.id : null, body: notification.text,