From 6fb634503d34451560fb1f22e07ec22c6a60e2ad Mon Sep 17 00:00:00 2001 From: Burhan Date: Fri, 21 Jun 2024 19:25:37 +0300 Subject: [PATCH 1/4] Update pubspec.yaml Include workmanager to use WorkmanagerPlugin.registerPeriodicTask --- pubspec.yaml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index f3162ba..ce802e0 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -61,7 +61,12 @@ dependencies: sqflite: ^2.3.2 path: ^1.9.0 flutter_local_notifications: ^17.0.0 - workmanager: ^0.5.2 + + # We are including workmanager this way to use WorkmanagerPlugin.registerPeriodicTask + workmanager: + git: + url: https://github.com/fluttercommunity/flutter_workmanager.git + ref: b783000 percent_indicator: ^4.2.3 liquid_pull_to_refresh: ^3.0.1 hijri: ^3.0.0 From 9f2c806702c24e3be7af0d9d76d563c582a5e49f Mon Sep 17 00:00:00 2001 From: Burhan Date: Fri, 21 Jun 2024 19:30:35 +0300 Subject: [PATCH 2/4] Update AppDelegate.swift Set up registerPeriodicTask to run every 2 days on iOS as on Android" --- ios/Runner/AppDelegate.swift | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/ios/Runner/AppDelegate.swift b/ios/Runner/AppDelegate.swift index ec076fb..5416d96 100644 --- a/ios/Runner/AppDelegate.swift +++ b/ios/Runner/AppDelegate.swift @@ -10,12 +10,17 @@ import workmanager ) -> Bool { GeneratedPluginRegistrant.register(with: self) - UIApplication.shared.setMinimumBackgroundFetchInterval(TimeInterval(60*15)) - WorkmanagerPlugin.registerTask(withIdentifier: "io.nedaa.schedule") + // Just like we implemented for Android, we have set up registerPeriodicTask to run every 2 days on iOS as well. + WorkmanagerPlugin.setPluginRegistrantCallback { registry in + GeneratedPluginRegistrant.register(with: registry) + } + + WorkmanagerPlugin.registerPeriodicTask(withIdentifier: "io.nedaa.schedule", frequency: NSNumber(value: 172800)) + if #available(iOS 10.0, *) { UNUserNotificationCenter.current().delegate = self as UNUserNotificationCenterDelegate } return super.application(application, didFinishLaunchingWithOptions: launchOptions) } -} \ No newline at end of file +} From d1f1eda0f41bc2d013c451a4425ad84f533f205e Mon Sep 17 00:00:00 2001 From: Burhan Date: Fri, 21 Jun 2024 19:37:53 +0300 Subject: [PATCH 3/4] Update main.dart Refactor callbackDispatcher to handle the same task for both Android and iOS --- lib/main.dart | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index ce0ace0..335c736 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -28,15 +28,14 @@ import 'package:workmanager/workmanager.dart'; const taskId = 'io.nedaa.schedule'; String appVersion = ""; +// This dispatcher is set up to execute the same task for both Android and iOS. +// The task identified by 'taskId' will be executed whenever it is triggered. @pragma('vm:entry-point') void callbackDispatcher() { Workmanager().executeTask((task, inputData) async { try { - if (task == taskId && Platform.isAndroid) { - debugPrint('android task $task'); - await _task(); - } else if (task == Workmanager.iOSBackgroundTask) { - debugPrint('iOSBackgroundTask $task'); + if (task == taskId) { + debugPrint('Executing scheduled task: $task'); await _task(); } } catch (e) { @@ -114,13 +113,14 @@ void main() async { false // If enabled it will post a notification whenever the task is running. Handy for debugging tasks ); await Workmanager().cancelAll(); - if (Platform.isAndroid) { - Workmanager().registerPeriodicTask( - taskId, - taskId, - frequency: const Duration(days: 2), - ); - } + + // The periodic task registration for Android has been removed as the same task is now handled uniformly for both Android and iOS. + Workmanager().registerPeriodicTask( + taskId, + taskId, + frequency: const Duration(days: 2), + ); + // Wait 0.5 seconds before removing the splash screen await Future.delayed(const Duration(milliseconds: 500)); From 73fa4d0b33f26143a709884cda37af0b67583de4 Mon Sep 17 00:00:00 2001 From: Burhan Date: Sat, 22 Jun 2024 00:23:51 +0300 Subject: [PATCH 4/4] Update main.dart --- lib/main.dart | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 335c736..d07cb46 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,5 +1,3 @@ -import 'dart:io'; - import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; @@ -120,7 +118,6 @@ void main() async { taskId, frequency: const Duration(days: 2), ); - // Wait 0.5 seconds before removing the splash screen await Future.delayed(const Duration(milliseconds: 500));