import 'dart:io'; import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:get/get.dart'; import 'package:service/controller/local_notification.dart'; import '../../constant/box_name.dart'; import '../../main.dart'; import '../../print.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("service"); // أو "users" حسب نوع المستخدم print("Subscribed to 'service' 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'] ?? ''; // اقرأ العنوان (للعرض) String title = message.notification?.title ?? ''; String body = message.notification?.body ?? ''; if (category == 'new_service_request') { // <-- مثال: كان 'Order'.tr Log.print('message: ${message}'); if (Platform.isAndroid) { NotificationController().showNotification(title, body, 'Order'); } } } }