【问题标题】:flutter_background_service not receiving updatesflutter_background_service 没有收到更新
【发布时间】:2022-07-08 19:15:22
【问题描述】:

当接收来自 FirebaseMessaging 的数据通知时,我将 awesome_notificationsflutter_background_service 结合使用来更新某些应用状态。如 awesome_notifications 中所述,后台消息处理程序必须是顶级函数,因此我使用 flutter_background_service 将数据传递给主隔离并更新应用状态。

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await initializeBackgroundService();
  FirebaseMessaging.onBackgroundMessage(_backgroundMessageHandler);
  _initLocalNotifications();
  runApp(MyApp());
}

我正在初始化后台服务,类似于flutter_background_service中的示例:

Future<void> initializeBackgroundService() async {
  final service = FlutterBackgroundService();
  await service.configure(
    androidConfiguration: AndroidConfiguration(
      onStart: onStart,
      autoStart: true,
      isForegroundMode: true,
    ),
    iosConfiguration: IosConfiguration(
      autoStart: true,
      onForeground: onStart,
      onBackground: onIosBackground,
    ),
  );
  await service.startService();
}

并在收到通知时在 _backgroundMessageHandler 中调用更新:

Future<void> _backgroundMessageHandler(
  RemoteMessage message,
) async {
  final service = FlutterBackgroundService();

  ...

  service.invoke('update', {
    'key1': 'val1',
    'key2': 'val2',
  });
}

在我的应用程序的 StatefulWidget 中,我正在监听更新调用以接收数据:

void listenForNotificationData() {
  final backgroundService = FlutterBackgroundService();
  backgroundService.on('update').listen((event) async {
    print('received data message in feed: $event');
  }, onError: (e, s) {
    print('error listening for updates: $e, $s');
  }, onDone: () {
    print('background listen closed');
  });
}

它永远不会在“更新”事件上调用监听回调。我可以确认它正在调用 invoke('update') 部分并调用 on('update').listen,但从未收到更新。它似乎也没有出错。我在这里的某个地方错过了一步吗?

【问题讨论】:

    标签: flutter dart background-process dart-isolates awesome-notifications


    【解决方案1】:

    这方面有什么进展吗? 我遇到了类似的问题,但不是 Future,而是 Timer(不是 Periodic) - Timer 中的代码不适用于 Service 实例...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-20
      • 1970-01-01
      • 1970-01-01
      • 2014-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-03
      相关资源
      最近更新 更多