Throttle message notifications during sync

This commit is contained in:
Janez T
2026-05-10 17:48:13 +02:00
parent e1400cf1ec
commit 6062f91c2d
3 changed files with 86 additions and 15 deletions

View File

@@ -2567,8 +2567,11 @@ class AppProvider with ChangeNotifier {
debugPrint(
'🔄 [AppProvider] Performing initial message sync (fallback for missed pushes)',
);
final initialMessageCount = await connectionProvider.syncAllMessages(
force: true,
final initialMessageCount =
await messagesProvider.withReceivedNotificationsSuppressed(
() => connectionProvider.syncAllMessages(
force: true,
),
);
debugPrint(
'📥 [AppProvider] Initial sync retrieved $initialMessageCount message(s)',
@@ -2627,8 +2630,11 @@ class AppProvider with ChangeNotifier {
);
await refreshChannelLocationSharingState();
final messageCount = await connectionProvider.syncAllMessages(
force: true,
final messageCount =
await messagesProvider.withReceivedNotificationsSuppressed(
() => connectionProvider.syncAllMessages(
force: true,
),
);
debugPrint(
'📥 [AppProvider] Reconnect sync retrieved $messageCount message(s)',

View File

@@ -36,6 +36,7 @@ class MessagesProvider with ChangeNotifier {
bool _isInitialized = false;
bool _isPersisting = false;
bool _persistRequested = false;
bool _suppressReceivedNotifications = false;
AppLocalizations? _localizations;
final Map<String, MessageContactLocation> _messageContactLocations = {};
final Map<String, MessageReceptionDetails> _messageReceptionDetails = {};
@@ -850,11 +851,13 @@ class MessagesProvider with ChangeNotifier {
_sarMarkers[marker.id] = marker;
// Trigger urgent notification for received SAR messages (not sent by user)
if (!finalMessage.isSentMessage) {
if (!finalMessage.isSentMessage && !_suppressReceivedNotifications) {
_triggerSarNotification(finalMessage, marker);
}
}
} else if (!finalMessage.isSentMessage && !finalMessage.isSystemMessage) {
} else if (!finalMessage.isSentMessage &&
!finalMessage.isSystemMessage &&
!_suppressReceivedNotifications) {
// Trigger notification for regular messages (not SAR, not sent by user, not system)
_triggerMessageNotification(finalMessage);
}
@@ -865,6 +868,18 @@ class MessagesProvider with ChangeNotifier {
notifyListeners();
}
Future<T> withReceivedNotificationsSuppressed<T>(
Future<T> Function() action,
) async {
final previous = _suppressReceivedNotifications;
_suppressReceivedNotifications = true;
try {
return await action();
} finally {
_suppressReceivedNotifications = previous;
}
}
/// Check if a message is a duplicate
///
/// Messages are considered duplicates if they have: