Phase 3 testing fixes: Fastify 5, SSE→WebSocket+FCM, enums, security, session lifecycle
- Upgrade Fastify 4→5 with all plugins (@fastify/websocket 11, cors 11, sensible 6) - Migrate all SSE endpoints to WebSocket + FCM push (mitra chat requests, customer pairing status) - Add flutter_local_notifications for foreground push notifications with sound - Add splash screen to both apps (hide auth loading flash) - Introduce constants/enums across entire codebase (no raw string literals) - Move price tiers from hardcoded array to app_config DB (data-driven, includes 1-min test tier) - Add session ownership validation on all shared chat routes - Add ownership checks on endSession, respondToExtension, requestExtension - Fix session timer: auto-complete expired/stale sessions on server restart - Add 5-min grace period for abandoned closing sessions - Fix extension flow: proper session_resumed handling, clearExtensionRequest, closure grace timer cleanup - Fix chat screens: ConnectChat in initState, session status check on connect - Fix customer expired view: 5-min countdown, closure state priority over expired state - Fix mitra extension UI: loading spinner, disable buttons, handle EXTENSION_RESOLVED error - Fix GoRouter navigation consistency (no more Navigator.pushNamed) - Fix goodbye view keyboard overflow (SingleChildScrollView) - Add active session card on customer home screen with refresh on navigate back - Fix PricingBottomSheet extension mode (RequestExtension instead of new pairing) - Send session_resumed to both parties on extension accept Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
95
mitra_app/lib/core/notifications/notification_service.dart
Normal file
95
mitra_app/lib/core/notifications/notification_service.dart
Normal file
@@ -0,0 +1,95 @@
|
||||
import 'dart:convert';
|
||||
import 'package:firebase_messaging/firebase_messaging.dart';
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
/// Handles FCM foreground/background notifications and local notification display.
|
||||
class NotificationService {
|
||||
static final _localNotifications = FlutterLocalNotificationsPlugin();
|
||||
static GoRouter? _router;
|
||||
|
||||
static const _channel = AndroidNotificationChannel(
|
||||
'chat_messages',
|
||||
'Chat Messages',
|
||||
description: 'Notifications for incoming chat messages',
|
||||
importance: Importance.high,
|
||||
);
|
||||
|
||||
static Future<void> initialize(GoRouter router) async {
|
||||
_router = router;
|
||||
|
||||
// Create Android notification channel
|
||||
await _localNotifications
|
||||
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
|
||||
?.createNotificationChannel(_channel);
|
||||
|
||||
// Initialize local notifications
|
||||
await _localNotifications.initialize(
|
||||
settings: const InitializationSettings(
|
||||
android: AndroidInitializationSettings('@mipmap/ic_launcher'),
|
||||
iOS: DarwinInitializationSettings(),
|
||||
),
|
||||
onDidReceiveNotificationResponse: _onNotificationTap,
|
||||
);
|
||||
|
||||
// FCM foreground messages → show local notification
|
||||
FirebaseMessaging.onMessage.listen(_onForegroundMessage);
|
||||
|
||||
// FCM notification tap (app was in background)
|
||||
FirebaseMessaging.onMessageOpenedApp.listen(_onMessageOpenedApp);
|
||||
|
||||
// Check if app was opened from a terminated state via notification
|
||||
final initialMessage = await FirebaseMessaging.instance.getInitialMessage();
|
||||
if (initialMessage != null) {
|
||||
_navigateFromMessage(initialMessage.data);
|
||||
}
|
||||
}
|
||||
|
||||
static void _onForegroundMessage(RemoteMessage message) {
|
||||
final notification = message.notification;
|
||||
if (notification == null) return;
|
||||
|
||||
_localNotifications.show(
|
||||
id: notification.hashCode,
|
||||
title: notification.title,
|
||||
body: notification.body,
|
||||
notificationDetails: NotificationDetails(
|
||||
android: AndroidNotificationDetails(
|
||||
_channel.id,
|
||||
_channel.name,
|
||||
channelDescription: _channel.description,
|
||||
importance: Importance.high,
|
||||
priority: Priority.high,
|
||||
),
|
||||
iOS: const DarwinNotificationDetails(
|
||||
presentAlert: true,
|
||||
presentBadge: true,
|
||||
presentSound: true,
|
||||
),
|
||||
),
|
||||
payload: jsonEncode(message.data),
|
||||
);
|
||||
}
|
||||
|
||||
static void _onNotificationTap(NotificationResponse response) {
|
||||
if (response.payload == null) return;
|
||||
try {
|
||||
final data = jsonDecode(response.payload!) as Map<String, dynamic>;
|
||||
_navigateFromMessage(data);
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
static void _onMessageOpenedApp(RemoteMessage message) {
|
||||
_navigateFromMessage(message.data);
|
||||
}
|
||||
|
||||
static void _navigateFromMessage(Map<String, dynamic> data) {
|
||||
final sessionId = data['session_id'] as String?;
|
||||
if (sessionId == null || _router == null) return;
|
||||
|
||||
final type = data['type'] as String?;
|
||||
if (type == 'chat_message' || type == 'chat_request') {
|
||||
_router!.push('/chat/session/$sessionId');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user