2023-10-06 12:18:35 +00:00
|
|
|
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') {
|
2023-10-06 12:49:56 +00:00
|
|
|
scheduleNotification();
|
2023-10-06 12:18:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
|
|
|
);
|
|
|
|
}
|