25-10-5/1
This commit is contained in:
@@ -69,7 +69,9 @@ class FirebaseMessagesController extends GetxController {
|
||||
}
|
||||
|
||||
NotificationController notificationController =
|
||||
Get.put(NotificationController());
|
||||
Get.isRegistered<NotificationController>()
|
||||
? Get.find<NotificationController>()
|
||||
: Get.put(NotificationController());
|
||||
|
||||
Future getTokens() async {
|
||||
String? basicAuthCredentials =
|
||||
@@ -96,7 +98,7 @@ class FirebaseMessagesController extends GetxController {
|
||||
|
||||
Future getToken() async {
|
||||
fcmToken.getToken().then((token) {
|
||||
Log.print('fcmToken: ${token}');
|
||||
// Log.print('fcmToken: ${token}');
|
||||
box.write(BoxName.tokenFCM, (token.toString()));
|
||||
});
|
||||
|
||||
@@ -125,16 +127,17 @@ class FirebaseMessagesController extends GetxController {
|
||||
|
||||
Future<void> fireBaseTitles(RemoteMessage message) async {
|
||||
if (message.notification!.title! == 'Order'.tr) {
|
||||
Log.print('message: ${message}');
|
||||
if (Platform.isAndroid) {
|
||||
notificationController.showNotification(
|
||||
'Order', message.notification!.body!, 'Order');
|
||||
'Order'.tr, message.notification!.body!, 'Order');
|
||||
}
|
||||
} else if (message.notification!.title! == 'Accepted Ride') {
|
||||
if (Platform.isAndroid) {
|
||||
notificationController.showNotification(
|
||||
'Accepted Ride'.tr, 'Driver Accepted the Ride for You'.tr, 'ding');
|
||||
}
|
||||
var passengerList = message.data['DriverList'];
|
||||
var passengerList = message.data['passengerList'];
|
||||
|
||||
var myList = jsonDecode(passengerList) as List<dynamic>;
|
||||
Log.print('myList: ${myList}');
|
||||
@@ -513,7 +516,7 @@ class FirebaseMessagesController extends GetxController {
|
||||
// 'DriverList': jsonEncode(data),
|
||||
// },
|
||||
'android': {
|
||||
'priority': 'high', // Set priority to high
|
||||
'priority': 'HIGH ', // Set priority to high
|
||||
'notification': {
|
||||
'sound': tone,
|
||||
},
|
||||
@@ -549,7 +552,7 @@ class FirebaseMessagesController extends GetxController {
|
||||
// 'body': body,
|
||||
// 'sound': 'true'
|
||||
// },
|
||||
// 'priority': 'high',
|
||||
// 'priority': 'HIGH ',
|
||||
// 'data': <String, dynamic>{
|
||||
// 'click_action': 'FLUTTER_NOTIFICATION_CLICK',
|
||||
// 'id': '1',
|
||||
@@ -581,7 +584,7 @@ class FirebaseMessagesController extends GetxController {
|
||||
|
||||
Future<void> sendNotificationToDriverMAP(
|
||||
String title, String body, String token, List<String> data, String tone,
|
||||
{int retryCount = 2}) async {
|
||||
{int retryCount = 1}) async {
|
||||
try {
|
||||
if (serviceAccountKeyJson.isEmpty) {
|
||||
print("🔴 Error: Service Account Key is empty");
|
||||
@@ -590,8 +593,8 @@ class FirebaseMessagesController extends GetxController {
|
||||
|
||||
// Initialize AccessTokenManager
|
||||
final accessTokenManager = AccessTokenManager(serviceAccountKeyJson);
|
||||
Log.print(
|
||||
'accessTokenManager: ${accessTokenManager.serviceAccountJsonKey}');
|
||||
// Log.print(
|
||||
// 'accessTokenManager: ${accessTokenManager.serviceAccountJsonKey}');
|
||||
|
||||
// Obtain an OAuth 2.0 access token
|
||||
final accessToken = await accessTokenManager.getAccessToken();
|
||||
@@ -616,7 +619,7 @@ class FirebaseMessagesController extends GetxController {
|
||||
'DriverList': jsonEncode(data),
|
||||
},
|
||||
'android': {
|
||||
'priority': 'high', // Set priority to high
|
||||
'priority': 'HIGH ', // Set priority to high
|
||||
'notification': {
|
||||
'sound': tone,
|
||||
},
|
||||
@@ -693,7 +696,7 @@ class FirebaseMessagesController extends GetxController {
|
||||
'body': body,
|
||||
},
|
||||
'android': {
|
||||
'priority': 'high', // Set priority to high
|
||||
'priority': 'HIGH ', // Set priority to high
|
||||
'notification': {
|
||||
'sound': tone,
|
||||
},
|
||||
|
||||
71
lib/controller/firebase/notification_service.dart
Normal file
71
lib/controller/firebase/notification_service.dart
Normal file
@@ -0,0 +1,71 @@
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'dart:convert';
|
||||
|
||||
class NotificationService {
|
||||
// استبدل هذا الرابط بالرابط الصحيح لملف PHP على السيرفر الخاص بك
|
||||
static const String _serverUrl =
|
||||
'https://syria.intaleq.xyz/intaleq/fcm/send_fcm.php';
|
||||
|
||||
/// Sends a notification via your backend server.
|
||||
///
|
||||
/// [target]: The device token or the topic name.
|
||||
/// [title]: The notification title.
|
||||
/// [body]: The notification body.
|
||||
/// [isTopic]: Set to true if the target is a topic, false if it's a device token.
|
||||
static Future<void> sendNotification({
|
||||
required String target,
|
||||
required String title,
|
||||
required String body,
|
||||
bool isTopic = false,
|
||||
}) async {
|
||||
try {
|
||||
final response = await http.post(
|
||||
Uri.parse(_serverUrl),
|
||||
headers: {
|
||||
'Content-Type': 'application/json; charset=UTF-8',
|
||||
},
|
||||
body: jsonEncode({
|
||||
'target': target,
|
||||
'title': title,
|
||||
'body': body,
|
||||
'isTopic': isTopic,
|
||||
}),
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
print('Notification sent successfully.');
|
||||
print('Server Response: ${response.body}');
|
||||
} else {
|
||||
print(
|
||||
'Failed to send notification. Status code: ${response.statusCode}');
|
||||
print('Server Error: ${response.body}');
|
||||
}
|
||||
} catch (e) {
|
||||
print('An error occurred while sending notification: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- Example of how to use it ---
|
||||
|
||||
// To send to a specific driver (using their token)
|
||||
void sendToSpecificDriver() {
|
||||
String driverToken =
|
||||
'bk3RNwTe3H0:CI2k_HHwgIpoDKCI5oT...'; // The driver's FCM token
|
||||
NotificationService.sendNotification(
|
||||
target: driverToken,
|
||||
title: 'New Trip Request!',
|
||||
body: 'A passenger is waiting for you.',
|
||||
isTopic: false, // Important: this is a token
|
||||
);
|
||||
}
|
||||
|
||||
// To send to all drivers (using a topic)
|
||||
void sendToAllDrivers() {
|
||||
NotificationService.sendNotification(
|
||||
target: 'drivers', // The name of the topic
|
||||
title: 'Important Announcement',
|
||||
body: 'Please update your app to the latest version.',
|
||||
isTopic: true, // Important: this is a topic
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user