2023-10-06 13:21:28 +00:00
|
|
|
// ignore_for_file: avoid_print
|
|
|
|
|
2023-10-05 17:33:47 +00:00
|
|
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
2023-10-06 13:21:28 +00:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2023-10-06 12:43:04 +00:00
|
|
|
import 'package:timezone/data/latest.dart';
|
|
|
|
import 'package:timezone/timezone.dart';
|
2023-10-05 17:33:47 +00:00
|
|
|
|
2023-10-06 12:43:04 +00:00
|
|
|
const details = NotificationDetails(
|
|
|
|
android: AndroidNotificationDetails(
|
|
|
|
'hrt',
|
|
|
|
'HRT',
|
|
|
|
autoCancel: false,
|
|
|
|
styleInformation: BigPictureStyleInformation(
|
|
|
|
DrawableResourceAndroidBitmap('take_your_hrt'),
|
|
|
|
),
|
|
|
|
importance: Importance.max,
|
|
|
|
ongoing: true,
|
|
|
|
actions: [AndroidNotificationAction("confirmation", "Done!")],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
void _initializePlugin() async {
|
2023-10-05 17:33:47 +00:00
|
|
|
var initialized = await FlutterLocalNotificationsPlugin().initialize(
|
|
|
|
const InitializationSettings(
|
|
|
|
android: AndroidInitializationSettings('estrogen'),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
if (initialized != true) {
|
|
|
|
throw "Not initialized";
|
|
|
|
}
|
2023-10-06 12:43:04 +00:00
|
|
|
}
|
2023-10-05 17:33:47 +00:00
|
|
|
|
2023-10-06 12:43:04 +00:00
|
|
|
void sendNotification() async {
|
|
|
|
_initializePlugin();
|
2023-10-05 17:33:47 +00:00
|
|
|
await FlutterLocalNotificationsPlugin().show(
|
|
|
|
1,
|
|
|
|
null,
|
|
|
|
null,
|
2023-10-06 12:43:04 +00:00
|
|
|
details,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void scheduleNotification() async {
|
|
|
|
_initializePlugin();
|
|
|
|
initializeTimeZones();
|
|
|
|
|
|
|
|
// [Hours, Minutes]
|
|
|
|
var times = [
|
|
|
|
[12, 00],
|
|
|
|
[18, 00],
|
|
|
|
];
|
|
|
|
|
|
|
|
if (times.isEmpty) return;
|
|
|
|
|
2023-10-06 13:21:28 +00:00
|
|
|
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
2023-10-06 12:43:04 +00:00
|
|
|
var currentTime = TZDateTime.now(local);
|
|
|
|
|
|
|
|
// Map the time entries to TZDateTime objects
|
|
|
|
var notificationTimes = times
|
|
|
|
.map((item) => TZDateTime(
|
|
|
|
local,
|
|
|
|
currentTime.year,
|
|
|
|
currentTime.month,
|
|
|
|
currentTime.day,
|
|
|
|
item[0],
|
|
|
|
item[1],
|
|
|
|
))
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
// For all entries in the past, add 1 day
|
2023-10-06 12:49:56 +00:00
|
|
|
for (var time
|
|
|
|
in notificationTimes.where((time) => time.compareTo(currentTime) < 0)) {
|
|
|
|
notificationTimes.remove(time);
|
|
|
|
notificationTimes.add(time.add(const Duration(days: 1)));
|
2023-10-06 12:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the array so we can find the closest time
|
|
|
|
notificationTimes.sort((a, b) => a.compareTo(b));
|
2023-10-06 13:21:28 +00:00
|
|
|
var notificationTime = notificationTimes[0];
|
|
|
|
|
|
|
|
if (prefs.getString('scheduled_notification') ==
|
|
|
|
notificationTime.toIso8601String()) {
|
|
|
|
print("Notification already scheduled for "
|
|
|
|
"${notificationTime.toIso8601String()}, not re-scheduling");
|
|
|
|
return;
|
|
|
|
}
|
2023-10-06 12:43:04 +00:00
|
|
|
|
2023-10-06 13:21:28 +00:00
|
|
|
print("Scheduling notification for ${notificationTime.toIso8601String()}");
|
2023-10-06 12:49:56 +00:00
|
|
|
|
2023-10-06 12:43:04 +00:00
|
|
|
await FlutterLocalNotificationsPlugin().zonedSchedule(
|
|
|
|
1,
|
|
|
|
null,
|
|
|
|
null,
|
2023-10-06 13:21:28 +00:00
|
|
|
notificationTime,
|
2023-10-06 12:43:04 +00:00
|
|
|
details,
|
|
|
|
uiLocalNotificationDateInterpretation:
|
|
|
|
UILocalNotificationDateInterpretation.absoluteTime,
|
2023-10-05 17:33:47 +00:00
|
|
|
);
|
2023-10-06 13:21:28 +00:00
|
|
|
await prefs.setString(
|
|
|
|
'scheduled_notification', notificationTime.toIso8601String());
|
2023-10-05 17:33:47 +00:00
|
|
|
}
|