This commit is contained in:
lizandrogd
2023-08-22 23:51:31 -05:00
parent a22d7efc27
commit 1270f3a6b5
6 changed files with 102 additions and 31 deletions
+22 -27
View File
@@ -1,4 +1,4 @@
import 'package:animated_splash_screen/animated_splash_screen.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
@@ -40,7 +40,7 @@ void main() async {
options: DefaultFirebaseOptions.currentPlatform,
).then((value) => Get.put(AuthenticationRepository()));
FirebaseMessaging.onBackgroundMessage((message) => _firebaseMessagingBackgroundHandler(message, context)); // Pasa el contexto aquí
FirebaseMessaging.onBackgroundMessage((_firebaseMessagingBackgroundHandler));
FirebaseMessaging messaging = FirebaseMessaging.instance;
@@ -54,14 +54,27 @@ void main() async {
sound: true,
);
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('Got a message whilst in the foreground!');
print('Message data: ${message.data}');
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('Got a message whilst in the foreground!');
print('Message data: ${message.data}');
// Obtener el valor de la clave "screen" de los datos de la notificación
String? notificationScreen = message.data['screen'];
// Navegar a la pantalla correspondiente según el valor de la clave "screen"
if (notificationScreen == "misservicios") {
// Navegar a MyServicesScreen
Get.offNamed('/misservicios');
} else if (notificationScreen == "solicitud") {
// Navegar a SolicitudScreen
Get.offNamed('/solicitud');
}
if (message.notification != null) {
print('Message also contained a notification: ${message.notification}');
}
});
if (message.notification != null) {
print('Message also contained a notification: ${message.notification}');
}
});
print('User granted permission: ${settings.authorizationStatus}');
@@ -76,25 +89,7 @@ void main() async {
usePathUrlStrategy();
runApp(const MyApp());
}
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message, BuildContext context) async {
final Map<String, dynamic> data = message.data;
if (data['click_action'] == 'FLUTTER_NOTIFICATION_CLICK') {
final String screen = data['screen'];
if (screen == 'solicitud') {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SolicitudScreen()),
);
} else if (screen == 'misservicios') {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => MyServicesScreen()),
);
}
}
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
+4
View File
@@ -33,7 +33,9 @@ class ServiceScreen extends StatefulWidget {
State<ServiceScreen> createState() => _ServiceScreenState();
}
class _ServiceScreenState extends State<ServiceScreen> {
void _saveToken() async {
FirebaseMessaging messaging = FirebaseMessaging.instance;
@@ -50,6 +52,7 @@ class _ServiceScreenState extends State<ServiceScreen> {
}
Future<void> sendPushNotification(String pro) async {
try {
http.Response response = await http.post(
@@ -203,6 +206,7 @@ class _ServiceScreenState extends State<ServiceScreen> {
@override
void initState() {
super.initState();
if (_ciudad == '...') {
AuthenticationRepository.instance
+17 -4
View File
@@ -1,4 +1,6 @@
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:get/get.dart';
class FirebaseMessagingService {
FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance;
@@ -18,11 +20,22 @@ class FirebaseMessagingService {
String? token = await _firebaseMessaging.getToken();
print('Token FCM: $token');
// Escuchar los mensajes entrantes en la aplicación
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('Mensaje FCM recibido: ${message.notification?.title} - ${message.notification?.body}');
// Aquí puedes mostrar la notificación o procesar los datos recibidos según tus necesidades
});
print('Mensaje FCM recibido: ${message.notification?.title} - ${message.notification?.body}');
// Obtener el valor de la clave "screen" de los datos de la notificación
String? notificationScreen = message.data['screen'];
// Navegar a la pantalla correspondiente según el valor de la clave "screen"
if (notificationScreen == "misservicios") {
// Navegar a MyServicesScreen
Get.offNamed('/misservicios');
} else if (notificationScreen == "solicitud") {
// Navegar a SolicitudScreen
Get.offNamed('/solicitud');
}
});
// Manejar la notificación cuando se toca y la aplicación está en primer plano (opcional)
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {