【问题标题】:Firebase Cloud Messaging for Flutter - Optionally handle background messages ERROR用于 Flutter 的 Firebase 云消息传递 - 可选择处理后台消息错误
【发布时间】:2020-05-15 06:26:09
【问题描述】:

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

我想在我的应用程序中使用 Firebase 通知。我为此添加了 Aplication.java。添加此文件后,注册表一词带有红色下划线。

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

【问题讨论】:

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


    【解决方案1】:

    请确保您这样做:

    1 - Application.java 类添加到与您的 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应用

    【讨论】:

    • 我为此添加了 Aplication.java。添加此文件后,注册表一词带有红色下划线。 GeneratedPluginRegistrant.registerWith(registry);
    • ok 尝试打开android项目(打开application.java并在android studio中点击open进行编辑)并与gradle同步并运行它
    • 确保您正确导入 GeneratedPluginRegistrant 包,删除所有导入并重新导入自动
    • 如果不行,试试这行: GeneratedPluginRegistrant.registerWith(new ShimPluginRegistry(flutterEngine));
    • 对不起,我无法想象为什么问题仍然存在,尝试显示这个问题并回答github.com/FirebaseExtended/flutterfire/issues/1467
    【解决方案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")
            );
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-28
      • 2021-12-02
      • 2021-07-20
      • 1970-01-01
      • 1970-01-01
      • 2020-05-15
      • 1970-01-01
      • 2020-07-09
      相关资源
      最近更新 更多