Firebase Cloud Messaging for Flutter - 可选择处理后台消息错误

Firebase Cloud Messaging for Flutter - Optionally handle background messages ERROR

我想将此字段添加到我的项目中,但出现错误。 https://pub.dev/packages/firebase_messaging#optionally-handle-background-messages

我想在我的应用程序中使用 firebase 通知。我为此添加 Aplication.java。添加此文件后,registry 字样带有红色下划线。

GeneratedPluginRegistrant.registerWith(注册表);

错误:

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

请确保您这样做了:

1 - Application.java class 到您的应用程序 与您的 MainActivity.java.

相同的目录

2- AndroidManifest.xml

中应用程序的名称 属性(与您的软件包相同)
<application android:name=".Application">

3- 对于我以前遇到过这个问题的重新安装的 FlutterFirebaseMessagingService,在你的 android 应用构建 gradle 文件中的依赖项

中添加最新版本的 firebase-messaging
implementation 'com.google.firebase:firebase-messaging:20.1.0'

然后 同步 android 项目 并尝试构建 flutter 应用程序

这是具有所需修复的代码,也是用 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")
        );
    }

}