This commit is contained in:
Hamza-Ayed
2024-11-17 22:13:31 +02:00
parent e8c72d79a9
commit 5aeb3cf685
45 changed files with 856 additions and 284 deletions

View File

@@ -230,7 +230,7 @@ class NotificationController extends GetxController {
});
}
void scheduleDailyNotifications(
void scheduleNotificationsForSevenDays(
String title, String message, String tone) async {
final AndroidNotificationDetails android = AndroidNotificationDetails(
'high_importance_channel',
@@ -262,47 +262,72 @@ class NotificationController extends GetxController {
}
}
// Schedule notifications for 10:00 AM and 3:00 PM daily
await _scheduleNotificationForTime(7, 0, title, message, details);
await _scheduleNotificationForTime(13, 0, title, message, details);
await _scheduleNotificationForTime(18, 0, title, message, details);
// await _scheduleNotificationForTime(0, 22, title, message, details);
// Schedule notifications for the next 7 days
for (int day = 0; day < 7; day++) {
// Schedule for 8:00 AM
await _scheduleNotificationForTime(
day, 7, 0, title, message, details, day * 1000 + 1); // Unique ID
print('Daily notifications scheduled successfully');
await _scheduleNotificationForTime(
day, 10, 14, title, message, details, day * 1000 + 2); // Unique ID
// Schedule for 3:00 PM
await _scheduleNotificationForTime(
day, 13, 0, title, message, details, day * 1000 + 3); // Unique ID
// Schedule for 8:00 PM
await _scheduleNotificationForTime(
day, 20, 0, title, message, details, day * 1000 + 4); // Unique ID
}
print('Notifications scheduled successfully for the next 7 days');
}
// Helper function to get the next instance of a specific hour and minute
Future<void> _scheduleNotificationForTime(int hour, int minute, String title,
String message, NotificationDetails details) async {
Future<void> _scheduleNotificationForTime(
int dayOffset,
int hour,
int minute,
String title,
String message,
NotificationDetails details,
int notificationId,
) async {
// Initialize and set Cairo timezone
tz.initializeTimeZones();
final cairoLocation = tz.getLocation('Africa/Cairo'); // Set Cairo timezone
var cairoLocation = tz.getLocation('Africa/Cairo');
final now = tz.TZDateTime.now(
cairoLocation); // Use Cairo timezone for the current time
final now = tz.TZDateTime.now(cairoLocation);
tz.TZDateTime scheduledDate = tz.TZDateTime(
cairoLocation, now.year, now.month, now.day, hour, minute);
cairoLocation,
now.year,
now.month,
now.day + dayOffset, // Add offset to schedule for the next days
hour,
minute,
);
// If scheduled time is already past today, schedule it for the next day
// If the scheduled time is in the past, move it to the next day
if (scheduledDate.isBefore(now)) {
scheduledDate = scheduledDate.add(const Duration(days: 1));
scheduledDate = scheduledDate.add(Duration(days: 1));
}
print('Current time (Cairo): $now');
print('Scheduling notification for: $scheduledDate');
await _flutterLocalNotificationsPlugin.zonedSchedule(
0, // Use unique IDs if you want to manage each notification separately
notificationId, // Unique ID for each notification
title,
message,
scheduledDate,
details,
androidAllowWhileIdle: true,
androidScheduleMode: AndroidScheduleMode.exact,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
matchDateTimeComponents: DateTimeComponents.time,
matchDateTimeComponents:
null, // Don't repeat automatically; we handle 7 days manually
);
print('Notification scheduled successfully for Cairo timezone');
print('Notification scheduled successfully for: $scheduledDate');
}
void scheduleNotificationEvery10Hours(