From 27ef942723d45b3c8b72631fcd23238f816ed8ec Mon Sep 17 00:00:00 2001
From: hihihaha <jkristof69420@gmail.com>
Date: Sat, 10 Jun 2023 21:46:53 +0200
Subject: [PATCH] fix headless task

---
 filcnaplo/lib/main.dart                       | 10 ++++--
 .../screens/navigation/navigation_screen.dart | 35 -------------------
 2 files changed, 8 insertions(+), 37 deletions(-)

diff --git a/filcnaplo/lib/main.dart b/filcnaplo/lib/main.dart
index 0d18794..1131ffb 100644
--- a/filcnaplo/lib/main.dart
+++ b/filcnaplo/lib/main.dart
@@ -136,14 +136,20 @@ Widget errorBuilder(FlutterErrorDetails details) {
     ), (String taskId) async {  // <-- Event handler
       print("[BackgroundFetch] Event received $taskId");
       NotificationsHelper().backgroundJob();
-     
       BackgroundFetch.finish(taskId);
     }, (String taskId) async {  // <-- Task timeout handler.
       print("[BackgroundFetch] TASK TIMEOUT taskId: $taskId");
       BackgroundFetch.finish(taskId);
     });
     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')
diff --git a/filcnaplo_mobile_ui/lib/screens/navigation/navigation_screen.dart b/filcnaplo_mobile_ui/lib/screens/navigation/navigation_screen.dart
index 4ceed6d..9f86529 100755
--- a/filcnaplo_mobile_ui/lib/screens/navigation/navigation_screen.dart
+++ b/filcnaplo_mobile_ui/lib/screens/navigation/navigation_screen.dart
@@ -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
   void initState() {
     super.initState();
 
-    initPlatformState();
 
     HomeWidget.setAppGroupId('hu.refilc.naplo.group');