Solvedflutterfire [firebase_messaging] Application.java:18: error: incompatible types: PluginRegistry cannot be converted to FlutterEngine GeneratedPluginRegistrant.registerWith(registry);

I wanted to add this field to my project but I got an error. https://pub.dev/packages/firebase_messaging#optionally-handle-background-messages

I want to use firebase notifications in my application. I'm adding Aplication.java for this. After adding this file, the word registry is underlined in red.

GeneratedPluginRegistrant.registerWith(registry);

ERROR:

\**APPNAME**\android\app\src\main\java\com\**PACKAGENAME**\**APPNAME**\Application.java:18: error: incompatible types: PluginRegistry cannot be converted to FlutterEngine
    GeneratedPluginRegistrant.registerWith(registry);
                                           ^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUοΏ½LD FAILED in 3s
Finished with error: Gradle task assembleDebug failed with exit code 1

APPNAME/android/app/src/main/java/app-organization-path/Application.java :

package **PACKAGENAME**;

import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService;

public class Application extends FlutterApplication implements PluginRegistrantCallback {
    @Override
    public void onCreate() {
        super.onCreate();
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
    }

    @Override
    public void registerWith(PluginRegistry registry) {
        GeneratedPluginRegistrant.registerWith(registry);
    }
}
18 Answers

βœ”οΈAccepted Answer

Instead of this method in application.java

    @Override
    public void registerWith(PluginRegistry registry) {
        GeneratedPluginRegistrant.registerWith(registry);
    }

Put this method :

    @Override
    public void registerWith(PluginRegistry registry) {
        FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
    }

Other Answers:

Instead of this method in application.java

    @Override
    public void registerWith(PluginRegistry registry) {
        GeneratedPluginRegistrant.registerWith(registry);
    }

Put this method :

    @Override
    public void registerWith(PluginRegistry registry) {
        FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
    }

I had to also add this in Application.java
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin;

The error does not show anymore. But when I "flutter run", I get stuck at "Installing build\app\outputs\apk\app.apk..." for a long time (i waited around 15-20 mins)

    FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));

Worked for me.

More Issues: