简体   繁体   English

Flutter 的 Firebase 云消息传递 - 可选择处理后台消息错误

[英]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 https://pub.dev/packages/firebase_messaging#optionally-handle-background-messages

I want to use firebase notifications in my application.我想在我的应用程序中使用 firebase 通知。 I'm adding Aplication.java for this.我为此添加了Aplication.java。 After adding this file, the word registry is underlined in red.添加此文件后,registry 一词以红色下划线表示。

GeneratedPluginRegistrant.registerWith( registry ); 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 : 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);
    }
}

Please make sure you are do this :请确保您这样做:

1 - Application.java class to your app in the same directory as your MainActivity.java. 1 - Application.java 类到MainActivity.java 位于同一目录中的应用程序。

2- name property of application(same your package) in AndroidManifest.xml 2- 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 3-对于我喜欢的 FlutterFirebaseMessagingService 我之前遇到过这个问题,在你的 android 应用程序中添加最新版本的 firebase-messaging 在依赖项中构建 gradle 文件

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

then sync android project and try build flutter app然后同步android项目并尝试构建flutter应用程序

This is the code with the desired fix, also written in Kotlin.这是具有所需修复的代码,也是用 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")
        );
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM