59 lines
1.7 KiB
Dart
59 lines
1.7 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') {
|
|
sendNotification();
|
|
}
|
|
|
|
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'),
|
|
),
|
|
// onSelectNotification: handleNotificationClick,
|
|
);
|
|
|
|
var launchDetails =
|
|
await FlutterLocalNotificationsPlugin().getNotificationAppLaunchDetails();
|
|
|
|
// if (launchDetails?.payload != null && launchDetails!.payload!.isNotEmpty) {
|
|
// handleNotificationClick(launchDetails.payload);
|
|
// }
|
|
|
|
// 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),
|
|
);
|
|
}
|