8/11/1
This commit is contained in:
@@ -12,6 +12,7 @@ import '../../constant/links.dart';
|
||||
import '../../constant/style.dart';
|
||||
import '../../main.dart';
|
||||
import '../../views/widgets/elevated_btn.dart';
|
||||
import '../notification_controller.dart';
|
||||
import 'local_notification.dart';
|
||||
|
||||
class FirebaseMessagesController extends GetxController {
|
||||
@@ -20,10 +21,11 @@ class FirebaseMessagesController extends GetxController {
|
||||
List<String> tokens = [];
|
||||
List<String> tokensPassengers = [];
|
||||
List dataTokens = [];
|
||||
List dataTokensPassenger = [];
|
||||
late String driverID;
|
||||
late String driverToken;
|
||||
NotificationSettings? notificationSettings;
|
||||
|
||||
bool isLoading = false;
|
||||
Future<void> getNotificationSettings() async {
|
||||
// Get the current notification settings
|
||||
NotificationSettings? notificationSettings =
|
||||
@@ -78,27 +80,126 @@ class FirebaseMessagesController extends GetxController {
|
||||
}
|
||||
}
|
||||
|
||||
Future getAllTokenDrivers() async {
|
||||
var res = await http.post(
|
||||
Uri.parse(AppLink.getAllTokenDrivers),
|
||||
headers: {
|
||||
'Authorization':
|
||||
'Basic ${base64Encode(utf8.encode(AK.basicAuthCredentials))}',
|
||||
},
|
||||
body: {},
|
||||
);
|
||||
var jsonResponse = jsonDecode(res.body);
|
||||
if (jsonResponse['status'] == 'success') {
|
||||
dataTokens = jsonResponse['data'];
|
||||
for (var i = 0; i < dataTokens.length; i++) {
|
||||
tokens.add(jsonResponse['data'][i]['token']);
|
||||
var currentPage = 1;
|
||||
var totalPages = 1;
|
||||
Future<void> getAllTokenDrivers({int page = 1}) async {
|
||||
isLoading = true;
|
||||
try {
|
||||
var res = await http.post(
|
||||
Uri.parse(AppLink.getAllTokenDrivers),
|
||||
headers: {
|
||||
'Authorization':
|
||||
'Basic ${base64Encode(utf8.encode(AK.basicAuthCredentials))}',
|
||||
},
|
||||
body: {
|
||||
'page': page.toString(),
|
||||
},
|
||||
);
|
||||
|
||||
var jsonResponse = jsonDecode(res.body);
|
||||
if (jsonResponse['status'] == 'success') {
|
||||
var newData = jsonResponse['data'] as List;
|
||||
if (page == 1) {
|
||||
dataTokens.clear();
|
||||
tokens.clear();
|
||||
}
|
||||
dataTokens.addAll(newData);
|
||||
for (var item in newData) {
|
||||
tokens.add(item['token']);
|
||||
}
|
||||
currentPage = int.parse(jsonResponse['currentPage']);
|
||||
totalPages = jsonResponse['totalPages'];
|
||||
box.write(BoxName.tokensDrivers, tokens);
|
||||
} else {
|
||||
Get.defaultDialog(
|
||||
title: "Warning", middleText: "No more data available");
|
||||
}
|
||||
box.write(BoxName.tokensDrivers, tokens);
|
||||
} else {
|
||||
Get.defaultDialog(title: "Warning", middleText: "Server Error");
|
||||
} catch (e) {
|
||||
Get.defaultDialog(title: "Error", middleText: "Server Error: $e");
|
||||
} finally {
|
||||
isLoading = false;
|
||||
}
|
||||
}
|
||||
|
||||
var currentPagePassenger = 1;
|
||||
var totalPagesPassenger = 1;
|
||||
Future<void> getAllTokenPassenger({int page = 1}) async {
|
||||
isLoading = true;
|
||||
try {
|
||||
var res = await http.post(
|
||||
Uri.parse(AppLink.getAllTokenDrivers),
|
||||
headers: {
|
||||
'Authorization':
|
||||
'Basic ${base64Encode(utf8.encode(AK.basicAuthCredentials))}',
|
||||
},
|
||||
body: {
|
||||
'page': page.toString(),
|
||||
},
|
||||
);
|
||||
|
||||
var jsonResponse = jsonDecode(res.body);
|
||||
if (jsonResponse['status'] == 'success') {
|
||||
var newData = jsonResponse['data'] as List;
|
||||
if (page == 1) {
|
||||
dataTokensPassenger.clear();
|
||||
tokensPassengers.clear();
|
||||
}
|
||||
dataTokensPassenger.addAll(newData);
|
||||
for (var item in newData) {
|
||||
tokensPassengers.add(item['token']);
|
||||
}
|
||||
currentPagePassenger = int.parse(jsonResponse['currentPage']);
|
||||
totalPagesPassenger = jsonResponse['totalPages'];
|
||||
box.write(BoxName.tokensPassengers, tokensPassengers);
|
||||
} else {
|
||||
Get.defaultDialog(
|
||||
title: "Warning", middleText: "No more data available");
|
||||
}
|
||||
} catch (e) {
|
||||
Get.defaultDialog(title: "Error", middleText: "Server Error: $e");
|
||||
} finally {
|
||||
isLoading = false;
|
||||
}
|
||||
}
|
||||
|
||||
bool isSendingNotifications = false;
|
||||
Future<void> loadAllPagesAndSendNotifications() async {
|
||||
isSendingNotifications = true;
|
||||
currentPage = 1;
|
||||
|
||||
while (currentPage <= totalPages) {
|
||||
await getAllTokenDrivers(page: currentPage);
|
||||
await NotificationController().sendNotificationDrivers();
|
||||
print(tokens);
|
||||
if (currentPage < totalPages) {
|
||||
await Future.delayed(const Duration(seconds: 3));
|
||||
}
|
||||
currentPage++;
|
||||
}
|
||||
|
||||
isSendingNotifications = false;
|
||||
Get.snackbar("Success", "All notifications sent!");
|
||||
}
|
||||
|
||||
bool isSendingNotificationsPassenger = false;
|
||||
Future<void> loadAllPagesAndSendNotificationsPassengers() async {
|
||||
isSendingNotificationsPassenger = true;
|
||||
currentPage = 1;
|
||||
|
||||
while (currentPagePassenger <= totalPagesPassenger) {
|
||||
await getAllTokenPassenger(page: currentPagePassenger);
|
||||
await NotificationController().sendNotificationDrivers();
|
||||
print(tokensPassengers);
|
||||
if (currentPagePassenger < totalPagesPassenger) {
|
||||
await Future.delayed(const Duration(seconds: 3));
|
||||
}
|
||||
currentPagePassenger++;
|
||||
}
|
||||
|
||||
isSendingNotificationsPassenger = false;
|
||||
Get.snackbar("Success", "All notifications sent!");
|
||||
}
|
||||
|
||||
Future getAllTokenPassengers() async {
|
||||
var res = await http.post(
|
||||
Uri.parse(AppLink.getAllTokenPassengers),
|
||||
|
||||
Reference in New Issue
Block a user