Skip to content
This repository was archived by the owner on Jun 19, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
25 changes: 11 additions & 14 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
Expand Down
7 changes: 6 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down