我正在尝试在ios上使用workermanger和flutter来了解后台服务。 这就是我的清单
现在这是我在 xcode 上的 AppDelegate:
import UIKit
import Flutter
import workmanager
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(.alert) // shows banner even if app is in foreground
}
override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
WorkmanagerPlugin.registerTask(withIdentifier: "simpleTask")
UIApplication.shared.setMinimumBackgroundFetchInterval(TimeInterval(60*15))
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
现在这是我的 Flutter 代码
import 'package:workmanager/workmanager.dart';
@pragma('vm:entry-point')
void callbackDispatcher() {
Workmanager().executeTask((task, inputData) {
switch (task) {
case Workmanager.iOSBackgroundTask:
print("The iOS background fetch was triggered");
break;
}
bool success = true;
return Future.value(success);
});
}
void main() {
WidgetsFlutterBinding.ensureInitialized();
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("simpleTask", "simpleTask");
// Workmanager().registerPeriodicTask("simpleTask", "simpleTask");
runApp(const MyApp());
}
我收到的错误是这样的:
Could not schedule app refresh: Error Domain=BGTaskSchedulerErrorDomain Code=3 "(null)"
你能告诉我我迷路的地方吗?也许关于文档,我无法理解我向你寻求帮助。
我遇到了同样的错误,但没有找到解决方案。 info-plist 和应用程序委托中的任务名称都是正确的。如果你找到一些解决方案。你能写信吗