onNotificationReceived property

Stream<NotificationPayload> get onNotificationReceived

Stream of received notifications from both Firebase and Local sources. Automatically deduplicates notifications based on ID and timestamp.

Implementation

Stream<NotificationPayload> get onNotificationReceived {
  final Set<String> seenNotifications = <String>{};

  return StreamGroup.merge(<Stream<NotificationPayload>>[
    _firebaseRepository.onNotificationReceived.map(
      (NotificationPayload payload) => payload.copyWith(
        data: <String, dynamic>{...payload.data, 'source': 'firebase'},
      ),
    ),
    _localRepository.onNotificationReceived
        .where((NotificationPayload payload) {
          // Skip notifications that were converted from Firebase
          final bool isConverted = payload.data['convertedToLocal'] == true;
          return !isConverted;
        })
        .map(
          (NotificationPayload payload) => payload.copyWith(
            data: <String, dynamic>{...payload.data, 'source': 'local'},
          ),
        ),
  ]).where((NotificationPayload payload) {
    // Create a unique key for deduplication
    final String key =
        '${payload.id}_${payload.timestamp?.millisecondsSinceEpoch ?? ""}';

    if (seenNotifications.contains(key)) {
      safeDebugLog('Duplicate notification filtered: ${payload.title}');
      return false;
    }

    seenNotifications.add(key);
    return true;
  });
}