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?
Firstly, if you use Firebase Cloud Messaging for notification, android will get the notification when app is open or closed. But on iOS side, iOS is using APN's (Apple Push Notification).
I think the problem is that one of the plugins has crashed, causing shared_preferences to not register successfully, resulting in this error. Debug your GeneratedPluginRegistrant file, you may find that an exception occurs at one of the plugins
) Now, if you run code with background handler on iOS, it explodes with the error in this issue. So the solution is to set the handler to null for iOS: _fcm. configure ( ... onBackgroundMessage: Platform .isIOS ? null : myBackgroundMessageHandler, ... )
On your firebase console go to Settings > Cloud Messaging > iOS Application Configuration then add your APN Identification Key from your Apple Developer panel. You can see on this document. Also you should add "content_available": true on your notification payload. Show activity on this post.
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"));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With