????
This commit is contained in:
parent
eec578b9c8
commit
a057d91493
|
@ -1,4 +1,7 @@
|
|||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
|
||||
<uses-permission android:name="android.permission.VIBRATE" />
|
||||
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||
<application
|
||||
android:label="hrt"
|
||||
android:name="${applicationName}"
|
||||
|
|
|
@ -1,21 +1,8 @@
|
|||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
import 'package:timezone/data/latest.dart';
|
||||
import 'package:timezone/timezone.dart';
|
||||
|
||||
void sendNotification() async {
|
||||
var initialized = await FlutterLocalNotificationsPlugin().initialize(
|
||||
const InitializationSettings(
|
||||
android: AndroidInitializationSettings('estrogen'),
|
||||
),
|
||||
);
|
||||
|
||||
if (initialized != true) {
|
||||
throw "Not initialized";
|
||||
}
|
||||
|
||||
await FlutterLocalNotificationsPlugin().show(
|
||||
1,
|
||||
null,
|
||||
null,
|
||||
const NotificationDetails(
|
||||
const details = NotificationDetails(
|
||||
android: AndroidNotificationDetails(
|
||||
'hrt',
|
||||
'HRT',
|
||||
|
@ -27,6 +14,72 @@ void sendNotification() async {
|
|||
ongoing: true,
|
||||
actions: [AndroidNotificationAction("confirmation", "Done!")],
|
||||
),
|
||||
);
|
||||
|
||||
void _initializePlugin() async {
|
||||
var initialized = await FlutterLocalNotificationsPlugin().initialize(
|
||||
const InitializationSettings(
|
||||
android: AndroidInitializationSettings('estrogen'),
|
||||
),
|
||||
);
|
||||
|
||||
if (initialized != true) {
|
||||
throw "Not initialized";
|
||||
}
|
||||
}
|
||||
|
||||
void sendNotification() async {
|
||||
_initializePlugin();
|
||||
await FlutterLocalNotificationsPlugin().show(
|
||||
1,
|
||||
null,
|
||||
null,
|
||||
details,
|
||||
);
|
||||
}
|
||||
|
||||
void scheduleNotification() async {
|
||||
_initializePlugin();
|
||||
initializeTimeZones();
|
||||
|
||||
// [Hours, Minutes]
|
||||
var times = [
|
||||
[12, 00],
|
||||
[18, 00],
|
||||
];
|
||||
|
||||
if (times.isEmpty) return;
|
||||
|
||||
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
|
||||
for (var time in notificationTimes
|
||||
.where((element) => element.compareTo(currentTime) < 0)) {
|
||||
time.add(const Duration(days: 1));
|
||||
}
|
||||
|
||||
// Sort the array so we can find the closest time
|
||||
notificationTimes.sort((a, b) => a.compareTo(b));
|
||||
|
||||
await FlutterLocalNotificationsPlugin().zonedSchedule(
|
||||
1,
|
||||
null,
|
||||
null,
|
||||
notificationTimes[0],
|
||||
details,
|
||||
uiLocalNotificationDateInterpretation:
|
||||
UILocalNotificationDateInterpretation.absoluteTime,
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue