how to save data in Hive database when receiving data in the background?
You can try the following code. The basic idea is to send data from background isolate to main isolate.
Future<void> backgroundMessageHandler(RemoteMessage msg){
IsolateNameServer.lookupPortByName('main_port')?.send(msg);
}
@override
void initState(){
super.initState();
ReceivePort receivePort = ReceivePort();
IsolateNameServer.registerPortWithName(receivePort.sendPort,'main_port');
receivePort.listen((message) {
if(message is RemoteMessage){
//TODO: save your data in hive box
}
}
}
Alexa289
Updated on November 23, 2022Comments
-
Alexa289 over 1 year
I have an issue saving data to Hive when receiving Firebase Cloud Messaging (FCM) push notification data when the app is in the background.
I have a static method to set up hive like this
static Future<void> setUpHive() async { try { await Hive.initFlutter(); if (!Hive.isBoxOpen("Box Name")) { await Hive.openBox("Box Name"); } } catch (error) { print(error.toString()); } }
I use that
setUpHive
static method inmain
function like thisFuture<void> main() async { await HiveHelper.setUpHive(); runApp( MyApp(), ); }
when the app is in the background, and then it receives FCM message, then this code below will be called. after that I try change the data stored in the Hive box
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async { // when receive FCM message when app is in the background, this block will be executed // set up the hive first await HiveHelper.setUpHive(); // then I try to change the data stored in the Hive box final myBox = Hive.box("BOX NAME"); myBox.put("key", 12345); }
it seems okay after receiving FCM background data, but when I fully close the app, and the
main
called again I have error when trying to open the box like thisstatic Future<void> setUpHive() async { try { await Hive.initFlutter(); if (!Hive.isBoxOpen("Box Name")) { await Hive.openBox("Box Name"); // Error in this line } } catch (error) { print(error.toString()); } }
the error is:
HiveError: This should not happen. Please open an issue on GitHub. E/flutter (13142): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: HiveError: This should not happen. Please open an issue on GitHub. E/flutter (13142): #0 BinaryReaderImpl.readFrame (package:hive/src/binary/binary_reader_impl.dart:250:7) E/flutter
I try to find the solution, and I find similar issue from here about Using Hive DB in a Background Process and it is said
leisim:
Unfortunately, Hive does not support opening boxes in multiple isolates. That means you can either close the box in the main isolate, update it in your background isolate and reopen it in the main isolate or you pass the data from the background to the main isolate and perform the update there...
I am new in Flutter, and I don't understand what he said. please help :(