This commit is contained in:
Hamza-Ayed
2024-11-10 18:38:07 +02:00
parent c9ac8da2ec
commit e8c72d79a9
25 changed files with 1211 additions and 329 deletions

View File

@@ -33,7 +33,8 @@ class FirebaseMessagesController extends GetxController {
late String driverID;
late String driverToken;
NotificationSettings? notificationSettings;
NotificationController notificationController =
Get.put(NotificationController());
Future<void> getNotificationSettings() async {
// Get the current notification settings
NotificationSettings? notificationSettings =
@@ -78,34 +79,15 @@ class FirebaseMessagesController extends GetxController {
RemoteNotification? notification = message.notification;
AndroidNotification? android = notification?.android;
// if (notification != null && android != null) {
if (message.data.isNotEmpty && message.notification != null) {
fireBaseTitles(message);
}
if (message.data.isNotEmpty && message.notification != null) {
fireBaseTitles(message);
}
});
FirebaseMessaging.onBackgroundMessage((RemoteMessage message) async {
// if (message.notification!.title! == 'Order'.tr) {
// if (Platform.isAndroid) {
// NotificationController1()
// .showNotification('Order'.tr, '', 'order', 'order_page_payload');
// }
// // await FirebaseMessagesController().showOverlayNotification(message);
// var myListString = message.data['DriverList'];
// // var points = message.data['PolylineJson'];
// var myList = jsonDecode(myListString) as List<dynamic>;
// // var myPoints = jsonDecode(points) as List<dynamic>;
// driverToken = myList[14].toString();
// // This is for location using and uploading status
// Get.put(HomeCaptainController()).changeRideId();
// update();
// Get.to(() => OrderRequestPage(), arguments: {
// 'myListString': myListString,
// 'DriverList': myList,
// // 'PolylineJson': myPoints,
// 'body': message.notification!.body
// });
// }
});
FirebaseMessaging.onBackgroundMessage((RemoteMessage message) async {});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
if (message.data.isNotEmpty && message.notification != null) {
@@ -117,7 +99,7 @@ class FirebaseMessagesController extends GetxController {
Future<void> fireBaseTitles(RemoteMessage message) async {
if (message.notification!.title! == 'OrderSpeed'.tr) {
if (Platform.isAndroid) {
NotificationController().showNotification(
notificationController.showNotification(
message.notification!.title.toString(),
message.notification!.body.toString(),
'order',
@@ -139,9 +121,25 @@ class FirebaseMessagesController extends GetxController {
// 'PolylineJson': myPoints,
'body': message.notification!.body
});
} else if (message.notification!.title == 'Cancel Trip') {
} else if (message.notification!.title == 'OrderVIP') {
var myListString = message.data['DriverList'];
var myList = jsonDecode(myListString) as List<dynamic>;
// driverToken = myList[10].toString();
if (Platform.isAndroid) {
NotificationController1().showNotification(
notificationController.showNotification(
'OrderVIP'.tr, 'OrderVIP'.tr, 'order', '');
}
Get.to(VipOrderPage(), arguments: {
'myListString': myListString,
'DriverList': myList,
// 'PolylineJson': myPoints,
'body': message.notification!.body
});
} else if (message.notification!.title == 'Cancel Trip'.tr) {
if (Platform.isAndroid) {
notificationController.showNotification(
'Cancel Trip'.tr, 'Passenger Cancel Trip'.tr, 'cancel', '');
}
cancelTripDialog();
@@ -149,8 +147,8 @@ class FirebaseMessagesController extends GetxController {
var myListString = message.data['DriverList'];
var driverList = jsonDecode(myListString) as List<dynamic>;
if (Platform.isAndroid) {
NotificationController1()
.showNotification('VIP Order'.tr, '', 'order', '');
notificationController.showNotification(
'VIP Order'.tr, '', 'order', '');
}
MyDialog().getDialog('VIP Order'.tr, 'midTitle', () {
sendNotificationToPassengerToken(
@@ -162,26 +160,19 @@ class FirebaseMessagesController extends GetxController {
});
// Get.to(const VipOrderPage());
} else if (message.notification!.title! == 'message From passenger'.tr) {
// passengerDialog(message.notification!.body!);
} else if (message.notification!.title == 'message From passenger') {
if (Platform.isAndroid) {
NotificationController()
.showNotification('message From passenger'.tr, ''.tr, 'ding', '');
notificationController.showNotification(
'message From passenger'.tr, ''.tr, 'ding', '');
}
MyDialog().getDialog(
'message From passenger'.tr, message.notification!.body!, () {
// FirebaseMessagesController().sendNotificationToPassengerToken(
// 'Hi ,I will go now'.tr,
// 'I will go now'.tr,
// Get.find<MapPassengerController>().driverToken, []);
// Get.find<MapPassengerController>()
// .startTimerDriverWaitPassenger5Minute();
Get.back();
});
} else if (message.notification!.title == 'Cancel') {
if (Platform.isAndroid) {
NotificationController()
.showNotification('Cancel'.tr, ''.tr, 'cancel', '');
notificationController.showNotification(
'Cancel'.tr, ''.tr, 'cancel', '');
}
MyDialog().getDialog(
'Passenger Cancel Trip'.tr,
@@ -192,14 +183,14 @@ class FirebaseMessagesController extends GetxController {
});
// cancelTripDialog1();
} else if (message.notification!.title! == 'token change') {
// NotificationController1()
// notificationController
// .showNotification('token change'.tr, 'token change', 'cancel');
// GoogleSignInHelper.signOut();
GoogleSignInHelper.signOut();
} else if (message.notification!.title! == 'face detect') {
if (Platform.isAndroid) {
NotificationController()
.showNotification('face detect'.tr, ''.tr, 'tone2', '');
notificationController.showNotification(
'face detect'.tr, ''.tr, 'tone2', '');
}
String result0 = await faceDetector();
// Handle the result here, e.g., show a dialog or update the UI
@@ -220,7 +211,7 @@ class FirebaseMessagesController extends GetxController {
// Get.snackbar('Hi ,I will go now', '',
// backgroundColor: AppColor.greenColor);
if (Platform.isAndroid) {
NotificationController1().showNotification(
notificationController.showNotification(
'Passenger come to you'.tr, 'Hi ,I will go now'.tr, 'tone2', '');
}
update();
@@ -230,7 +221,7 @@ class FirebaseMessagesController extends GetxController {
var driverList = jsonDecode(myListString) as List<dynamic>;
// if (Platform.isAndroid) {
if (Platform.isAndroid) {
NotificationController1().showNotification('Call Income'.tr,
notificationController.showNotification('Call Income'.tr,
message.notification!.body!, 'iphone_ringtone', '');
}
// }
@@ -248,7 +239,7 @@ class FirebaseMessagesController extends GetxController {
var driverList = jsonDecode(myListString) as List<dynamic>;
// if (Platform.isAndroid) {
if (Platform.isAndroid) {
NotificationController1().showNotification('Call Income'.tr,
notificationController.showNotification('Call Income'.tr,
message.notification!.body!, 'iphone_ringtone', '');
}
// }
@@ -262,11 +253,8 @@ class FirebaseMessagesController extends GetxController {
} else if (message.notification!.title! ==
"Criminal Document Required".tr) {
if (Platform.isAndroid) {
NotificationController1().showNotification(
"Criminal Document Required".tr,
message.notification!.body!,
'tone2',
'');
notificationController.showNotification("Criminal Document Required".tr,
message.notification!.body!, 'tone2', '');
}
MyDialog().getDialog(
"Criminal Document Required".tr, 'You should have upload it .'.tr,
@@ -279,7 +267,7 @@ class FirebaseMessagesController extends GetxController {
var myListString = message.data['passengerList'];
var driverList = jsonDecode(myListString) as List<dynamic>;
if (Platform.isAndroid) {
NotificationController1().showNotification(
notificationController.showNotification(
'Call End'.tr, message.notification!.body!, 'tone2', '');
}
// Assuming GetMaterialApp is initialized and context is valid for navigation
@@ -296,7 +284,7 @@ class FirebaseMessagesController extends GetxController {
);
} else if (message.notification!.title! == 'Order') {
if (Platform.isAndroid) {
NotificationController().showNotification(
notificationController.showNotification(
message.notification!.title.toString(),
message.notification!.body.toString(),
'order',
@@ -318,7 +306,7 @@ class FirebaseMessagesController extends GetxController {
});
} else if (message.notification!.title! == 'Order Applied'.tr) {
if (Platform.isAndroid) {
NotificationController1().showNotification(
notificationController.showNotification(
'The order Accepted by another Driver'.tr,
'We regret to inform you that another driver has accepted this order.'
.tr,
@@ -761,98 +749,6 @@ class FirebaseMessagesController extends GetxController {
}
}
void sendNotificationToAnyWithoutData(
String title, String body, String token, String tone,
{int retryCount = 2}) async {
try {
String serviceAccountKeyJson = '''{
"type": "service_account",
"project_id": "ride-b1bd8",
"private_key_id": "75e817c0b902db2ef35edf2c2bd159dec1f13249",
"private_key": "-----BEGIN PRIVATE KEY-----\\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQD0zH9TQGDQHUv3\\na3/JAD1UKPwAp3wNKT0a6fxiIzjI3JxQWI30QvZCcfl6CdMhIcydX1ncSaYTcEeC\\n/AdPVCPkqyJx1YIGGg6P/mRzCWeaN8fsp6z250m5vcObDCZc3dbJEkepbep+6FPY\\n21m3KO+AHh1glgsTGZOTm5xiU8NGXpdk2QEh8wpiIIlR/HuKwVw9g8urNe3Sno+U\\nDm3z37iFqvZdmpqO8aWTJu6beb3hsREK9XK2I9JqC2JUwiGQRo3idOvPP6hkqrWx\\nKSX96vglQFYfakvJdDp2ZATOlpBYPMtS/IWhJ985u58TSS+Kl8qpnpaZBSxgJirf\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FmQT8McFXmGxZ5belsAsdetSGW9icbLUerTGQ0CgYEAmf/G8Ag3XxmqTXvvHuv2\\n14OP7WnrVqkEMnydrftEwn4peXd/Lz+/GYX5Zc4ZoNgbN8IvZ5z0+OmRsallsbiW\\nBw0/tc68CjzxXOvReWxDluUopqWVGj5tlGqE5xUDku9SWJSxbkiQ3rqutzBdPXpr\\noqHwPyDrmK/Zgqn+uiIm4Ck=\\n-----END PRIVATE KEY-----\\n",
"client_email": "firebase-adminsdk-o2wqi@ride-b1bd8.iam.gserviceaccount.com",
"client_id": "111210077025005706623",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-o2wqi%40ride-b1bd8.iam.gserviceaccount.com",
"universe_domain": "googleapis.com"
}
'''; // As defined above
// Initialize AccessTokenManager
final accessTokenManager = AccessTokenManager(serviceAccountKeyJson);
// Obtain an OAuth 2.0 access token
final accessToken = await accessTokenManager.getAccessToken();
// Send the notification
final response = await http.post(
Uri.parse(
'https://fcm.googleapis.com/v1/projects/ride-b1bd8/messages:send'),
headers: <String, String>{
'Content-Type': 'application/json',
'Authorization': 'Bearer $accessToken',
},
body: jsonEncode({
'message': {
'token': token,
'notification': {
'title': title,
'body': body,
},
// 'data': {
// 'DriverList': jsonEncode([]),
// },
'android': {
'priority': 'high', // Set priority to high
'notification': {
'sound': tone,
},
},
'apns': {
'headers': {
'apns-priority': '10', // Set APNs priority to 10
},
'payload': {
'aps': {
'sound': tone,
},
},
},
},
}),
);
if (response.statusCode == 200) {
print(
'Notification sent successfully. Status code: ${response.statusCode}');
print('Response body: ${response.body}');
} else {
print(
'Failed to send notification. Status code: ${response.statusCode}');
print('Response body: ${response.body}');
if (retryCount > 0) {
print('Retrying... Attempts remaining: $retryCount');
await Future.delayed(
const Duration(seconds: 2)); // Optional delay before retrying
return sendNotificationToAnyWithoutData(title, body, token, tone,
retryCount: retryCount - 1);
}
}
} catch (e) {
print('Error sending notification: $e');
if (retryCount > 0) {
print('Retrying... Attempts remaining: $retryCount');
await Future.delayed(
const Duration(seconds: 2)); // Optional delay before retrying
return sendNotificationToAnyWithoutData(title, body, token, tone,
retryCount: retryCount - 1);
}
}
}
void sendNotificationToDriverMAP(
String title, String body, String token, List<String> data, String tone,
{int retryCount = 2}) async {
@@ -946,79 +842,6 @@ class FirebaseMessagesController extends GetxController {
}
}
}
void sendNotificationToDriverMapPolyline(String title, String body,
String token, List<String> data, String polylineJson, String tone) async {
try {
String serviceAccountKeyJson = '''{
"type": "service_account",
"project_id": "ride-b1bd8",
"private_key_id": "75e817c0b902db2ef35edf2c2bd159dec1f13249",
"private_key": "-----BEGIN PRIVATE KEY-----\\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQD0zH9TQGDQHUv3\\na3/JAD1UKPwAp3wNKT0a6fxiIzjI3JxQWI30QvZCcfl6CdMhIcydX1ncSaYTcEeC\\n/AdPVCPkqyJx1YIGGg6P/mRzCWeaN8fsp6z250m5vcObDCZc3dbJEkepbep+6FPY\\n21m3KO+AHh1glgsTGZOTm5xiU8NGXpdk2QEh8wpiIIlR/HuKwVw9g8urNe3Sno+U\\nDm3z37iFqvZdmpqO8aWTJu6beb3hsREK9XK2I9JqC2JUwiGQRo3idOvPP6hkqrWx\\nKSX96vglQFYfakvJdDp2ZATOlpBYPMtS/IWhJ985u58TSS+Kl8qpnpaZBSxgJirf\\nhWzhnKLfAgMBAAECggEAJP785SePGhS7ZN6ltspm+l+hSjYFrPWFCxq+rlQ1YkHZ\\nC9l+RqKSFhOkiPmQI2s4wbXl3kFxLHHlFNoi/q2wKQBmGb8TQfnRJpjjNHGA61Ev\\n0Ue7/6qPvVb9B2MsLw/FxKiTFPuMG3bgKR9pbSFuJLYoaW7zqITOhVnYphGTqwAY\\nBVVcvISSLvELDmH9VZcv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n-----END PRIVATE KEY-----\\n",
"client_email": "firebase-adminsdk-o2wqi@ride-b1bd8.iam.gserviceaccount.com",
"client_id": "111210077025005706623",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-o2wqi%40ride-b1bd8.iam.gserviceaccount.com",
"universe_domain": "googleapis.com"
}
'''; // As defined above
// Initialize AccessTokenManager
final accessTokenManager = AccessTokenManager(serviceAccountKeyJson);
// Obtain an OAuth 2.0 access token
final accessToken = await accessTokenManager.getAccessToken();
// Send the notification
final response = await http.post(
Uri.parse(
'https://fcm.googleapis.com/v1/projects/ride-b1bd8/messages:send'),
headers: <String, String>{
'Content-Type': 'application/json',
'Authorization': 'Bearer $accessToken',
},
body: jsonEncode({
'message': {
'token': token,
'notification': {
'title': title,
'body': body,
},
'data': {
'DriverList': jsonEncode(data),
},
'android': {
'priority': 'high', // Set priority to high
'notification': {
'sound': tone,
},
},
'apns': {
'headers': {
'apns-priority': '10', // Set APNs priority to 10
},
'payload': {
'aps': {
'sound': tone,
},
},
},
},
}),
);
if (response.statusCode == 200) {
// Notification sent successfully
} else {
// Handle error response
'Failed to send notification. Status code: ${response.statusCode}';
}
} catch (e) {
// Handle other exceptions
}
}
}
class OverlayContent extends StatelessWidget {