Repox
Repox

Reputation: 15476

MissingPluginException with background handler for push notifications when using SharedPreferences

I have the following file which is based on the firebase_messaging example on how to manage background messages (i.e. when the application is terminated or in the background).

The file currently contains the following:

import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'notifications.dart';

Future<dynamic> backgroundMessageHandler(Map<String, dynamic> message) async {

  print("backgroundHandler: $message");
  SharedPreferences prefs = await SharedPreferences.getInstance();
  print(prefs.getInt('latest_id'));

}

The application does receive the notification, which is revealed when it prints the contents of message but when I try and get the SharedPreferences the console presents me with the following messages and code execution stops:

I/flutter (17018): Unable to handle incoming background message.
I/flutter (17018): MissingPluginException(No implementation found for method getAll on channel plugins.flutter.io/shared_preferences)

Is it possible to fix this?

Upvotes: 5

Views: 1309

Answers (3)

This solves my problem

dependency_overrides: shared_preferences_android: 2.0.10

Upvotes: -1

Ranjith Bobby
Ranjith Bobby

Reputation: 79

just call before

SharedPreferences prefs = await SharedPreferences.getInstance();

this line on where you get value.

like.,

SharedPreferences prefs = await SharedPreferences.getInstance();

String name = prefs.getString("Name");

Upvotes: -1

montauvergne
montauvergne

Reputation: 173

I know it's a little late, but I found a solution. I share it for the next ones who will have this issue. You should add SharedPreferencesPlugin in your PluginRegistry :

SharedPreferencesPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin"));

The complete class :

import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import com.tekartik.sqflite.SqflitePlugin
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService
import com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin
import io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin

class Application : FlutterApplication(), PluginRegistry.PluginRegistrantCallback {
    override fun onCreate() {
        super.onCreate()

        FlutterFirebaseMessagingService.setPluginRegistrant(this);
    }

    override fun registerWith(registry: PluginRegistry?) {
        io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
        SqflitePlugin.registerWith(registry?.registrarFor("com.tekartik.sqflite.SqflitePlugin"));
        FlutterLocalNotificationsPlugin.registerWith(registry?.registrarFor("com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin"))
        SharedPreferencesPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.sharedpreferences.SharedPreferencesPlugin"));
    }

}

Upvotes: 6

Related Questions