import 'dart:convert'; import 'dart:io'; import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:http/http.dart' as http; import '../../constant/api_key.dart'; import '../../constant/box_name.dart'; import '../../constant/colors.dart'; import '../../constant/links.dart'; import '../../constant/style.dart'; import '../../main.dart'; import 'functions/crud.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); } } Future getTokens() async { var res = await CRUD().post(link: AppLink.getTokens, payload: {}); if (res != 'failure' && res['status'] == 'success') { dataTokens = res['data']; for (var i = 0; i < dataTokens.length; i++) { tokens.add(res['data'][i]['token']); } box.write(BoxName.tokens, tokens); } else { Get.snackbar("Warning".tr, "Server Error".tr, backgroundColor: AppColor.redColor); } } Future getToken() async { fcmToken.getToken().then((token) { box.write(BoxName.tokenFCM, token); }); 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); } }); } void fireBaseTitles(RemoteMessage message) { if (message.notification!.title! == 'Order'.tr) { } else if (message.notification!.title! == 'Apply Ride'.tr) { var passengerList = message.data['passengerList']; var myList = jsonDecode(passengerList) as List; driverID = myList[0].toString(); } } }