Files
intaleq/lib/controller/firebase/notification_service.dart
Hamza-Ayed de84662e02 25-10-6/1
2025-10-05 23:45:18 +03:00

56 lines
1.7 KiB
Dart

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';
static Future<void> sendNotification({
required String target,
required String title,
required String body,
String? tone,
List<String>? driverList, // <-- [تعديل 1] : إضافة المتغير الجديد
bool isTopic = false,
}) async {
try {
final Map<String, dynamic> payload = {
'target': target,
'title': title,
'body': body,
'isTopic': isTopic,
};
// نضيف النغمة فقط إذا لم تكن فارغة
if (tone != null) {
payload['tone'] = tone;
}
// <-- [تعديل 2] : نضيف قائمة البيانات بعد تشفيرها إلى JSON
if (driverList != null) {
payload['driverList'] = jsonEncode(driverList);
}
final response = await http.post(
Uri.parse(_serverUrl),
headers: {
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(payload),
);
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');
}
}
}