hrt/lib/tasks.dart
2023-10-06 15:21:28 +02:00

51 lines
1.4 KiB
Dart

import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:hrt/notification.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:workmanager/workmanager.dart';
@pragma('vm:entry-point')
void callbackDispatcher() {
Workmanager().executeTask((taskName, inputData) async {
print("Native called background task: $taskName");
if (taskName == 'scheduleNotifications') {
scheduleNotification();
}
return true;
});
}
void setupBackgroundTasks() async {
WidgetsFlutterBinding.ensureInitialized();
await Workmanager().initialize(
callbackDispatcher,
isInDebugMode: kDebugMode,
);
await Permission.notification.request();
await FlutterLocalNotificationsPlugin().initialize(
const InitializationSettings(
android: AndroidInitializationSettings('estrogen'),
),
);
// Runs every 15 minutes
await Workmanager().registerPeriodicTask(
'schedule_notifications',
'scheduleNotifications',
constraints: Constraints(
networkType: NetworkType.not_required,
requiresCharging: false,
requiresBatteryNotLow: false,
requiresDeviceIdle: false,
requiresStorageNotLow: false,
),
initialDelay: const Duration(seconds: 15),
frequency: const Duration(minutes: 15),
);
}