Flutter配置单元删除在Firebase后台消息处理程序中不起作用



当应用程序在后台收到推送通知或终止状态时,配置单元删除不起作用。我正在使用以下代码。

FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
.
.
//few lines of code
.
.

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async { 
  if (Platform.isAndroid || Platform.isIOS) {
    final Directory appDocDirectory =
        await getApplicationDocumentsDirectory();
    globals.hiveBoxesPath = appDocDirectory.path + '/boxes';
    Hive.init(globals.hiveBoxesPath);
  }
  BoxConstants.registerHiveAdapters();
   String mappingId = '';
    if (message.isNotEmpty && message.containsKey('data')) {
      final data = message['data'];
      final otherData =
          data.containsKey('other') ? json.decode(data['other']) : null;
      mappingId = otherData != null && otherData.containsKey('mapping_id')
          ? otherData['mapping_id'].toString()
          : mappingId;
    } else {
      final otherData =
          message.containsKey('other') ? json.decode(message['other']) : null;
      mappingId = otherData != null && otherData.containsKey('mapping_id')
          ? otherData['mapping_id'].toString()
          : mappingId;
    }
 //---- EDIT
    if (mappingId.isNotEmpty) {
  final String name = (OrgMemberUserBox).toString();
  if (Hive.isBoxOpen(name)) {
    Hive.box(name).close();
  }
  await HiveDbManager.openBox<OrgMemberUserBox>().then((box) async {
    await box.delete(mappingId);
  });
}
}

当设备收到通知时,会调用上述方法。我正在尝试按关键字mappingId删除,但它不起作用。

对此有什么解决方案吗?

onBackgroundMessage将生成一个隔离来运行您传递的方法。有人提到,要在另一个过程中使用Hive,您需要事先从主隔离物中关闭盒子。

以下是HiveDB存储库维护人员Simon Leier的声明:

不幸的是,Hive不支持打开多个框分离物。这意味着你可以关闭主隔离区的盒子,在后台隔离中更新它,并在主隔离中重新打开它或者将数据从后台传递到主隔离区在那里执行更新。。。

此外,您正在使用包path_provider中的getApplicationDocumentsDirectory(),该程序包目前似乎无法在隔离中运行。你可以在Flutter存储库上关注那些GitHub问题,这些问题是为了通知这个问题而打开的:

  • path_provider getApplicationDocumentsDirectory在隔离中损坏
  • 在隔离中使用path_provider包时出现问题

但不幸的是,到目前为止,这方面还没有取得太多进展。