import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:votianlt_app/services/developer.dart' as developer; class NotificationService { NotificationService._internal(); static final NotificationService _instance = NotificationService._internal(); factory NotificationService() => _instance; final FlutterLocalNotificationsPlugin _plugin = FlutterLocalNotificationsPlugin(); bool _initialized = false; /// The conversation key of the chat currently being viewed by the user. /// When set, incoming chat notifications for this conversation are suppressed. String? activeConversationKey; static const String _chatChannelId = 'chat_messages'; static const String _chatChannelName = 'Chat-Nachrichten'; static const String _chatChannelDescription = 'Benachrichtigungen bei neuen Chat-Nachrichten'; static const String _jobChannelId = 'new_jobs'; static const String _jobChannelName = 'Neue Jobs'; static const String _jobChannelDescription = 'Benachrichtigungen bei neuen Job-Zuweisungen'; int _nextId = 0; Future initialize() async { if (_initialized) return; const androidSettings = AndroidInitializationSettings( '@mipmap/ic_launcher', ); const iosSettings = DarwinInitializationSettings( requestAlertPermission: true, requestBadgePermission: true, requestSoundPermission: true, ); const initSettings = InitializationSettings( android: androidSettings, iOS: iosSettings, ); await _plugin.initialize(initSettings); await _plugin .resolvePlatformSpecificImplementation< AndroidFlutterLocalNotificationsPlugin>() ?.requestNotificationsPermission(); _initialized = true; developer.log('NotificationService initialized', name: 'NotificationService'); } Future showChatNotification({ required String title, required String body, required String conversationKey, }) async { if (!_initialized) return; if (activeConversationKey == conversationKey) return; const androidDetails = AndroidNotificationDetails( _chatChannelId, _chatChannelName, channelDescription: _chatChannelDescription, importance: Importance.high, priority: Priority.high, playSound: true, enableVibration: true, ); const iosDetails = DarwinNotificationDetails( presentAlert: true, presentBadge: true, presentSound: true, ); const details = NotificationDetails( android: androidDetails, iOS: iosDetails, ); await _plugin.show(_nextId++, title, body, details, payload: 'chat:$conversationKey'); } Future showJobNotification({ required String title, required String body, }) async { if (!_initialized) return; const androidDetails = AndroidNotificationDetails( _jobChannelId, _jobChannelName, channelDescription: _jobChannelDescription, importance: Importance.high, priority: Priority.high, playSound: true, enableVibration: true, ); const iosDetails = DarwinNotificationDetails( presentAlert: true, presentBadge: true, presentSound: true, ); const details = NotificationDetails( android: androidDetails, iOS: iosDetails, ); await _plugin.show(_nextId++, title, body, details, payload: 'job'); } }