import 'dart:convert'; import 'dart:io'; import 'package:Intaleq/views/home/HomePage/trip_monitor/trip_link_monitor.dart'; import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:Intaleq/controller/functions/toast.dart'; import 'package:Intaleq/views/widgets/elevated_btn.dart'; import '../../constant/box_name.dart'; import '../../constant/colors.dart'; import '../../constant/style.dart'; import '../../main.dart'; import '../../print.dart'; import '../../views/Rate/rate_captain.dart'; import '../../views/home/map_page_passenger.dart'; import '../../views/home/profile/promos_passenger_page.dart'; import '../auth/google_sign.dart'; import '../functions/audio_record1.dart'; import '../home/map_passenger_controller.dart'; import 'local_notification.dart'; class FirebaseMessagesController extends GetxController { final fcmToken = FirebaseMessaging.instance; List tokens = []; List dataTokens = []; late String driverID; late String driverToken; NotificationSettings? notificationSettings; Future getNotificationSettings() async { // Get the current notification settings NotificationSettings? notificationSettings = await FirebaseMessaging.instance.getNotificationSettings(); 'Notification authorization status: ${notificationSettings.authorizationStatus}'; // Call the update function if needed update(); } Future requestFirebaseMessagingPermission() async { FirebaseMessaging messaging = FirebaseMessaging.instance; // Check if the platform is Android if (Platform.isAndroid) { // Request permission for Android await messaging.requestPermission(); } else if (Platform.isIOS) { // Request permission for iOS NotificationSettings settings = await messaging.requestPermission( alert: true, announcement: true, badge: true, carPlay: true, criticalAlert: true, provisional: false, sound: true, ); messaging.setForegroundNotificationPresentationOptions( alert: true, badge: true, sound: true); } } NotificationController notificationController = Get.isRegistered() ? Get.find() : Get.put(NotificationController()); Future getToken() async { fcmToken.getToken().then((token) { Log.print('fcmToken: ${token}'); box.write(BoxName.tokenFCM, (token.toString())); }); // 🔹 الاشتراك في topic await fcmToken .subscribeToTopic("passengers"); // أو "users" حسب نوع المستخدم print("Subscribed to 'passengers' topic ✅"); FirebaseMessaging.onMessage.listen((RemoteMessage message) { // If the app is in the background or terminated, show a system tray message RemoteNotification? notification = message.notification; AndroidNotification? android = notification?.android; // if (notification != null && android != null) { if (message.data.isNotEmpty && message.notification != null) { fireBaseTitles(message); } }); FirebaseMessaging.onBackgroundMessage((RemoteMessage message) async { // Handle background message if (message.data.isNotEmpty && message.notification != null) { fireBaseTitles(message); } }); FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) { if (message.data.isNotEmpty && message.notification != null) { fireBaseTitles(message); } }); } Future fireBaseTitles(RemoteMessage message) async { // [!! تعديل !!] // اقرأ "النوع" من حمولة البيانات، وليس من العنوان String category = message.data['category'] ?? ''; final mapCtrl = Get.isRegistered() ? Get.find() : null; // اقرأ العنوان (للعرض) String title = message.notification?.title ?? ''; String body = message.notification?.body ?? ''; if (category == 'ORDER') { // <-- مثال: كان 'Order'.tr Log.print('message: ${message}'); if (Platform.isAndroid) { notificationController.showNotification(title, body, 'Order'); } } // ... داخل معالج الإشعارات في تطبيق الراكب ... else if (category == 'Accepted Ride') { if (mapCtrl != null) { Map? driverInfoMap; // 2. معالجة driver_info (تأتي كـ String JSON من PHP) if (message.data['driver_info'] != null) { try { String rawJson = message.data['driver_info']; // 🔥 فك التشفير: تحويل الـ String إلى Map driverInfoMap = jsonDecode(rawJson); } catch (e) { print("❌ Error decoding FCM driver_info: $e"); } } // 3. تمرير البيانات الجاهزة للكنترولر await mapCtrl.processRideAcceptance( driverData: driverInfoMap, source: "FCM", ); } } else if (category == 'Promo') { // <-- كان 'Promo'.tr if (Platform.isAndroid) { notificationController.showNotification(title, body, 'promo'); } Get.to(const PromosPassengerPage()); } else if (category == 'Trip Monitoring') { // <-- كان 'Trip Monitoring'.tr if (Platform.isAndroid) { notificationController.showNotification(title, body, 'iphone_ringtone'); } var myListString = message.data['passengerList']; var myList = jsonDecode(myListString) as List; Get.to(() => TripMonitor(), arguments: { 'rideId': myList[0].toString(), 'driverId': myList[1].toString(), }); } else if (category == 'token change') { // <-- كان 'token change'.tr if (Platform.isAndroid) { notificationController.showNotification(title, body, 'cancel'); } GoogleSignInHelper.signOut(); } else if (category == 'Driver Is Going To Passenger') { // <-- كان 'Driver Is Going To Passenger' Get.find().isDriverInPassengerWay = true; Get.find().update(); if (Platform.isAndroid) { notificationController.showNotification(title, body, 'tone1'); } } else if (category == 'MSG_FROM_PASSENGER') { // <-- كان 'message From passenger' if (Platform.isAndroid) { notificationController.showNotification(title, body, 'ding'); } passengerDialog(body); update(); } else if (category == 'message From Driver') { // <-- كان 'message From Driver' if (Platform.isAndroid) { notificationController.showNotification(title, body, 'ding'); } passengerDialog(body); update(); } else if (category == 'Trip is Begin') { // <-- كان 'Trip is Begin' Log.print('[FCM] استقبل إشعار "TRIP_BEGUN".'); // استدعاء الحارس mapCtrl!.processRideBegin(source: "FCM"); } else if (category == 'Hi ,I will go now') { // <-- كان 'Hi ,I will go now'.tr if (Platform.isAndroid) { notificationController.showNotification(title, body, 'ding'); } update(); } else if (category == "Arrive Ride") { // استدعاء الحارس mapCtrl!.processDriverArrival("FCM"); } else if (category == 'Driver Finish Trip') { List driverList = []; // ✅ معالجة آمنة للبيانات var rawData = message.data['DriverList']; if (rawData != null && rawData.isNotEmpty) { try { driverList = jsonDecode(rawData) as List; } catch (e) { print("❌ Error decoding DriverList: $e"); } } if (driverList.isNotEmpty) { Get.find() .processRideFinished(driverList, source: "FCM"); } } else if (category == 'Finish Monitor') { // <-- كان "Finish Monitor".tr Get.defaultDialog( titleStyle: AppStyle.title, title: 'Trip finished '.tr, middleText: '', confirm: MyElevatedButton( title: 'Ok'.tr, onPressed: () { Get.offAll(() => const MapPagePassenger()); })); } else if (category == 'Cancel Trip from driver') { Log.print("🔔 FCM: Ride Cancelled by Driver received."); // لا داعي لكتابة منطق التنظيف هنا، الكنترولر يتكفل بكل شيء if (Get.isRegistered()) { // استدعاء الحارس (سيتجاهل الأمر إذا كان السوكيت قد سبقه) Get.find() .processRideCancelledByDriver(message.data, source: "FCM"); } // إشعار محلي (اختياري، لأن الديالوج سيظهر) if (Platform.isAndroid) { notificationController.showNotification( 'Trip Cancelled'.tr, 'The driver cancelled the trip.'.tr, 'cancel'); } } // ... (باقي الحالات مثل Call Income, Call End, إلخ) ... // ... بنفس الطريقة ... else if (category == 'Order Applied') { if (Platform.isAndroid) { notificationController.showNotification( 'The order Accepted by another Driver'.tr, 'We regret to inform you that another driver has accepted this order.' .tr, 'order'); } } } // Future fireBaseTitles(RemoteMessage message) async { // if (message.notification!.title! == 'Order'.tr) { // Log.print('message: ${message}'); // if (Platform.isAndroid) { // notificationController.showNotification( // 'Order'.tr, message.notification!.body!, 'Order'); // } // } else // ... داخل معالج الإشعارات في تطبيق الراكب ... // if (message.notification!.title! == 'Accepted Ride') { // // ... // // انظر هنا: قمنا بتغيير "passengerList" إلى "driverList" // var driverListJson = message.data['driverList']; // // تأكد من أن البيانات ليست null قبل المتابعة // if (driverListJson != null) { // var myList = jsonDecode(driverListJson) as List; // Log.print('myList: ${myList}'); // final controller = Get.find(); // // استدعاء الدالة الموحدة الجديدة التي أنشأناها // await controller.processRideAcceptance( // driverIdFromFCM: myList[0].toString(), // rideIdFromFCM: myList[3].toString()); // } else { // Log.print( // '❌ خطأ فادح: إشعار "Accepted Ride" وصل بدون بيانات (driverList is null)'); // } // } else if (message.notification!.title! == 'Promo'.tr) { // if (Platform.isAndroid) { // notificationController.showNotification( // 'Promo', 'Show latest promo'.tr, 'promo'); // } // Get.to(const PromosPassengerPage()); // } else if (message.notification!.title! == 'Trip Monitoring'.tr) { // if (Platform.isAndroid) { // notificationController.showNotification( // 'Trip Monitoring'.tr, '', 'iphone_ringtone'); // } // var myListString = message.data['DriverList']; // var myList = jsonDecode(myListString) as List; // Get.toNamed('/tripmonitor', arguments: { // 'rideId': myList[0].toString(), // 'driverId': myList[1].toString(), // }); // } else if (message.notification!.title! == 'token change'.tr) { // if (Platform.isAndroid) { // notificationController.showNotification( // 'token change'.tr, 'token change'.tr, 'cancel'); // } // GoogleSignInHelper.signOut(); // } else if (message.notification!.title! == 'Driver Is Going To Passenger') { // Get.find().isDriverInPassengerWay = true; // Get.find().update(); // if (Platform.isAndroid) { // notificationController.showNotification('Driver is Going To You'.tr, // 'Please stay on the picked point.'.tr, 'tone1'); // } // // Get.snackbar('Driver is Going To Passenger', '', // // backgroundColor: AppColor.greenColor); // } else if (message.notification!.title! == 'message From passenger') { // if (Platform.isAndroid) { // notificationController.showNotification( // 'message From passenger'.tr, ''.tr, 'ding'); // } // passengerDialog(message.notification!.body!); // update(); // } else if (message.notification!.title! == 'message From Driver') { // if (Platform.isAndroid) { // notificationController.showNotification( // 'message From Driver'.tr, ''.tr, 'ding'); // } // passengerDialog(message.notification!.body!); // update(); // } else // (هذا الكود في معالج الإشعارات لديك) // if (message.notification!.title! == 'Trip is Begin') { // Log.print('[FCM] استقبل إشعار "Trip is Begin".'); // // (تم حذف الإشعار المحلي من هنا، نُقل إلى الدالة الموحدة) // final controller = Get.find(); // // استدعاء حارس البوابة الجديد والآمن // controller.processRideBegin(); // // (تم حذف كل الأوامر التالية من هنا) // // Get.find().getBeginRideFromDriver(); // // box.write(BoxName.passengerWalletTotal, '0'); // // update(); // } else if (message.notification!.title! == 'Hi ,I will go now'.tr) { // // Get.snackbar('Hi ,I will go now', '', // // backgroundColor: AppColor.greenColor); // if (Platform.isAndroid) { // notificationController.showNotification( // 'Passenger come to you'.tr, 'Hi ,I will go now'.tr, 'ding'); // } // update(); // } // ... داخل معالج الإشعارات (FCM Handler) ... // if (message.notification!.title! == 'Hi ,I Arrive your site'.tr) { // final controller = Get.find(); // // 1. التأكد أننا في الحالة الصحيحة (السائق كان في الطريق) // if (controller.currentRideState.value == RideState.driverApplied) { // Log.print('[FCM] السائق وصل. تغيير الحالة إلى driverArrived'); // // 2. تغيير الحالة فقط! // controller.currentRideState.value = RideState.driverArrived; // } // } else if (message.notification!.title! == "Cancel Trip from driver") { // Get.back(); // if (Platform.isAndroid) { // notificationController.showNotification("Cancel Trip from driver".tr, // "We will look for a new driver.\nPlease wait.".tr, 'cancel'); // } // Get.defaultDialog( // title: "The driver canceled your ride.".tr, // middleText: "We will look for a new driver.\nPlease wait.".tr, // confirm: MyElevatedButton( // kolor: AppColor.greenColor, // title: 'Ok'.tr, // onPressed: () async { // Get.back(); // await Get.find() // .reSearchAfterCanceledFromDriver(); // }, // ), // cancel: MyElevatedButton( // title: 'Cancel'.tr, // kolor: AppColor.redColor, // onPressed: () { // Get.offAll(() => const MapPagePassenger()); // }, // ) // // Get.find() // // .searchNewDriverAfterRejectingFromDriver(); // ); // } else if (message.notification!.title! == 'Driver Finish Trip'.tr) { // // الخطوة 1: استقبل البيانات وتحقق من وجودها // final rawData = message.data['DriverList']; // List driverList = []; // ابدأ بقائمة فارغة كإجراء وقائي // // الخطوة 2: قم بفك تشفير البيانات بأمان // if (rawData != null && rawData is String) { // try { // driverList = jsonDecode(rawData); // Log.print('Successfully decoded DriverList: $driverList'); // } catch (e) { // Log.print('Error decoding DriverList JSON: $e'); // // اترك القائمة فارغة في حالة حدوث خطأ // } // } else { // Log.print('Error: DriverList data is null or not a String.'); // } // // الخطوة 3: استخدم البيانات فقط إذا كانت القائمة تحتوي على العناصر المطلوبة // // هذا يمنع خطأ "RangeError" إذا كانت القائمة أقصر من المتوقع // if (driverList.length >= 4) { // if (Platform.isAndroid) { // notificationController.showNotification( // "Driver Finish Trip".tr, // '${'you will pay to Driver'.tr} ${driverList[3].toString()} \$', // تم تحسين طريقة عرض النص // 'tone1'); // } // Get.find().stopRecording(); // if ((double.tryParse( // box.read(BoxName.passengerWalletTotal).toString()) ?? // 0) < // 0) { // box.write(BoxName.passengerWalletTotal, 0); // } // Get.find().tripFinishedFromDriver(); // NotificationController().showNotification( // 'Don’t forget your personal belongings.'.tr, // 'Please make sure you have all your personal belongings and that any remaining fare, if applicable, has been added to your wallet before leaving. Thank you for choosing the Intaleq app' // .tr, // 'ding'); // Get.to(() => RateDriverFromPassenger(), arguments: { // 'driverId': driverList[0].toString(), // 'rideId': driverList[1].toString(), // 'price': driverList[3].toString() // }); // } else { // Log.print( // 'Error: Decoded driverList does not have enough elements. Received: $driverList'); // // هنا يمكنك عرض رسالة خطأ للمستخدم إذا لزم الأمر // } // } else if (message.notification!.title! == "Finish Monitor".tr) { // Get.defaultDialog( // titleStyle: AppStyle.title, // title: 'Trip finished '.tr, // middleText: '', // confirm: MyElevatedButton( // title: 'Ok'.tr, // onPressed: () { // Get.offAll(() => const MapPagePassenger()); // })); // } // // else if (message.notification!.title! == "Trip Monitoring".tr) { // // Get.to(() => const TripMonitor()); // // } // else if (message.notification!.title! == 'Call Income') { // try { // var myListString = message.data['DriverList']; // var driverList = jsonDecode(myListString) as List; // // if (Platform.isAndroid) { // if (Platform.isAndroid) { // notificationController.showNotification( // 'Call Income'.tr, // message.notification!.body!, // 'iphone_ringtone', // ); // } // // } // // Assuming GetMaterialApp is initialized and context is valid for navigation // // Get.to(() => PassengerCallPage( // // channelName: driverList[1].toString(), // // token: driverList[0].toString(), // // remoteID: driverList[2].toString(), // // )); // } catch (e) {} // } else if (message.notification!.title! == 'Call Income from Driver'.tr) { // try { // var myListString = message.data['DriverList']; // var driverList = jsonDecode(myListString) as List; // // if (Platform.isAndroid) { // if (Platform.isAndroid) { // notificationController.showNotification( // 'Call Income'.tr, // message.notification!.body!, // 'iphone_ringtone', // ); // } // // Assuming GetMaterialApp is initialized and context is valid for navigation // // Get.to(() => PassengerCallPage( // // channelName: driverList[1].toString(), // // token: driverList[0].toString(), // // remoteID: driverList[2].toString(), // // )); // } catch (e) {} // } else if (message.notification!.title! == 'Call End'.tr) { // try { // var myListString = message.data['DriverList']; // var driverList = jsonDecode(myListString) as List; // if (Platform.isAndroid) { // notificationController.showNotification( // 'Call End'.tr, // message.notification!.body!, // 'ding', // ); // } // // Assuming GetMaterialApp is initialized and context is valid for navigation // // Get.off(const CallPage()); // } catch (e) {} // } else if (message.notification!.title! == 'Driver Cancelled Your Trip') { // // Get.snackbar( // // 'You will be pay the cost to driver or we will get it from you on next trip' // // .tr, // // 'message', // // backgroundColor: AppColor.redColor); // if (Platform.isAndroid) { // notificationController.showNotification( // 'Driver Cancelled Your Trip'.tr, // 'you will pay to Driver you will be pay the cost of driver time look to your Intaleq Wallet' // .tr, // 'cancel'); // } // box.write(BoxName.parentTripSelected, false); // box.remove(BoxName.tokenParent); // Get.find().restCounter(); // Get.offAll(() => const MapPagePassenger()); // } // // else if (message.notification!.title! == 'Order Applied') { // // Get.snackbar( // // "The order has been accepted by another driver." // // .tr, // Corrected grammar // // "Be more mindful next time to avoid dropping orders." // // .tr, // Improved sentence structure // // backgroundColor: AppColor.yellowColor, // // snackPosition: SnackPosition.BOTTOM, // // ); // // } // else if (message.notification!.title! == 'Order Applied'.tr) { // if (Platform.isAndroid) { // notificationController.showNotification( // 'The order Accepted by another Driver'.tr, // 'We regret to inform you that another driver has accepted this order.' // .tr, // 'order'); // } // } // } SnackbarController driverAppliedTripSnakBar() { return Get.snackbar( 'Driver Applied the Ride for You'.tr, '', colorText: AppColor.greenColor, duration: const Duration(seconds: 3), snackPosition: SnackPosition.TOP, titleText: Text( 'Applied'.tr, style: const TextStyle(color: AppColor.redColor), ), messageText: Text( 'Driver Applied the Ride for You'.tr, style: AppStyle.title, ), icon: const Icon(Icons.approval), shouldIconPulse: true, margin: const EdgeInsets.all(16), padding: const EdgeInsets.all(16), ); } Future passengerDialog(String message) { return Get.defaultDialog( barrierDismissible: false, title: message.tr, titleStyle: AppStyle.title, middleTextStyle: AppStyle.title, middleText: message.tr, confirm: MyElevatedButton( title: 'Ok'.tr, onPressed: () { // Get.find().sendNotificationToPassengerToken( // 'Hi ,I will go now'.tr, // 'I will go now'.tr, // Get.find().driverToken, []); // Get.find() // .startTimerDriverWaitPassenger5Minute(); Get.back(); })); } Future driverFinishTripDialoge(List driverList) { return Get.defaultDialog( title: 'Driver Finish Trip'.tr, content: const DriverTipWidget(), confirm: MyElevatedButton( title: 'Yes'.tr, onPressed: () async { Get.to(() => RateDriverFromPassenger(), arguments: { 'driverId': driverList[0].toString(), 'rideId': driverList[1].toString(), 'price': driverList[3].toString() }); }, kolor: AppColor.greenColor, ), cancel: MyElevatedButton( title: 'No,I want'.tr, onPressed: () { Get.to(() => RateDriverFromPassenger(), arguments: { 'driverId': driverList[0].toString(), 'rideId': driverList[1].toString(), 'price': driverList[3].toString() }); }, kolor: AppColor.redColor, )); } } class DriverTipWidget extends StatelessWidget { const DriverTipWidget({ super.key, }); @override Widget build(BuildContext context) { return GetBuilder(builder: (controller) { return Column( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ // Text( // '${'Your fee is '.tr}${Get.find().totalPassenger.toStringAsFixed(2)}'), Text( 'Do you want to pay Tips for this Driver'.tr, textAlign: TextAlign.center, ), Row( mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ InkWell( onTap: () { box.write(BoxName.tipPercentage, '0.05'); Toast.show( context, '${'Tip is '.tr}${(controller.totalPassenger) * (double.parse(box.read(BoxName.tipPercentage.toString())))}', AppColor.blueColor); controller.update(); }, child: Container( decoration: BoxDecoration(border: Border.all()), child: const Padding( padding: EdgeInsets.all(4), child: Center( child: Text('5%'), ), ), ), ), InkWell( onTap: () { box.write(BoxName.tipPercentage, '0.10'); Toast.show( context, '${'Tip is'.tr} ${(controller.totalPassenger) * (double.parse(box.read(BoxName.tipPercentage.toString())))}', AppColor.blueColor); controller.update(); }, child: Container( decoration: BoxDecoration(border: Border.all()), child: const Center( child: Padding( padding: EdgeInsets.all(5), child: Text('10%'), ), ), ), ), InkWell( onTap: () { box.write(BoxName.tipPercentage, '0.15'); Toast.show( context, '${'Tip is'.tr} ${(controller.totalPassenger) * (double.parse(box.read(BoxName.tipPercentage.toString())))}', AppColor.blueColor); controller.update(); }, child: Container( decoration: BoxDecoration(border: Border.all()), child: const Center( child: Padding( padding: EdgeInsets.all(5), child: Text('15%'), ), ), ), ), InkWell( onTap: () { box.write(BoxName.tipPercentage, '0.20'); Toast.show( context, '${'Tip is'.tr} ${(controller.totalPassenger) * (double.parse(box.read(BoxName.tipPercentage.toString())))}', AppColor.blueColor); controller.update(); }, child: Container( decoration: BoxDecoration(border: Border.all()), child: const Center( child: Padding( padding: EdgeInsets.all(5), child: Text('20%'), ), ), ), ), ], ), Row( mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ MyElevatedButton( kolor: AppColor.redColor, title: 'No i want'.tr, onPressed: () { box.write(BoxName.tipPercentage, '0'); controller.update(); }), Container( decoration: AppStyle.boxDecoration1, child: Padding( padding: const EdgeInsets.all(6), child: Text( '${(controller.totalPassenger) * (double.parse(box.read(BoxName.tipPercentage.toString())))} ${box.read(BoxName.countryCode) == 'Egypt' ? 'LE'.tr : 'JOD'.tr}', style: AppStyle.title, ), ), ), ], ) ], ); }); } }