Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 383
Repox Avatar asked Aug 21 '20 12:08

Repox


People also ask

What is the difference between Android push notifications and iOS push notifications?

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).

Why is my shared_preferences not registering?

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

How to set the background handler to null for iOS?

) 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, ... )

How to add APN to Firebase notifications?

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.


Video Answer


1 Answers

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"));
    }

}
like image 119
montauvergne Avatar answered Oct 17 '22 02:10

montauvergne