import 'dart:async'; import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import '../state/app/modules/chat/bloc/chat_bloc.dart'; import '../widget/debug/debug_tile.dart'; import '../widget/debug/json_viewer.dart'; import '../widget/info_dialog.dart'; import 'notification_tasks.dart'; // `vm:entry-point` on the class so AOT tree-shaking doesn't drop it: the // FCM background handler runs in a fresh isolate that looks up the class // by name from native code. @pragma('vm:entry-point') class NotificationController { // Notification display is handled by the Firebase SDK using server-generated payloads. @pragma('vm:entry-point') static Future onBackgroundMessageHandler(RemoteMessage message) async { NotificationTasks.updateBadgeCount(message); } static Future onForegroundMessageHandler( RemoteMessage message, BuildContext context, ) async { final pushToken = _extractChatToken(message); final activeToken = context.read().state.data?.currentToken ?? ''; final chatIsOpen = pushToken != null && pushToken.isNotEmpty && pushToken == activeToken; NotificationTasks.updateBadgeCount(message); if (chatIsOpen) { // Long-poll already fetches the message and moves the marker; just // dismiss any tray entry that slipped through foreground rendering. unawaited(NotificationTasks.clearNotificationsForChat(pushToken)); return; } NotificationTasks.updateProviders(context); } static Future onAppOpenedByNotification( RemoteMessage message, BuildContext context, ) async { NotificationTasks.navigateToTalk( context, chatToken: _extractChatToken(message), ); NotificationTasks.updateProviders(context); DebugTile(context).run(() { InfoDialog.show( context, 'Dieser Bericht wird angezeigt, da du den Entwicklermodus aktiviert hast und die App über eine Benachrichtigung geöffnet wurde.\n\n' '${JsonViewer.format(message.data)}', copyable: true, title: 'Notification report', ); }); } static String? _extractChatToken(RemoteMessage message) { for (final key in const ['chatToken', 'token', 'roomToken']) { final value = message.data[key]; if (value is String && value.isNotEmpty) return value; } return null; } }