user11847424
user11847424

Reputation:

Firebase Cloud Messaging for Flutter - Optionally handle background messages ERROR

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:

\live_chat\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);
    }
}

Upvotes: 1

Views: 2193

Answers (2)

Cesar Castro
Cesar Castro

Reputation: 1970

This is the code with the desired fix, also written in Kotlin.

package YOUR.PACKAGE.NAME

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

class Application: FlutterApplication(), PluginRegistrantCallback {

    override fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingService.setPluginRegistrant(this)
    }

    override fun registerWith(registry: PluginRegistry) {
        FirebaseMessagingPlugin.registerWith(
            registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin")
        );
    }

}

Upvotes: 2

Mahmoud Abu Alheja
Mahmoud Abu Alheja

Reputation: 3648

Please make sure you are do this :

1 - Application.java class to your app in the same directory as your MainActivity.java.

2- name property of application(same your package) in AndroidManifest.xml

<application android:name=".Application">

3- and for resloved FlutterFirebaseMessagingService i faced this problem before, add last version of firebase-messaging in your android app build gradle file inside dependencies

implementation 'com.google.firebase:firebase-messaging:20.1.0'

then sync android project and try build flutter app

Upvotes: 2

Related Questions