From 1270f3a6b58ebe35ec57863a57a12758e95e5a78 Mon Sep 17 00:00:00 2001 From: lizandrogd <77708265+lizandrogd@users.noreply.github.com> Date: Tue, 22 Aug 2023 23:51:31 -0500 Subject: [PATCH] uodate --- lib/main.dart | 49 ++++++++-------- lib/src/screens/service.dart | 4 ++ lib/src/services/firebase_messaging.dart | 21 +++++-- macos/Flutter/GeneratedPluginRegistrant.swift | 2 + pubspec.lock | 56 +++++++++++++++++++ pubspec.yaml | 1 + 6 files changed, 102 insertions(+), 31 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index baeea3e..e7c0955 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -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 _firebaseMessagingBackgroundHandler(RemoteMessage message, BuildContext context) async { - final Map 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}); diff --git a/lib/src/screens/service.dart b/lib/src/screens/service.dart index 404aeb2..eb31e6e 100644 --- a/lib/src/screens/service.dart +++ b/lib/src/screens/service.dart @@ -33,7 +33,9 @@ class ServiceScreen extends StatefulWidget { State createState() => _ServiceScreenState(); } + class _ServiceScreenState extends State { + void _saveToken() async { FirebaseMessaging messaging = FirebaseMessaging.instance; @@ -50,6 +52,7 @@ class _ServiceScreenState extends State { } + Future sendPushNotification(String pro) async { try { http.Response response = await http.post( @@ -203,6 +206,7 @@ class _ServiceScreenState extends State { @override void initState() { super.initState(); + if (_ciudad == '...') { AuthenticationRepository.instance diff --git a/lib/src/services/firebase_messaging.dart b/lib/src/services/firebase_messaging.dart index a590b62..fe6d259 100644 --- a/lib/src/services/firebase_messaging.dart +++ b/lib/src/services/firebase_messaging.dart @@ -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) { diff --git a/macos/Flutter/GeneratedPluginRegistrant.swift b/macos/Flutter/GeneratedPluginRegistrant.swift index b883437..3584be3 100644 --- a/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/macos/Flutter/GeneratedPluginRegistrant.swift @@ -16,6 +16,7 @@ import firebase_storage import flutter_local_notifications import geolocator_apple import path_provider_foundation +import shared_preferences_foundation import url_launcher_macos func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { @@ -30,5 +31,6 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { FlutterLocalNotificationsPlugin.register(with: registry.registrar(forPlugin: "FlutterLocalNotificationsPlugin")) GeolocatorPlugin.register(with: registry.registrar(forPlugin: "GeolocatorPlugin")) PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin")) + SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin")) UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin")) } diff --git a/pubspec.lock b/pubspec.lock index 8a48901..be977ab 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -960,6 +960,62 @@ packages: url: "https://pub.dev" source: hosted version: "2.0.0" + shared_preferences: + dependency: "direct main" + description: + name: shared_preferences + sha256: "0344316c947ffeb3a529eac929e1978fcd37c26be4e8468628bac399365a3ca1" + url: "https://pub.dev" + source: hosted + version: "2.2.0" + shared_preferences_android: + dependency: transitive + description: + name: shared_preferences_android + sha256: fe8401ec5b6dcd739a0fe9588802069e608c3fdbfd3c3c93e546cf2f90438076 + url: "https://pub.dev" + source: hosted + version: "2.2.0" + shared_preferences_foundation: + dependency: transitive + description: + name: shared_preferences_foundation + sha256: d29753996d8eb8f7619a1f13df6ce65e34bc107bef6330739ed76f18b22310ef + url: "https://pub.dev" + source: hosted + version: "2.3.3" + shared_preferences_linux: + dependency: transitive + description: + name: shared_preferences_linux + sha256: "71d6806d1449b0a9d4e85e0c7a917771e672a3d5dc61149cc9fac871115018e1" + url: "https://pub.dev" + source: hosted + version: "2.3.0" + shared_preferences_platform_interface: + dependency: transitive + description: + name: shared_preferences_platform_interface + sha256: "23b052f17a25b90ff2b61aad4cc962154da76fb62848a9ce088efe30d7c50ab1" + url: "https://pub.dev" + source: hosted + version: "2.3.0" + shared_preferences_web: + dependency: transitive + description: + name: shared_preferences_web + sha256: "7347b194fb0bbeb4058e6a4e87ee70350b6b2b90f8ac5f8bd5b3a01548f6d33a" + url: "https://pub.dev" + source: hosted + version: "2.2.0" + shared_preferences_windows: + dependency: transitive + description: + name: shared_preferences_windows + sha256: f95e6a43162bce43c9c3405f3eb6f39e5b5d11f65fab19196cf8225e2777624d + url: "https://pub.dev" + source: hosted + version: "2.3.0" simple_gesture_detector: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 4ff35aa..0e3bb9d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -30,6 +30,7 @@ environment: dependencies: flutter: sdk: flutter + shared_preferences: ^2.0.10 # The following adds the Cupertino Icons font to your application.