import 'dart:async'; import 'package:flutter/foundation.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; class PushNotificationMessage { final String id; final String title; final String body; final String? type; final Map data; final DateTime timestamp; const PushNotificationMessage({ required this.id, required this.title, required this.body, this.type, this.data = const {}, required this.timestamp, }); factory PushNotificationMessage.fromMap(Map map) { return PushNotificationMessage( id: map['id']?.toString() ?? UniqueKey().toString(), title: map['title']?.toString() ?? 'Notification', body: map['body']?.toString() ?? '', type: map['type']?.toString(), data: map['data'] is Map ? map['data'] as Map : {}, timestamp: DateTime.now(), ); } } abstract class NotificationService { Future initialize(); Future getDeviceToken(); Stream get onMessageReceived; Future showInAppNotification(PushNotificationMessage message); void dispose(); } class InAppNotificationService implements NotificationService { final _messageController = StreamController.broadcast(); final List _history = []; bool _isInitialized = false; @override Future initialize() async { _isInitialized = true; debugPrint('[NotificationService] In-App Fallback Notification Service initialized.'); } @override Future getDeviceToken() async { // In-app fallback has no cloud device push token return null; } @override Stream get onMessageReceived => _messageController.stream; List get history => List.unmodifiable(_history); @override Future showInAppNotification(PushNotificationMessage message) async { if (!_isInitialized) { await initialize(); } _history.insert(0, message); if (_history.length > 50) { _history.removeLast(); } _messageController.add(message); debugPrint('[NotificationService] In-app notification received: ${message.title} - ${message.body}'); } @override void dispose() { _messageController.close(); } } final notificationServiceProvider = Provider((ref) { final service = InAppNotificationService(); ref.onDispose(() => service.dispose()); return service; });