37 lines
1.4 KiB
Dart
37 lines
1.4 KiB
Dart
|
|
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) {
|
||
|
|
// Verifichiamo che tipo di notifica è e prendiamo l'ID
|
||
|
|
final eventType = message.data['eventType'];
|
||
|
|
final referenceId = message.data['referenceId'];
|
||
|
|
|
||
|
|
if (eventType == 'task_assigned' && referenceId != null) {
|
||
|
|
// Navighiamo verso il form del Task usando la GlobalKey!
|
||
|
|
// Assicuriamoci che il context sia disponibile
|
||
|
|
final context = AppRouter.rootNavigatorKey.currentContext;
|
||
|
|
if (context != null) {
|
||
|
|
// Usiamo .push perché è una rotta di dettaglio fuori dalla shell
|
||
|
|
// Il path è /tasks/form/:id (vedi il tuo AppRouter)
|
||
|
|
context.push('/tasks/form/$referenceId');
|
||
|
|
} else {
|
||
|
|
debugPrint("Attenzione: Context non trovato per il Deep Link!");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|