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) {
@@ -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"))
}
+56
View File
@@ -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:
+1
View File
@@ -30,6 +30,7 @@ environment:
dependencies:
flutter:
sdk: flutter
shared_preferences: ^2.0.10
# The following adds the Cupertino Icons font to your application.