Primer MVP
This commit is contained in:
@@ -24,7 +24,7 @@ import 'dart:async';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
// Descomenta cuando Firebase esté configurado:
|
||||
// import 'package:firebase_messaging/firebase_messaging.dart';
|
||||
import 'package:firebase_messaging/firebase_messaging.dart';
|
||||
import '../services/api_service.dart';
|
||||
|
||||
class HomeScreen extends StatefulWidget {
|
||||
@@ -89,7 +89,7 @@ class _HomeScreenState extends State<HomeScreen>
|
||||
_usuarioId = args;
|
||||
_cargarETA();
|
||||
_iniciarAutoRefresh();
|
||||
// _registrarFCMToken(); // Activar cuando Firebase esté listo
|
||||
_registrarFCMToken(); // Activar cuando Firebase esté listo
|
||||
} else {
|
||||
// Fallback: leer de shared_preferences si no viene por argumento
|
||||
_cargarUsuarioDeStorage();
|
||||
@@ -179,43 +179,41 @@ class _HomeScreenState extends State<HomeScreen>
|
||||
// manda al backend para poder recibir notificaciones push.
|
||||
// DESCOMENTA cuando tengas Firebase configurado.
|
||||
// ----------------------------------------------------------------
|
||||
// Future<void> _registrarFCMToken() async {
|
||||
// try {
|
||||
// final messaging = FirebaseMessaging.instance;
|
||||
//
|
||||
// // Pedir permisos de notificación al usuario (iOS requiere esto)
|
||||
// final settings = await messaging.requestPermission(
|
||||
// alert: true,
|
||||
// sound: true,
|
||||
// badge: true,
|
||||
// );
|
||||
//
|
||||
// if (settings.authorizationStatus == AuthorizationStatus.authorized) {
|
||||
// final token = await messaging.getToken();
|
||||
// if (token != null && _usuarioId != null) {
|
||||
// await _apiService.registrarFcmToken(_usuarioId!, token);
|
||||
// debugPrint('✅ FCM Token registrado: ${token.substring(0, 20)}...');
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // Escuchar notificaciones cuando la app está en FOREGROUND
|
||||
// FirebaseMessaging.onMessage.listen((RemoteMessage message) {
|
||||
// if (message.notification != null && mounted) {
|
||||
// ScaffoldMessenger.of(context).showSnackBar(
|
||||
// SnackBar(
|
||||
// content: Text('🚛 ${message.notification!.body}'),
|
||||
// backgroundColor: Colors.green.shade700,
|
||||
// duration: const Duration(seconds: 5),
|
||||
// ),
|
||||
// );
|
||||
// // Refrescar ETA al recibir notificación
|
||||
// _cargarETA();
|
||||
// }
|
||||
// });
|
||||
// } catch (e) {
|
||||
// debugPrint('Error registrando FCM token: $e');
|
||||
// }
|
||||
// }
|
||||
Future<void> _registrarFCMToken() async {
|
||||
try {
|
||||
final messaging = FirebaseMessaging.instance;
|
||||
|
||||
// Pedir permisos de notificación al usuario (iOS requiere esto)
|
||||
final settings = await messaging.requestPermission(
|
||||
alert: true,
|
||||
sound: true,
|
||||
badge: true,
|
||||
);
|
||||
if (settings.authorizationStatus == AuthorizationStatus.authorized) {
|
||||
final token = await messaging.getToken();
|
||||
if (token != null && _usuarioId != null) {
|
||||
await _apiService.registrarFcmToken(_usuarioId!, token);
|
||||
debugPrint('✅ FCM Token registrado: ${token.substring(0, 20)}...');
|
||||
}
|
||||
}
|
||||
// Escuchar notificaciones cuando la app está en FOREGROUND
|
||||
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
|
||||
if (message.notification != null && mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('🚛 ${message.notification!.body}'),
|
||||
backgroundColor: Colors.green.shade700,
|
||||
duration: const Duration(seconds: 5),
|
||||
),
|
||||
);
|
||||
// Refrescar ETA al recibir notificación
|
||||
_cargarETA();
|
||||
}
|
||||
});
|
||||
} catch (e) {
|
||||
debugPrint('Error registrando FCM token: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
// CERRAR SESIÓN
|
||||
|
||||
Reference in New Issue
Block a user