|
| 1 | +import 'package:timezone/data/latest_all.dart' as tz; |
| 2 | +import 'package:timezone/timezone.dart' as tz; |
| 3 | +import 'ImportAll.dart'; |
| 4 | + |
| 5 | +final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = |
| 6 | + FlutterLocalNotificationsPlugin(); |
| 7 | + |
| 8 | +Future<void> requestNotificationPermissions() async { |
| 9 | + final bool? granted = await flutterLocalNotificationsPlugin |
| 10 | + .resolvePlatformSpecificImplementation< |
| 11 | + AndroidFlutterLocalNotificationsPlugin>() |
| 12 | + ?.requestNotificationsPermission(); |
| 13 | + |
| 14 | + if (granted == true) { |
| 15 | + print("Notification permissions granted."); |
| 16 | + } else { |
| 17 | + print("Notification permissions denied."); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +Future<void> initializeNotifications() async { |
| 22 | + const AndroidInitializationSettings initializationSettingsAndroid = |
| 23 | + AndroidInitializationSettings('@mipmap/ic_launcher'); |
| 24 | + |
| 25 | + const InitializationSettings initializationSettings = InitializationSettings( |
| 26 | + android: initializationSettingsAndroid, |
| 27 | + ); |
| 28 | + |
| 29 | + await flutterLocalNotificationsPlugin.initialize( |
| 30 | + initializationSettings, |
| 31 | + onDidReceiveNotificationResponse: (NotificationResponse response) { |
| 32 | + print('Notification Clicked!'); |
| 33 | + AuthWrapper(); |
| 34 | + }, |
| 35 | + // onDidReceiveBackgroundNotificationResponse: (NotificationResponse response) { |
| 36 | + // print('Background Notification Clicked!'); |
| 37 | + // }, |
| 38 | + ); |
| 39 | +} |
| 40 | + |
| 41 | +Future<void> scheduleDailyNotification() async { |
| 42 | + final tz.TZDateTime now = tz.TZDateTime.now(tz.local); |
| 43 | + final tz.TZDateTime scheduledTime = tz.TZDateTime( |
| 44 | + tz.local, |
| 45 | + now.year, |
| 46 | + now.month, |
| 47 | + now.day, |
| 48 | + 15, |
| 49 | + 45, |
| 50 | + ); |
| 51 | + |
| 52 | + final tz.TZDateTime adjustedScheduledTime = scheduledTime.isBefore(now) |
| 53 | + ? scheduledTime.add(Duration(days: 1)) |
| 54 | + : scheduledTime; |
| 55 | + |
| 56 | + const AndroidNotificationDetails androidNotificationDetails = |
| 57 | + AndroidNotificationDetails( |
| 58 | + 'MoneyMate', |
| 59 | + 'Daily Reminder', |
| 60 | + channelDescription: 'Daily notifications at a fixed time', |
| 61 | + importance: Importance.high, |
| 62 | + priority: Priority.high, |
| 63 | + color: Color(0xFF42A5F5), |
| 64 | + // Custom notification color |
| 65 | + playSound: true, |
| 66 | + enableLights: true, |
| 67 | + enableVibration: true, |
| 68 | + visibility: NotificationVisibility.public, |
| 69 | + // Make it visible on the lock screen |
| 70 | + timeoutAfter: 10000, |
| 71 | + // Timeout after 10 seconds |
| 72 | + actions: <AndroidNotificationAction>[ |
| 73 | + AndroidNotificationAction( |
| 74 | + 'action_id_1', |
| 75 | + 'Mark as Done', // Action button to mark as done |
| 76 | + ), |
| 77 | + AndroidNotificationAction( |
| 78 | + 'action_id_2', |
| 79 | + 'Dismiss', // Action button to dismiss |
| 80 | + ), |
| 81 | + ], |
| 82 | + ); |
| 83 | + |
| 84 | + const NotificationDetails notificationDetails = |
| 85 | + NotificationDetails(android: androidNotificationDetails); |
| 86 | + |
| 87 | + await flutterLocalNotificationsPlugin.zonedSchedule( |
| 88 | + 0, |
| 89 | + 'MoneyMate', |
| 90 | + 'This is your daily reminder at 11 PM.', |
| 91 | + adjustedScheduledTime, |
| 92 | + notificationDetails, |
| 93 | + uiLocalNotificationDateInterpretation: |
| 94 | + UILocalNotificationDateInterpretation.absoluteTime, |
| 95 | + matchDateTimeComponents: DateTimeComponents.time, |
| 96 | + androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle, |
| 97 | + ); |
| 98 | +} |
0 commit comments