Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PluginRegistrantCallback is not set in Flutter

I got this error when I run my app and the background notifications when app is closed doesnt work:

firebase_messaging: ^7.0.3

final FirebaseMessaging firebaseMessaging = FirebaseMessaging();
  

  @override
  void initState() {
    super.initState();
    registerNotification();
  }



  void registerNotification() {
    firebaseMessaging.requestNotificationPermissions();

    firebaseMessaging.configure(onMessage: (Map<String, dynamic> message) {
      print('onMessage: $message');
      return ;
    }, onResume: (Map<String, dynamic> message) {
      print('onResume: $message');
      return Navigator.push(
          context,
          MaterialPageRoute(
              builder: (context) => NotificationsScreen()));
    }, onBackgroundMessage: _firebaseMessagingBackgroundHandler,
        onLaunch: (Map<String, dynamic> message) {
      print('onLaunch: $message');
      return;
    });

    firebaseMessaging.getToken().then((token) {
      print('token: $token');
      FirebaseFirestore.instance
          .collection('Consultant')
          .doc(firebaseUser.uid)
          .update({'deviceToken': token});
    }).catchError((err) {
      //Fluttertoast.showToast(msg: err.message.toString());
    });
  }

out from the class:

Future<dynamic> _firebaseMessagingBackgroundHandler(
    Map<String, dynamic> message,
    ) async {
  // Initialize the Firebase app
  await Firebase.initializeApp();
  print('onBackgroundMessage received: $message');
}

I got : ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: PlatformException(error, PluginRegistrantCallback is not set., null, java.lang.RuntimeException: PluginRegistrantCallback is not set.

The problem I figured out is when app is closed I can't get the notifications. Searching on the internet I see that: PluginRegistrantCallback is not set is an error which involve Application.kt class, but I tried in any manner to put this file unsuccesfully. Any one has any suggestion?

like image 771
Emanuel Developer Avatar asked Oct 15 '22 21:10

Emanuel Developer


2 Answers

That error comes from your application file. Make sure register properly.

Application.java

package <your package name>;

import com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin;
public final class CustomPluginRegistrant {
    public static void registerWith(PluginRegistry registry) {
        if (alreadyRegisteredWith(registry)) {
            return;
        }
        FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
        FlutterLocalNotificationsPlugin.registerWith(registry.registrarFor("com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin"));
    }

    private static boolean alreadyRegisteredWith(PluginRegistry registry) {
        final String key = CustomPluginRegistrant.class.getCanonicalName();
        if (registry.hasPlugin(key)) {
            return true;
        }
        registry.registrarFor(key);
        return false;
    }
}

Add a file in same route called CustomPluginRegistant.java

package <your package name>;

import com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin;
public final class CustomPluginRegistrant {
    public static void registerWith(PluginRegistry registry) {
        if (alreadyRegisteredWith(registry)) {
            return;
        }
        FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
        FlutterLocalNotificationsPlugin.registerWith(registry.registrarFor("com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin"));
    }

    private static boolean alreadyRegisteredWith(PluginRegistry registry) {
        final String key = CustomPluginRegistrant.class.getCanonicalName();
        if (registry.hasPlugin(key)) {
            return true;
        }
        registry.registrarFor(key);
        return false;
    }
}

If you are not using local notification plugin then can delete that part from code.

like image 58
yahya007 Avatar answered Oct 25 '22 01:10

yahya007


I have tried to go with the Application.java workaround using firebase_messaging: ^7.0.3, with and I ended up running into a lot of issues. Fortunately, I've come across an update about how to setup firebase for flutter. As indicated by the official doc that when you using Flutter Android Embedding V2 (Flutter Version >= 1.12), there are no additional steps required for android. So, we start by adding the following dependencies to pubsec.yaml:

firebase_core: ^0.7.0
firebase_messaging: ^8.0.0-dev.15

I've included a more detailed answer with code samples in my answer here.

You run flutter pub get. and then follow every step mentioned in the doc and you're ready to go.

like image 43
Fahima Mokhtari Avatar answered Oct 24 '22 23:10

Fahima Mokhtari