Open SticksDev opened 7 months ago
@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:
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 callSharedPreferences
and 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 callSharedPreferences
and 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
Version
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