flutter_workmanager
flutter_workmanager copied to clipboard
πTasks do not run on iOS at all
- [x] I have read the README
- [x] I have done the setup for Android
- [x] I have done the setup for iOS
- [x] I have ran the sample app and it does not work there
Version
| Technology | Version |
|---|---|
| Workmanager version | latest |
| Xcode version | 15.0.1 |
| Swift version | latest |
| iOS deployment target | 13 |
Describe the error
I have followed all of the setup steps, run the command in the debugger, and nothing happens. No errors, just the task does not get triggered at all. I both have the @pragma('vm:entry-point') on my task handler and listen for both the name and the iosbackroundtask enum. My initial print doesn't even get logged either...
Ideas? Is This a possible bug? Thanks.
Output of flutter doctor -v
[β] Flutter (Channel stable, 3.13.9, on macOS 14.1.1 23B81 darwin-x64, locale en-US)
β’ Flutter version 3.13.9 on channel stable at /usr/local/Caskroom/flutter/3.13.9/flutter
β’ Upstream repository https://github.com/flutter/flutter.git
β’ Framework revision d211f42860 (2 weeks ago), 2023-10-25 13:42:25 -0700
β’ Engine revision 0545f8705d
β’ Dart version 3.1.5
β’ DevTools version 2.25.0
[β] Android toolchain - develop for Android devices
β Unable to locate Android SDK.
Install Android Studio from: https://developer.android.com/studio/index.html
On first launch it will assist you in installing the Android SDK components.
(or visit https://flutter.dev/docs/get-started/install/macos#android-setup for detailed instructions).
If the Android SDK has been installed to a custom location, please use
`flutter config --android-sdk` to update to that location.
[β] Xcode - develop for iOS and macOS (Xcode 15.0.1)
β’ Xcode at /Applications/Xcode.app/Contents/Developer
β’ Build 15A507
β’ CocoaPods version 1.14.2
[β] Chrome - develop for the web (Cannot find Chrome executable at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome)
! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.
[!] Android Studio (not installed)
β’ Android Studio not found; download from https://developer.android.com/studio/index.html
(or visit https://flutter.dev/docs/get-started/install/macos#android-setup for detailed instructions).
[β] IntelliJ IDEA Ultimate Edition (version 2023.2.5)
β’ IntelliJ at /Users/sticks/Applications/IntelliJ IDEA Ultimate.app
β’ Flutter plugin version 76.3.4
β’ Dart plugin version 232.10072.19
[β] Connected device (2 available)
β’ [omitted] iPhone (mobile) β’ 00008120-001A698A01E3C01E β’ ios β’ iOS 17.0.3 21A360
β’ macOS (desktop) β’ macos β’ darwin-x64 β’ macOS 14.1.1 23B81 darwin-x64
[β] Network resources
β’ All expected network resources are available.```
@SticksDev are you still getting the error? if so, then what other versions of ios did you test it?
iOS 17.0.3 and I used your demo app, which also did not work.
Hello I have the same problem. Background task are running on IOS only if Workmanager is initialized in debug mode = true. Workmanager().initialize(callbackDispatcher, isInDebugMode: true); If isInDebugMode = false. Does not start.
I do register the oneOffTask with static method like this. On Android works. but on IOS not. static void registerOneOffTask( {required String taskKey, required String taskName, required Map<String, dynamic> inputData, required Duration initialDelay}) { try { Constraints constraints = Constraints( networkType: NetworkType.not_required, requiresBatteryNotLow: false, requiresCharging: false, requiresDeviceIdle: false, requiresStorageNotLow: false, ); if(Platform.isIOS){ Workmanager().registerOneOffTask(taskKey, taskName, initialDelay: initialDelay,constraints: constraints, inputData: inputData, existingWorkPolicy: ExistingWorkPolicy.append); }else { Workmanager().registerOneOffTask(taskKey, taskName, initialDelay: initialDelay, inputData: inputData, existingWorkPolicy: ExistingWorkPolicy.append); } if (kDebugMode) { DateTime executionTime = DateTime.now().add(initialDelay); print('Task $taskKey will be executed at $executionTime'); } } catch (e, s) { if (kDebugMode) { print('Error in registerOneOffTask'); print(e); print(s); } } }
Any updates?
I am not able to use it as well. I follow documentation in details, I even created a new project just to test this package only.
BGTaskScheduler works if I triggered by e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:@"task-identifier"], not on function call on flutter.
Background Fetch won't work or throw any erros (trying to trigger it by Xcode>debug>Simulate Background Fetch)
I have:
- [x] added Background Modes capability (Background processing and background fetch)
- [x] registered my task on AppDelegate.swift and Info.plist
background_service.dart:
import 'dart:io';
import 'package:workmanager/workmanager.dart';
@pragma(
'vm:entry-point') // Mandatory if the App is obfuscated or using Flutter 3.1+
void callbackDispatcher() {
Workmanager().executeTask((task, inputData) {
switch (task) {
case Workmanager.iOSBackgroundTask:
stderr.writeln("The iOS background fetch was triggered");
print(
"The iOS background fetch was triggered"); //simpleTask will be emitted here.
break;
default:
print(
"Native called background task: $task"); //simpleTask will be emitted here.
break;
}
return Future.value(true);
});
}
class BackgruondService {
static Future<void> initialize() async {
Workmanager().initialize(
callbackDispatcher, // The top level function, aka callbackDispatcher
isInDebugMode:
true // If enabled it will post a notification whenever the task is running. Handy for debugging tasks
);
Workmanager().registerOneOffTask("task-identifier", "simpleTask");
}
}
AppDelegate.swift:
import UIKit
import Flutter
import workmanager
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
// WORKMANAGER
UNUserNotificationCenter.current().delegate = self
WorkmanagerPlugin.setPluginRegistrantCallback { registry in
// Registry in this case is the FlutterEngine that is created in Workmanager's
// performFetchWithCompletionHandler or BGAppRefreshTask.
// This will make other plugins available during a background operation.
GeneratedPluginRegistrant.register(with: registry)
}
WorkmanagerPlugin.registerTask(withIdentifier: "task-identifier")
// background fetch
UIApplication.shared.setMinimumBackgroundFetchInterval(TimeInterval(60*15))
// WORKMANAGER
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
Info.plist:
(...)
<key>BGTaskSchedulerPermittedIdentifiers</key>
<array>
<string>task-identifier</string>
</array>
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>processing</string>
</array>
(...)
Am I missing something? Documentation does not seem quite up to date...
I moved to this PR and it seems to be working for me now https://github.com/fluttercommunity/flutter_workmanager/pull/511
Commenting now on multiple issues while debugging the issue with plugins on iOS background isolates, but it finally seems that the missing piece was this:
// AppDelegate.swift
GeneratedPluginRegistrant.register(with: self)
WorkmanagerPlugin.setPluginRegistrantCallback { registry in
GeneratedPluginRegistrant.register(with: registry)
}
With the above added (after GeneratedPluginRegistrant.register(with: self), I'm finally able to call SharedPreferences and also the Firebase.initializeApp() successfully π
I did not find this documented anywhere, I'll try to find a suitable place from the official README.
Commenting now on multiple issues while debugging the issue with plugins on iOS background isolates, but it finally seems that the missing piece was this:
// AppDelegate.swift GeneratedPluginRegistrant.register(with: self) WorkmanagerPlugin.setPluginRegistrantCallback { registry in GeneratedPluginRegistrant.register(with: registry) }With the above added (after
GeneratedPluginRegistrant.register(with: self), I'm finally able to callSharedPreferencesand also theFirebase.initializeApp()successfully πI did not find this documented anywhere, I'll try to find a suitable place from the official README.
It worked.
Commenting now on multiple issues while debugging the issue with plugins on iOS background isolates, but it finally seems that the missing piece was this:
// AppDelegate.swift GeneratedPluginRegistrant.register(with: self) WorkmanagerPlugin.setPluginRegistrantCallback { registry in GeneratedPluginRegistrant.register(with: registry) }With the above added (after
GeneratedPluginRegistrant.register(with: self), I'm finally able to callSharedPreferencesand also theFirebase.initializeApp()successfully πI did not find this documented anywhere, I'll try to find a suitable place from the official README.
It worked, thank u