用于Flutter的Firebase云消息传递-(可选)处理后台消息ERROR

时间:2020-01-29 17:23:36

标签: android firebase flutter notifications firebase-cloud-messaging

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

我想在我的应用程序中使用Firebase通知。我为此添加Aplication.java。添加此文件后,单词registry用红色下划线。

GeneratedPluginRegistrant.registerWith( registry );

错误:

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

2 个答案:

答案 0 :(得分:2)

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

}

答案 1 :(得分:1)

请确保您这样做:

1-与MainActivity.java相同的目录中的App的Application.java类。

AndroidManifest.xml

中应用程序的

2- name属性(与您的软件包相同)

<application android:name=".Application">

3-并且对于我以前遇到过的FlutterFirebaseMessagingService来说,在依赖项内的android app build gradle文件中添加了最新版本的firebase-messaging

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

然后同步android项目并尝试构建Flutter应用