Urvesh Rathod
Urvesh Rathod

Reputation: 31

MissingPluginException(No implementation found for method getAll on channel plugins.flutter.io/shared_preferences) while getting FCM in background

I'm working on a Flutter app in which I'm using the shared_preferences: ^2.0.13 package.

MissingPluginException(No implementation found for method getAll on channel plugins.flutter.io/shared_preferences).

While getting FCM message received in background.

main.dart

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();

  print('Got a message onBackgroundMessageHandler_');
  print("Handling a background message: ${message.messageId}");

  print('Shared pref process starts');
  SharedPreferences sf = await SharedPreferences.getInstance();
  sf.setString("key", "Value");
  print('${sf.getKeys()}');
  print('Shared pref process ends');
}

void main() async{
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();

  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);

  runApp(const MyApp());
}

MainActivity.kt

class MainActivity: FlutterActivity() {

}

Upvotes: 2

Views: 1254

Answers (2)

MerdanDev
MerdanDev

Reputation: 389

Some edit into your code below:


import 'package:shared_preferences_android/shared_preferences_android.dart';
import 'package:shared_preferences_ios/shared_preferences_ios.dart';

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();

  print('Got a message onBackgroundMessageHandler_');
  print("Handling a background message: ${message.messageId}");

  print('Shared pref process starts');
  // I am not sure what does SharedPreferencesAndroid.registerWith(); but it can solve problem
  if (Platform.isAndroid) SharedPreferencesAndroid.registerWith();
  if (Platform.isIOS) SharedPreferencesIOS.registerWith();
  SharedPreferences sf = await SharedPreferences.getInstance();
  sf.setString("key", "Value");
  print('${sf.getKeys()}');
  print('Shared pref process ends');
}

void main() async{
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();

  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);

  runApp(const MyApp());
}

There wasn't a little bit of information about it, espacially in firebase cloud messaging docs. I think this information must be available in onBackgroundMessage section of docs!

Upvotes: 1

Jim
Jim

Reputation: 7601

please reference to my post

you need to register share_preference for background access

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

Upvotes: 1

Related Questions