-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement new feature to add daily reminders.
- Loading branch information
Showing
14 changed files
with
452 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
lib/features/notes/data/repositories/notifications_repository.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import 'package:dairy_app/core/logger/logger.dart'; | ||
import 'package:dairy_app/features/notes/domain/repositories/notifications_repository.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_local_notifications/flutter_local_notifications.dart'; | ||
import 'package:flutter_timezone/flutter_timezone.dart'; | ||
import 'package:timezone/data/latest_all.dart' as tz; | ||
import 'package:timezone/timezone.dart' as tz; | ||
|
||
final log = printer("NotificationsRepository"); | ||
|
||
class NotificationsRepository implements INotificationsRepository { | ||
late FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin; | ||
|
||
NotificationsRepository({required this.flutterLocalNotificationsPlugin}); | ||
|
||
tz.TZDateTime nextInstanceOfTime(TimeOfDay time, tz.Location localTimeZOne) { | ||
final tz.TZDateTime now = tz.TZDateTime.now(localTimeZOne); | ||
|
||
tz.TZDateTime scheduledDate = tz.TZDateTime( | ||
tz.local, now.year, now.month, now.day, time.hour, time.minute); | ||
if (scheduledDate.isBefore(now)) { | ||
scheduledDate = scheduledDate.add(const Duration(days: 1)); | ||
} | ||
|
||
log.i("Scheduling alarm at $scheduledDate"); | ||
return scheduledDate; | ||
} | ||
|
||
@override | ||
Future<void> zonedScheduleNotification(TimeOfDay time) async { | ||
try { | ||
final permissionsEnabled = await areNotificationsEnabled(); | ||
|
||
log.w("permissions enabled = $permissionsEnabled"); | ||
|
||
if (!permissionsEnabled) { | ||
// We can request permission from within the App for >= Android 13 | ||
final arePermissionsGranted = await requestPermission(); | ||
if (!arePermissionsGranted) { | ||
throw Exception("Notification permissions are not enabled"); | ||
} | ||
} | ||
|
||
// inititalize time zones | ||
tz.initializeTimeZones(); | ||
final String? timeZoneName = await FlutterTimezone.getLocalTimezone(); | ||
tz.setLocalLocation(tz.getLocation(timeZoneName!)); | ||
|
||
log.i("Local timezone = ${tz.local}"); | ||
|
||
// cancel all previously scheduled notifications before scheduling new ones | ||
cancelAllNotifications(); | ||
|
||
await flutterLocalNotificationsPlugin.zonedSchedule( | ||
0, | ||
'Time to Journal!', | ||
'Take a few minutes to reflect on your day in your diary', | ||
nextInstanceOfTime(time, tz.local), | ||
const NotificationDetails( | ||
android: AndroidNotificationDetails( | ||
'daily_reminder', | ||
'Daily reminders', | ||
importance: Importance.high, | ||
channelDescription: 'Daily Reminder Notifications', | ||
), | ||
), | ||
androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle, | ||
matchDateTimeComponents: DateTimeComponents.time, | ||
uiLocalNotificationDateInterpretation: | ||
UILocalNotificationDateInterpretation.absoluteTime); | ||
} catch (e) { | ||
log.e(e); | ||
rethrow; | ||
} | ||
} | ||
|
||
Future<bool> areNotificationsEnabled() async { | ||
final bool granted = await flutterLocalNotificationsPlugin | ||
.resolvePlatformSpecificImplementation< | ||
AndroidFlutterLocalNotificationsPlugin>() | ||
?.areNotificationsEnabled() ?? | ||
false; | ||
return granted; | ||
} | ||
|
||
Future<bool> requestPermission() async { | ||
final AndroidFlutterLocalNotificationsPlugin? androidImplementation = | ||
flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation< | ||
AndroidFlutterLocalNotificationsPlugin>(); | ||
|
||
final bool grantedNotificationPermission = | ||
await androidImplementation?.requestNotificationsPermission() ?? false; | ||
|
||
return grantedNotificationPermission; | ||
} | ||
|
||
@override | ||
Future<void> cancelAllNotifications() async { | ||
log.i("Cancelling all scheduled notifications"); | ||
await flutterLocalNotificationsPlugin.cancelAll(); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
lib/features/notes/domain/repositories/notifications_repository.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
abstract class INotificationsRepository { | ||
/// Schedules daily notification at [time] | ||
Future<void> zonedScheduleNotification(TimeOfDay time); | ||
|
||
/// Cancels/removes all notifications that have been scheduled and those | ||
/// that have already been presented. | ||
Future<void> cancelAllNotifications(); | ||
} |
Oops, something went wrong.