import 'package:Intaleq/print.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 const String _batchServerUrl = 'https://syria.intaleq.xyz/intaleq/fcm/send_fcm_batch.php'; static Future sendNotification({ required String target, required String title, required String body, required String? category, // <-- [الإضافة الأولى] String? tone, List? driverList, // <-- [تعديل 1] : إضافة المتغير الجديد bool isTopic = false, }) async { try { final Map payload = { 'target': target, 'title': title, 'body': body, 'isTopic': isTopic, }; if (category != null) { payload['category'] = category; // <-- [الإضافة الثانية] (النص الثابت للتحكم) } // نضيف النغمة فقط إذا لم تكن فارغة 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'); } } /// [4] !! دالة جديدة مضافة !! /// ترسل إشعاراً "مجمعاً" إلى قائمة من السائقين static Future sendBatchNotification({ required List targets, // <-- قائمة التوكينز required String title, required String body, String? tone, List? driverList, // <-- بيانات الرحلة (نفسها للجميع) }) async { // لا ترسل شيئاً إذا كانت القائمة فارغة if (targets.isEmpty) { Log.print('⚠️ [Batch] No targets to send to. Skipped.'); return; } try { final Map payload = { // "targets" بدلاً من "target" 'targets': jsonEncode(targets), // تشفير قائمة التوكينز 'title': title, 'body': body, }; if (tone != null) { payload['tone'] = tone; } // بيانات الرحلة (DriverList) if (driverList != null) { payload['driverList'] = jsonEncode(driverList); } final response = await http.post( Uri.parse(_batchServerUrl), // <-- !! تستخدم الرابط الجديد headers: { 'Content-Type': 'application/json; charset=UTF-8', }, body: jsonEncode(payload), ); if (response.statusCode == 200) { Log.print('✅ [Batch] Notifications sent successfully.'); Log.print('Server Response: ${response.body}'); } else { Log.print('❌ [Batch] Failed to send. Status: ${response.statusCode}'); Log.print('Server Error: ${response.body}'); } } catch (e) { Log.print('❌ [Batch] An error occurred: $e'); } } }