Files
intaleq/lib/controller/notification/passenger_notification_controller.dart

88 lines
2.5 KiB
Dart

import 'dart:convert';
import 'package:get/get.dart';
import 'package:Intaleq/controller/firebase/firbase_messge.dart';
import '../../constant/box_name.dart';
import '../../constant/links.dart';
import '../../main.dart';
import '../../views/widgets/mydialoug.dart';
import '../firebase/notification_service.dart';
import '../functions/crud.dart';
class PassengerNotificationController extends GetxController {
bool isloading = false;
Map notificationData = {};
getNotifications() async {
isloading = true;
update();
var res = await CRUD().get(
link: AppLink.getNotificationPassenger,
payload: {'passenger_id': box.read(BoxName.passengerID)});
if (res == "failure" || res == "error") {
MyDialog().getDialog('There is no notification yet'.tr, '', () {
Get.back();
Get.back();
});
} else {
final decoded = jsonDecode(res);
// التحقق من وجود البيانات في 'data' أو 'message'
var rawData = decoded['data'] ?? decoded['message'];
if (decoded['status'] == 'error' || decoded['status'] == 'failure' || rawData == "No notification data found") {
notificationData = {'status': 'success', 'message': []}; // قائمة فارغة لمنع الخطأ في UI
} else {
// التأكد أننا نخزن قائمة
notificationData = {
'status': 'success',
'message': rawData is List ? rawData : [rawData]
};
}
isloading = false;
update();
}
// sql.insertData(notificationData['message'], TableName.captainNotification);
}
updateNotification(String id) async {
await CRUD().post(
link: AppLink.updateNotificationPassenger,
payload: {'isShown': 'true', 'id': id},
);
Get.back();
getNotifications();
}
addNotificationToPassenger(String title, body) async {
var res = CRUD().post(link: AppLink.addNotificationPassenger, payload: {
'title': title,
'body': body,
});
// Get.find<FirebaseMessagesController>().sendNotificationToPassengerToken(
// title,
// body,
// 'token',
// [],
// 'iphone_ringtone',
// );
await NotificationService.sendNotification(
category: title,
target: 'token'.toString(),
title: title,
body: body.tr,
isTopic: false, // Important: this is a token
tone: 'cancel',
driverList: [],
);
}
@override
void onInit() {
getNotifications();
super.onInit();
}
}