fix headless task

This commit is contained in:
hihihaha 2023-06-10 21:46:53 +02:00
parent fe03554fbf
commit 27ef942723
2 changed files with 8 additions and 37 deletions

View File

@ -136,14 +136,20 @@ Widget errorBuilder(FlutterErrorDetails details) {
), (String taskId) async { // <-- Event handler ), (String taskId) async { // <-- Event handler
print("[BackgroundFetch] Event received $taskId"); print("[BackgroundFetch] Event received $taskId");
NotificationsHelper().backgroundJob(); NotificationsHelper().backgroundJob();
BackgroundFetch.finish(taskId); BackgroundFetch.finish(taskId);
}, (String taskId) async { // <-- Task timeout handler. }, (String taskId) async { // <-- Task timeout handler.
print("[BackgroundFetch] TASK TIMEOUT taskId: $taskId"); print("[BackgroundFetch] TASK TIMEOUT taskId: $taskId");
BackgroundFetch.finish(taskId); BackgroundFetch.finish(taskId);
}); });
print('[BackgroundFetch] configure success: $status'); print('[BackgroundFetch] configure success: $status');
BackgroundFetch.scheduleTask(TaskConfig(
taskId: "com.transistorsoft.refilcnotification",
delay: 900000, // 15 minutes
periodic: true,
forceAlarmManager: true,
stopOnTerminate: false,
enableHeadless: true
));
} }
@pragma('vm:entry-point') @pragma('vm:entry-point')

View File

@ -95,45 +95,10 @@ class NavigationScreenState extends State<NavigationScreen>
} }
} }
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
// Configure BackgroundFetch.
int status = await BackgroundFetch.configure(
BackgroundFetchConfig(
minimumFetchInterval: 15,
stopOnTerminate: false,
enableHeadless: true,
requiresBatteryNotLow: false,
requiresCharging: false,
requiresStorageNotLow: false,
requiresDeviceIdle: false,
requiredNetworkType: NetworkType.ANY), (String taskId) async {
// <-- Event handler
// This is the fetch-event callback.
print("[BackgroundFetch] Event received $taskId");
// IMPORTANT: You must signal completion of your task or the OS can punish your app
// for taking too long in the background.
BackgroundFetch.finish(taskId);
}, (String taskId) async {
// <-- Task timeout handler.
// This task has exceeded its allowed running-time. You must stop what you're doing and immediately .finish(taskId)
print("[BackgroundFetch] TASK TIMEOUT taskId: $taskId");
BackgroundFetch.finish(taskId);
});
print('[BackgroundFetch] configure success: $status');
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
}
@override @override
void initState() { void initState() {
super.initState(); super.initState();
initPlatformState();
HomeWidget.setAppGroupId('hu.refilc.naplo.group'); HomeWidget.setAppGroupId('hu.refilc.naplo.group');