How do I add my background notifications into an Hive box? How to initialize Hive box in a static method?

903 views Asked by At

I'm trying to add my background notification's data into local DB, that is hive (into a box). My notifications are handled in such a way that when they come in, they get added to a box. I have a separate class for notification in which I have a static method myBackgroundHandler. I'm trying to initialize hive using await Hive.initFlutter() or Hive.init(path) but it gives me this error:

Unhandled Exception: MissingPluginException(No implementation found for method getApplicationDocumentsDirectory on channel plugins.flutter.io/path_provider)

My function:

static Future<dynamic> myBackgroundMessageHandler(
      Map<String, dynamic> message) async {
    try {
       WidgetsFlutterBinding.ensureInitialized();
       Hive.initFlutter();
       Box box = await Hive.openBox("notifs");
       box.add(message);
      print(message);
    } catch (e) {
       print("In catch block");
       print(e);
    }
0

There are 0 answers