This commit is contained in:
Hamza-Ayed
2024-11-10 18:38:07 +02:00
parent c9ac8da2ec
commit e8c72d79a9
25 changed files with 1211 additions and 329 deletions

View File

@@ -1,4 +1,6 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:SEFER/constant/colors.dart';
@@ -6,7 +8,11 @@ import 'package:SEFER/views/home/Captin/orderCaptin/order_request_page.dart';
import 'package:SEFER/views/home/Captin/orderCaptin/order_speed_request.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 '../../constant/box_name.dart';
import '../../main.dart';
import '../../print.dart';
import '../../views/notification/notification_captain.dart';
@@ -19,10 +25,17 @@ class NotificationController extends GetxController {
Future<void> initNotifications() async {
const AndroidInitializationSettings android =
AndroidInitializationSettings('@mipmap/launcher_icon');
const InitializationSettings initializationSettings =
InitializationSettings(android: android);
DarwinInitializationSettings ios = DarwinInitializationSettings(
requestAlertPermission: true,
requestBadgePermission: true,
requestSoundPermission: true,
onDidReceiveLocalNotification:
(int id, String? title, String? body, String? payload) async {},
);
InitializationSettings initializationSettings =
InitializationSettings(android: android, iOS: ios);
tz.initializeTimeZones();
print('Notifications initialized');
await _flutterLocalNotificationsPlugin.initialize(
initializationSettings,
onDidReceiveNotificationResponse: onDidReceiveNotificationResponse,
@@ -85,13 +98,266 @@ class NotificationController extends GetxController {
],
category: AndroidNotificationCategory.progress,
);
NotificationDetails details = NotificationDetails(android: android);
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,
payload: jsonEncode({'title': title, 'data': payLoad}));
}
void scheduleNotificationAtSpecificTime(
String title, String message, String tone, int hour, int minute) async {
// Initialize and set Cairo time zone
tz.initializeTimeZones();
var cairoLocation;
if (box.read(BoxName.countryCode).toString() == 'Egypt') {
cairoLocation = tz.getLocation('Africa/Cairo');
} else {} // todo get for location country
final AndroidNotificationDetails android = AndroidNotificationDetails(
'high_importance_channel',
'High Importance Notifications',
importance: Importance.max,
priority: Priority.high,
showWhen: false,
sound: RawResourceAndroidNotificationSound(tone),
);
const DarwinNotificationDetails ios = DarwinNotificationDetails(
sound: 'default',
presentAlert: true,
presentBadge: true,
presentSound: true,
);
final NotificationDetails details =
NotificationDetails(android: android, iOS: ios);
final now =
tz.TZDateTime.now(cairoLocation); // Use Cairo timezone for current time
tz.TZDateTime scheduledTime = tz.TZDateTime(
cairoLocation, now.year, now.month, now.day, hour, minute);
// If the scheduled time has already passed for today, schedule it for the next day
if (scheduledTime.isBefore(now)) {
scheduledTime = scheduledTime.add(const Duration(days: 1));
}
if (Platform.isAndroid) {
if (await Permission.scheduleExactAlarm.isDenied) {
if (await Permission.scheduleExactAlarm.request().isGranted) {
print('SCHEDULE_EXACT_ALARM permission granted');
} else {
print('SCHEDULE_EXACT_ALARM permission denied');
return;
}
}
}
print('Current time: $now');
print('Scheduling notification for: $scheduledTime');
await _flutterLocalNotificationsPlugin.zonedSchedule(
0,
title,
message,
scheduledTime,
details,
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
matchDateTimeComponents:
DateTimeComponents.time, // Triggers daily at the same time
);
print('Notification scheduled successfully');
}
void scheduleNotificationAfter1Minute(
String title, String message, String tone) async {
final AndroidNotificationDetails android = AndroidNotificationDetails(
'high_importance_channel',
'High Importance Notifications',
importance: Importance.max,
priority: Priority.high,
showWhen: false,
sound: RawResourceAndroidNotificationSound(tone),
);
const DarwinNotificationDetails ios = DarwinNotificationDetails(
sound: 'default',
presentAlert: true,
presentBadge: true,
presentSound: true,
);
final NotificationDetails details =
NotificationDetails(android: android, iOS: ios);
// Schedule the notification to be shown after 1 minute
Timer.periodic(const Duration(seconds: 15), (timer) async {
final now = tz.TZDateTime.now(tz.local);
final scheduledTime = now.add(const Duration(seconds: 10));
Log.print('scheduledTime: ${scheduledTime}');
if (Platform.isAndroid) {
if (await Permission.scheduleExactAlarm.isDenied) {
if (await Permission.scheduleExactAlarm.request().isGranted) {
print('SCHEDULE_EXACT_ALARM permission granted');
} else {
print('SCHEDULE_EXACT_ALARM permission denied');
return;
}
}
}
print('Scheduling notification for: $scheduledTime');
await _flutterLocalNotificationsPlugin.zonedSchedule(
0,
title,
message,
scheduledTime,
details,
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
matchDateTimeComponents: DateTimeComponents.time,
);
print('Notification scheduled successfully');
});
}
void scheduleDailyNotifications(
String title, String message, String tone) async {
final AndroidNotificationDetails android = AndroidNotificationDetails(
'high_importance_channel',
'High Importance Notifications',
importance: Importance.max,
priority: Priority.high,
sound: RawResourceAndroidNotificationSound(tone),
);
const DarwinNotificationDetails ios = DarwinNotificationDetails(
sound: 'default',
presentAlert: true,
presentBadge: true,
presentSound: true,
);
final NotificationDetails details =
NotificationDetails(android: android, iOS: ios);
// Check for the exact alarm permission on Android 12 and above
if (Platform.isAndroid) {
if (await Permission.scheduleExactAlarm.isDenied) {
if (await Permission.scheduleExactAlarm.request().isGranted) {
print('SCHEDULE_EXACT_ALARM permission granted');
} else {
print('SCHEDULE_EXACT_ALARM permission denied');
return;
}
}
}
// 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);
print('Daily notifications scheduled successfully');
}
// 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 {
// Initialize and set Cairo timezone
tz.initializeTimeZones();
final cairoLocation = tz.getLocation('Africa/Cairo'); // Set Cairo timezone
final now = tz.TZDateTime.now(
cairoLocation); // Use Cairo timezone for the current time
tz.TZDateTime scheduledDate = tz.TZDateTime(
cairoLocation, now.year, now.month, now.day, hour, minute);
// If scheduled time is already past today, schedule it for the next day
if (scheduledDate.isBefore(now)) {
scheduledDate = scheduledDate.add(const 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
title,
message,
scheduledDate,
details,
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
matchDateTimeComponents: DateTimeComponents.time,
);
print('Notification scheduled successfully for Cairo timezone');
}
void scheduleNotificationEvery10Hours(
String title, String message, String tone) async {
final AndroidNotificationDetails android = AndroidNotificationDetails(
'high_importance_channel',
'High Importance Notifications',
importance: Importance.max,
priority: Priority.high,
showWhen: false,
sound: RawResourceAndroidNotificationSound(tone),
);
const DarwinNotificationDetails ios = DarwinNotificationDetails(
sound: 'default',
presentAlert: true,
presentBadge: true,
presentSound: true,
);
final NotificationDetails details =
NotificationDetails(android: android, iOS: ios);
if (Platform.isAndroid) {
if (await Permission.scheduleExactAlarm.isDenied) {
if (await Permission.scheduleExactAlarm.request().isGranted) {
print('SCHEDULE_EXACT_ALARM permission granted');
} else {
print('SCHEDULE_EXACT_ALARM permission denied');
return;
}
}
}
Timer.periodic(const Duration(hours: 10), (timer) async {
final now = tz.TZDateTime.now(tz.local);
final scheduledTime = now.add(const Duration(minutes: 10));
print('Scheduling notification for: $scheduledTime');
await _flutterLocalNotificationsPlugin.zonedSchedule(
0,
title.tr,
message.tr,
scheduledTime,
details,
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
matchDateTimeComponents: DateTimeComponents.time,
);
});
print('Notifications scheduled every 5 seconds');
}
// Callback when the notification is tapped
void onDidReceiveNotificationResponse(NotificationResponse response) {
handleNotificationResponse(response);