26-1-22/1
This commit is contained in:
137
lib/main.dart
137
lib/main.dart
@@ -155,68 +155,103 @@ Future<void> createAllNotificationChannels() async {
|
||||
}
|
||||
|
||||
/// ============ Handlers: Background ============
|
||||
// في ملف main.dart (خارج كلاس MyApp)
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
Future<void> backgroundMessageHandler(RemoteMessage message) async {
|
||||
// 1. تهيئة بيئة فلاتر في الخلفية
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await initFirebaseIfNeeded();
|
||||
await GetStorage.init();
|
||||
|
||||
if (!Get.isRegistered<NotificationController>()) {
|
||||
Get.put(NotificationController());
|
||||
}
|
||||
if (!Get.isRegistered<FirebaseMessagesController>()) {
|
||||
Get.put(FirebaseMessagesController());
|
||||
}
|
||||
// 2. تهيئة الكونترولر (لأنه isolate منفصل)
|
||||
// ملاحظة: تأكد أنك لا تعتمد على Context هنا
|
||||
final NotificationController notificationController =
|
||||
NotificationController();
|
||||
|
||||
if (!await FlutterOverlayWindow.isPermissionGranted()) {
|
||||
Log.print("Overlay permission not granted; showing only notification.");
|
||||
}
|
||||
// مهم جداً: إعادة تهيئة الإشعارات داخل هذه العملية المنفصلة
|
||||
await notificationController.initNotifications();
|
||||
|
||||
if (Platform.isAndroid) {
|
||||
String category = message.data['category'] ?? '';
|
||||
if (message.notification != null) {
|
||||
Log.print('message.notification!.title: ${message.notification!.title}');
|
||||
print("🟢 Background Message Received: ${message.data}");
|
||||
|
||||
if (category == 'Order' || category == 'OrderSpeed') {
|
||||
final myListString = message.data['DriverList'] ?? '[]';
|
||||
Log.print('myListString: $myListString');
|
||||
// 3. استخراج البيانات (الآن العنوان والنص داخل data وليس notification)
|
||||
String? title = message.data['title'];
|
||||
String? body = message.data['body'];
|
||||
String? tone = message.data['tone'] ?? 'order';
|
||||
String? myListString = message.data['DriverList'];
|
||||
|
||||
List<dynamic> myList;
|
||||
try {
|
||||
myList = jsonDecode(myListString) as List<dynamic>;
|
||||
} catch (e) {
|
||||
Log.print('Error decoding JSON: $e');
|
||||
myList = [];
|
||||
}
|
||||
|
||||
final isOverlayActive = await FlutterOverlayWindow.isActive();
|
||||
if (isOverlayActive) {
|
||||
await FlutterOverlayWindow.shareData(myList);
|
||||
} else {
|
||||
await FlutterOverlayWindow.showOverlay(
|
||||
enableDrag: true,
|
||||
flag: OverlayFlag.focusPointer,
|
||||
positionGravity: PositionGravity.auto,
|
||||
height: WindowSize.matchParent,
|
||||
width: WindowSize.matchParent,
|
||||
startPosition: const OverlayPosition(0, -30),
|
||||
);
|
||||
await FlutterOverlayWindow.shareData(myList);
|
||||
}
|
||||
|
||||
NotificationController().showNotification(
|
||||
message.notification!.title.toString(),
|
||||
message.notification!.body.toString(),
|
||||
'order',
|
||||
myListString,
|
||||
);
|
||||
} else {
|
||||
FirebaseMessagesController().fireBaseTitles(message);
|
||||
}
|
||||
}
|
||||
// 4. شرط الأمان: التأكد من وجود البيانات المطلوبة
|
||||
if (title != null && body != null && myListString != null) {
|
||||
// 5. عرض الإشعار المحلي
|
||||
notificationController.showOrderNotification(
|
||||
title,
|
||||
body,
|
||||
'ding.wav',
|
||||
myListString,
|
||||
);
|
||||
} else {
|
||||
print("⚠️ Received empty data message or missing fields.");
|
||||
}
|
||||
}
|
||||
// @pragma('vm:entry-point')
|
||||
// Future<void> backgroundMessageHandler(RemoteMessage message) async {
|
||||
// WidgetsFlutterBinding.ensureInitialized();
|
||||
// await initFirebaseIfNeeded();
|
||||
// await GetStorage.init();
|
||||
|
||||
// if (!Get.isRegistered<NotificationController>()) {
|
||||
// Get.put(NotificationController());
|
||||
// }
|
||||
// if (!Get.isRegistered<FirebaseMessagesController>()) {
|
||||
// Get.put(FirebaseMessagesController());
|
||||
// }
|
||||
|
||||
// if (!await FlutterOverlayWindow.isPermissionGranted()) {
|
||||
// Log.print("Overlay permission not granted; showing only notification.");
|
||||
// }
|
||||
|
||||
// if (Platform.isAndroid) {
|
||||
// String category = message.data['category'] ?? '';
|
||||
// Log.print('category: ${category}');
|
||||
// // if (message.notification != null) {
|
||||
// // Log.print('message.notification!.title: ${message.notification!.title}');
|
||||
|
||||
// if (category == 'Order' || category == 'OrderSpeed') {
|
||||
// final myListString = message.data['DriverList'] ?? '[]';
|
||||
// Log.print('myListString: $myListString');
|
||||
|
||||
// List<dynamic> myList;
|
||||
// try {
|
||||
// myList = jsonDecode(myListString) as List<dynamic>;
|
||||
// } catch (e) {
|
||||
// Log.print('Error decoding JSON: $e');
|
||||
// myList = [];
|
||||
// }
|
||||
|
||||
// // final isOverlayActive = await FlutterOverlayWindow.isActive();
|
||||
// // if (isOverlayActive) {
|
||||
// // await FlutterOverlayWindow.shareData(myList);
|
||||
// // } else {
|
||||
// // await FlutterOverlayWindow.showOverlay(
|
||||
// // enableDrag: true,
|
||||
// // flag: OverlayFlag.focusPointer,
|
||||
// // positionGravity: PositionGravity.auto,
|
||||
// // height: WindowSize.matchParent,
|
||||
// // width: WindowSize.matchParent,
|
||||
// // startPosition: const OverlayPosition(0, -30),
|
||||
// // );
|
||||
// // await FlutterOverlayWindow.shareData(myList);
|
||||
// // }
|
||||
// NotificationController().showOrderNotification(
|
||||
// message.notification?.title ?? "طلب جديد", // العنوان
|
||||
// message.notification?.body ?? "لديك طلب توصيل جديد", // النص الأساسي
|
||||
// 'ding', // اسم نغمة الإشعار (تأكد أنها موجودة في raw)
|
||||
// myListString, // البيانات القادمة من السيرفر (JSON String List)
|
||||
// );
|
||||
// } else {
|
||||
// FirebaseMessagesController().fireBaseTitles(message);
|
||||
// }
|
||||
// // }
|
||||
// }
|
||||
// }
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
void notificationTapBackground(NotificationResponse notificationResponse) {
|
||||
|
||||
Reference in New Issue
Block a user