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 +} diff --git a/lib/main.dart b/lib/main.dart index ce0ace0..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'; @@ -28,15 +26,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 +111,13 @@ 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)); 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