Flutter:使用 workmanager 的定期后台任务在 IOS 上不起作用

Flutter: periodic background task using workmanager is not working on IOS

我正在使用 Flutter 构建 IOS 和 Android 应用程序。我的应用程序在关闭时需要 运行 后台任务(假设大约每 15 分钟一次)。然后显示通知。我正在关注这篇文章,https://www.geeksforgeeks.org/background-local-notifications-in-flutter/?fbclid=IwAR0sKCVATW068AxuRIyDO693vcqwU0VTPVl1bgVkM3J3EGCHExv8P13dvz4. So, I am using this library, https://pub.dev/packages/workmanager。它适用于 Android,但不适用于 IOS。

这是我的代码。

void main() {
  // needed if you intend to initialize in the `main` function
  WidgetsFlutterBinding.ensureInitialized();
  Workmanager().initialize(

    // The top level function, aka callbackDispatcher
      callbackDispatcher,

      // If enabled it will post a notification whenever
      // the task is running. Handy for debugging tasks
      isInDebugMode: true
  );
  // Periodic task registration
  Workmanager().registerPeriodicTask(
    "2",

    //This is the value that will be
    // returned in the callbackDispatcher
    "simplePeriodicTask",

    // When no frequency is provided
    // the default 15 minutes is set.
    // Minimum frequency is 15 min.
    // Android will automatically change
    // your frequency to 15 min
    // if you have configured a lower frequency.
    frequency: const Duration(minutes: 15),
  );

  runApp(const MyApp());
}

void callbackDispatcher() {
  Workmanager().executeTask((task, inputData) {

    // initialise the plugin of flutterlocalnotifications.
    FlutterLocalNotificationsPlugin flip = FlutterLocalNotificationsPlugin();

    // app_icon needs to be a added as a drawable
    // resource to the Android head project.
    var android = const AndroidInitializationSettings('@mipmap/ic_launcher');
    var ios = const IOSInitializationSettings();

    // initialise settings for both Android and iOS device.
    var settings = InitializationSettings(android: android, iOS: ios);
    flip.initialize(settings);
    _showNotificationWithDefaultSound(flip);
    return Future.value(true);
  });
}

Future _showNotificationWithDefaultSound(flip) async {
  var androidPlatformChannelSpecifics = const AndroidNotificationDetails("your channel id", "my channel name");
  var iOSPlatformChannelSpecifics = const IOSNotificationDetails();

  // initialise channel platform for both Android and iOS device.
  var platformChannelSpecifics = NotificationDetails(android: androidPlatformChannelSpecifics, iOS: iOSPlatformChannelSpecifics);

  await flip.show(0, 'GeeksforGeeks',
      'Your are one step away to connect with GeeksforGeeks',
      platformChannelSpecifics, payload: 'Default_Sound'
  );
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

正如我所提到的,它按预期工作并在 Android 上显示通知。当我在 IOS 模拟器上 运行 它时,出现以下错误。

An Observatory debugger and profiler on iPhone 12 Pro Max is available at: http://127.0.0.1:56183/fjCBE_KZDpw=/
[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: PlatformException(unhandledMethod("registerPeriodicTask") error, Unhandled method registerPeriodicTask, null, null)
#0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
#1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:156:18)
<asynchronous suspension>
#2      Workmanager.registerPeriodicTask (package:workmanager/src/workmanager.dart:173:7)
<asynchronous suspension>
The Flutter DevTools debugger and profiler on iPhone 12 Pro Max is available at: http://127.0.0.1:9100?uri=http://127.0.0.1:56183/fjCBE_KZDpw=/

基本上 workmanager 没有工作。我的代码有什么问题,我该如何解决?

该插件仅支持 iOS 上的一次性任务。

异常消息告诉您方法 registerPeriodicTask 未处理。

即使它在 iOS 上运行,iOS 也不支持按特定时间间隔安排的任务。您可以请求后台任务调度,但操作系统会根据电池寿命、设备是否唤醒甚至您的应用程序过去的性能等因素来决定是否实际为您的应用程序执行后台任务。

理想情况下,与其每 15 分钟在移动设备上执行一次任务,不如设计一些其他方法;例如,让服务器执行任务并在需要时发送通知。