Issue
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?
Solution
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"));
}
}
Answered By - montauvergne
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.