// import 'package:flutter_local_notifications/flutter_local_notifications.dart'; // import 'package:timezone/timezone.dart' as tz; // import 'package:timezone/data/latest_all.dart' as tz; // import 'dart:developer'; // // class SendRidesNotification { // final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = // FlutterLocalNotificationsPlugin(); // // final Set _activeScheduledNotificationIds = {}; // // Set get activeScheduledNotificationIds => _activeScheduledNotificationIds; // // Future initNotifications() async { // // Android setup // const AndroidInitializationSettings initializationSettingsAndroid = // AndroidInitializationSettings('@mipmap/ic_launcher'); // // // iOS setup // final DarwinInitializationSettings initializationSettingsDarwin = // DarwinInitializationSettings( // onDidReceiveLocalNotification: (id, title, body, payload) async { // log('iOS notification received: $title'); // }, // ); // // final InitializationSettings initializationSettings = InitializationSettings( // android: initializationSettingsAndroid, // iOS: initializationSettingsDarwin, // ); // // await flutterLocalNotificationsPlugin.initialize( // initializationSettings, // onDidReceiveNotificationResponse: (NotificationResponse response) async { // log('Notification tapped: ${response.payload}'); // }, // ); // // // Request permissions // await requestPermissions(); // } // // Future requestPermissions() async { // // For Android 13+ and iOS, use requestPermission method // final AndroidFlutterLocalNotificationsPlugin? androidImplementation = // flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation< // AndroidFlutterLocalNotificationsPlugin>(); // // if (androidImplementation != null) { // // For Android, request permissions for notifications and post notifications. // // Note: canRequestNotificationsPermissions() and requestNotificationsPermissions() // // are not directly exposed or are deprecated. The plugin's requestPermission() // // method handles this more broadly for both platforms. // final bool? granted = await androidImplementation.requestPermission(); // if (granted != null && granted) { // log('Android notification permissions granted.'); // } else { // log('Android notification permissions denied.'); // } // } // // final IOSFlutterLocalNotificationsPlugin? iOSImplementation = // flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation< // IOSFlutterLocalNotificationsPlugin>(); // // if (iOSImplementation != null) { // final bool? granted = await iOSImplementation.requestPermissions( // alert: true, // badge: true, // sound: true, // ); // if (granted != null && granted) { // log('iOS notification permissions granted.'); // } else { // log('iOS notification permissions denied.'); // } // } // } // // Future scheduleRideNotification({ // required int rideId, // required String title, // required String body, // required DateTime scheduleDateTime, // String? payload, // }) async { // final notificationTime = scheduleDateTime.subtract(const Duration(minutes: 30)); // // if (notificationTime.isBefore(DateTime.now())) { // log('Notification time for ride $rideId is in the past ($notificationTime), not scheduling.'); // return; // } // // // Ensure timezone data is initialized and local location is set // // This should ideally be done once in main.dart // if (!tz.local.isInitialized) { // Corrected: tz.local.isInitialized // tz.initializeAll(); // tz.setLocalLocation(tz.getLocation('Europe/Paris')); // IMPORTANT: Set your app's default timezone (e.g., 'Africa/Tunis' for Sousse) // } // // // await flutterLocalNotificationsPlugin.zonedSchedule( // rideId, // title, // body, // tz.TZDateTime.from(notificationTime, tz.local), // const NotificationDetails( // android: AndroidNotificationDetails( // 'wanisdriver_schedule_channel', // 'Scheduled Ride Reminders', // channelDescription: 'Notifications for upcoming scheduled rides', // importance: Importance.max, // priority: Priority.high, // ticker: 'ticker', // ), // iOS: DarwinNotificationDetails( // sound: 'default.wav', // presentAlert: true, // presentBadge: true, // presentSound: true, // ), // ), // androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle, // uiLocalNotificationDateInterpretation: // UILocalNotificationDateInterpretation.absoluteTime, // payload: payload ?? 'ride_id_$rideId', // ); // _activeScheduledNotificationIds.add(rideId); // log('Notification scheduled for ride $rideId at $notificationTime'); // } // // Future cancelRideNotification(int rideId) async { // await flutterLocalNotificationsPlugin.cancel(rideId); // _activeScheduledNotificationIds.remove(rideId); // log('Notification for ride $rideId cancelled.'); // } // // Future clearAllPendingRides() async { // // This clears ALL pending notifications. // // If you need more selective clearing, you'd iterate through _activeScheduledNotificationIds // // and cancel them one by one. // await flutterLocalNotificationsPlugin.cancelAll(); // _activeScheduledNotificationIds.clear(); // log('All pending notifications cleared.'); // } // }