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` keeps this alive through AOT tree-shaking — the FCM // background isolate looks the class up by name from native code. @pragma('vm:entry-point') class NotificationController { @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 chatBloc = context.read(); // hasOpenChat, not currentToken: currentToken sticks around after // leaveChat so didPopNext can re-claim a stacked chat. final activeToken = chatBloc.state.data?.currentToken ?? ''; final chatIsOpen = chatBloc.hasOpenChat && pushToken != null && pushToken.isNotEmpty && pushToken == activeToken; NotificationTasks.updateBadgeCount(message); if (chatIsOpen) { // Long-poll handles the message; just dismiss any stray tray entry. 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; } }