This commit is contained in:
Hamza-Ayed
2024-11-17 12:02:54 +02:00
parent bf8a29b814
commit f796f4bc48
13 changed files with 611 additions and 493 deletions

View File

@@ -1,92 +1,12 @@
// import 'package:flutter_local_notifications/flutter_local_notifications.dart';
// import 'package:get/get.dart';
// import 'package:timezone/data/latest.dart' as tz;
// import 'package:timezone/timezone.dart' as tz;
// class NotificationController extends GetxController {
// final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin =
// FlutterLocalNotificationsPlugin();
// // Initializes the local notifications plugin
// Future<void> initNotifications() async {
// const AndroidInitializationSettings android =
// AndroidInitializationSettings('@mipmap/launcher_icon');
// const InitializationSettings initializationSettings =
// InitializationSettings(android: android);
// await _flutterLocalNotificationsPlugin.initialize(initializationSettings);
// }
// // Displays a notification with the given title and message
// void showNotification(String title, String message, String tone) async {
// AndroidNotificationDetails android = AndroidNotificationDetails(
// 'high_importance_channel',
// 'High Importance Notifications',
// importance: Importance.max,
// priority: Priority.high,
// showWhen: false,
// sound: RawResourceAndroidNotificationSound(tone),
// );
// DarwinNotificationDetails ios = const DarwinNotificationDetails(
// sound: 'default',
// presentAlert: true,
// presentBadge: true,
// presentSound: true,
// );
// NotificationDetails details =
// NotificationDetails(android: android, iOS: ios);
// await _flutterLocalNotificationsPlugin.show(0, title, message, details);
// }
// // Schedules a notification after 1 minute
// void scheduleNotificationAfter1Minute(
// String title, String message, String tone) async {
// AndroidNotificationDetails android = AndroidNotificationDetails(
// 'high_importance_channel', 'High Importance Notifications',
// importance: Importance.max,
// priority: Priority.high,
// showWhen: false,
// sound: RawResourceAndroidNotificationSound(tone));
// DarwinNotificationDetails ios = const DarwinNotificationDetails(
// sound: 'default',
// presentAlert: true,
// presentBadge: true,
// presentSound: true,
// );
// NotificationDetails details =
// NotificationDetails(android: android, iOS: ios);
// // Schedule the notification to be shown after 1 minute
// final now = tz.TZDateTime.now(tz.local);
// final scheduledTime = now.add(const Duration(minutes: 1));
// await _flutterLocalNotificationsPlugin.zonedSchedule(
// 0,
// title,
// message,
// scheduledTime,
// details,
// androidAllowWhileIdle: true,
// uiLocalNotificationDateInterpretation:
// UILocalNotificationDateInterpretation.absoluteTime,
// matchDateTimeComponents: DateTimeComponents.time,
// );
// }
// }
import 'dart:async';
import 'dart:io';
import 'package:SEFER/constant/box_name.dart';
import 'package:SEFER/main.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:timezone/data/latest.dart' as tz;
import 'package:timezone/timezone.dart' as tz;
import '../../print.dart';
class NotificationController extends GetxController {
final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
@@ -139,8 +59,11 @@ class NotificationController extends GetxController {
await _flutterLocalNotificationsPlugin.show(0, title, message, details);
print('Notification shown: $title - $message');
}
// /Users/hamzaaleghwairyeen/development/App/ride 2/lib/controller/firebase/local_notification.dart
void scheduleDailyNotifications(
// Assume _flutterLocalNotificationsPlugin is initialized somewhere in your code
void scheduleNotificationsForSevenDays(
String title, String message, String tone) async {
final AndroidNotificationDetails android = AndroidNotificationDetails(
'high_importance_channel',
@@ -172,53 +95,68 @@ class NotificationController extends GetxController {
}
}
// Schedule notifications for 10:00 AM and 3:00 PM daily
await _scheduleNotificationForTime(8, 0, title, message, details);
await _scheduleNotificationForTime(15, 0, title, message, details);
await _scheduleNotificationForTime(20, 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, 8, 0, title, message, details, day * 1000 + 1); // Unique ID
print('Daily notifications scheduled successfully');
// Schedule for 3:00 PM
await _scheduleNotificationForTime(
day, 15, 0, title, message, details, day * 1000 + 2); // Unique ID
// Schedule for 8:00 PM
await _scheduleNotificationForTime(
day, 20, 0, title, message, details, day * 1000 + 3); // 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();
var cairoLocation;
// if (box.read(BoxName.countryCode).toString() == 'Egypt') {
cairoLocation = tz.getLocation('Africa/Cairo');
// } else {
// cairoLocation = tz.getLocation('UTC');
// } // todo get for location country
// Set Cairo timezone
Log.print('cairoLocation: ${cairoLocation}');
final now = tz.TZDateTime.now(
cairoLocation); // Use Cairo timezone for the current time
var cairoLocation = tz.getLocation('Africa/Cairo');
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');
}
}