2026-05-31 19:04:48 +02:00
|
|
|
import 'package:firebase_messaging/firebase_messaging.dart';
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
|
import 'package:flux/core/routes/app_router.dart';
|
|
|
|
|
import 'package:go_router/go_router.dart';
|
|
|
|
|
|
|
|
|
|
// Chiamala dopo l'autenticazione o nel main()
|
|
|
|
|
Future<void> setupInteractedMessage() async {
|
|
|
|
|
// CASO A: L'app era completamente CHIUSA e viene aperta tappando la notifica
|
|
|
|
|
RemoteMessage? initialMessage = await FirebaseMessaging.instance
|
|
|
|
|
.getInitialMessage();
|
|
|
|
|
if (initialMessage != null) {
|
|
|
|
|
_handleNotificationTap(initialMessage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CASO B: L'app era in BACKGROUND (minimizzata) e l'utente tappa la notifica
|
|
|
|
|
FirebaseMessaging.onMessageOpenedApp.listen(_handleNotificationTap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _handleNotificationTap(RemoteMessage message) {
|
|
|
|
|
final eventType = message.data['eventType'];
|
|
|
|
|
final referenceId = message.data['referenceId'];
|
|
|
|
|
|
|
|
|
|
if (eventType == 'task_assigned' && referenceId != null) {
|
2026-06-01 10:08:44 +02:00
|
|
|
final routePath = '/tasks/form/$referenceId';
|
2026-05-31 19:04:48 +02:00
|
|
|
final context = AppRouter.rootNavigatorKey.currentContext;
|
2026-06-01 10:08:44 +02:00
|
|
|
|
2026-05-31 19:04:48 +02:00
|
|
|
if (context != null) {
|
2026-06-01 10:08:44 +02:00
|
|
|
// Scenario A: App già aperta, naviga all'istante
|
|
|
|
|
context.push(routePath);
|
2026-05-31 19:04:48 +02:00
|
|
|
} else {
|
2026-06-01 10:08:44 +02:00
|
|
|
// Scenario B: App chiusa. Il contesto non c'è ancora, congeliamo la rotta!
|
|
|
|
|
debugPrint("App in fase di avvio. Congelo la rotta: $routePath");
|
|
|
|
|
AppRouter.pendingRoute = routePath;
|
2026-05-31 19:04:48 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|