Flutter firebase_message 插件设置错误

2024-04-28

我想在我的 Flutter 应用程序中尝试云消息传递,但我总是遇到错误,而且我还没有找到解决方案。我按照此处所述的步骤进行操作:firebase_消息传递 https://github.com/FirebaseExtended/flutterfire/tree/master/packages/firebase_messaging

如果我按照除可选部分之外的步骤为后台消息创建 Application.java 文件,并将测试消息从 Firebase 发送到启动的应用程序,则会收到此错误:

java.lang.RuntimeException: Unable to create service io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService: java.lang.RuntimeException: PluginRegistrantCallback is not set.
   at android.app.ActivityThread.handleCreateService(ActivityThread.java:3577)
   at android.app.ActivityThread.access$1400(ActivityThread.java:200)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1689)
   at android.os.Handler.dispatchMessage(Handler.java:106)
   at android.os.Looper.loop(Looper.java:201)
   at android.app.ActivityThread.main(ActivityThread.java:6806)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
E/AndroidRuntime(29468): Caused by: java.lang.RuntimeException: PluginRegistrantCallback is not set.
   at io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService.startBackgroundIsolate(FlutterFirebaseMessagingService.java:157)
   at io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService.onCreate(FlutterFirebaseMessagingService.java:77)
   at android.app.ActivityThread.handleCreateService(ActivityThread.java:3565)

对我来说,似乎也需要可选部分,但是当我这样做时,应用程序打开时会出现错误。出现错误后,应用程序仍然运行,如果我从 Firebase 发送测试消息,应用程序会成功接收它。后来它甚至没有收到消息,但抛出相同的错误。

java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.Application android.app.Activity.getApplication()' on a null object reference
   at com.github.adee42.keyboardvisibility.KeyboardVisibilityPlugin.registerWith(KeyboardVisibilityPlugin.java:107)
   at io.flutter.plugins.GeneratedPluginRegistrant.registerWith(GeneratedPluginRegistrant.java:22)
   at io.flutter.plugins.Application.registerWith(Application.java:18)
   at io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService.startBackgroundIsolate(FlutterFirebaseMessagingService.java:164)
   at io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.onMethodCall(FirebaseMessagingPlugin.java:133)
   at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:222)
   at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:96)
   at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:656)
   at android.os.MessageQueue.nativePollOnce(Native Method)
   at android.os.MessageQueue.next(MessageQueue.java:326)
   at android.os.Looper.loop(Looper.java:165)
   at android.app.ActivityThread.main(ActivityThread.java:6806)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(error, Attempt to invoke virtual method 'android.app.Application android.app.Activity.getApplication()' on a null object reference, null)
    StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
    MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:316:33)
    <asynchronous suspension>
    FirebaseMessaging.configure (package:firebase_messaging/firebase_messaging.dart:118:16)
    main (package:vonatles/main.dart:18:22)
    _AsyncAwaitCompleter.start (dart:async-patch/async_patch.dart:43:6)
    main (package:vonatles/main.dart:12:10)
    _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:229:25)
    _rootRun (dart:async/zone.dart:1124:13)
    _CustomZone.run (dart:async/zone.dart:1021:19)
    _runZoned (dart:async/zone.dart:1516:10)
    runZoned (dart:async/zone.dart:1500:12)
    _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:221:5)
    _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:305:19)
    _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:172:12)

版本:

firebase_core: ^0.4.0+1  
firebase_database: 3.0.7  
firebase_messaging: 5.1.5 

classpath 'com.android.tools.build:gradle:3.3.0'  
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.0"  
classpath 'com.google.gms:google-services:4.3.0'  

在 Flutter main 函数中我有这个:

final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();

_firebaseMessaging.configure(
  onMessage: (Map<String, dynamic> message) async {
    print("onMessage: $message");
  },
  onLaunch: (Map<String, dynamic> message) async {
    print("onLaunch: $message");
  },
  onResume: (Map<String, dynamic> message) async {
    print("onResume: $message");
  },
);

Application.java代码:

package io.flutter.plugins;

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

我的安卓主目录:

-main  
  -java  
    -io.flutter.plugins  
      Apllication.java  
      GeneratedPluginRegistrant.java  
  -kotlin  
    -my.package.name  
      MainActivity.kt

Android 清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="my.package.name">
    <application
        android:name="io.flutter.plugins.Application"
        android:label="mylabel"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            ...
            <intent-filter>
                <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>
</manifest>

由于我在 android 中的 MainActivity 是一个 Kotlin 类,因此当我尝试将 Application 类作为 Kotlin 类而不是尝试将其作为 Java 类时,我得到了这个问题的解决方案。 我尝试了很多解决方案,但仍然无法运行该应用程序。

应用程序.kt

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.FlutterFirebaseMessagingService

public class Application: FlutterApplication(), PluginRegistrantCallback {
    override fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingService.setPluginRegistrant(this)
    }

    override fun registerWith(registry: PluginRegistry) {
        FirebaseCloudMessagingPluginRegistrant.registerWith(registry)
    }
}

另外,FirebaseCloudMessagingPluginRegistrant.kt

package YOUR_PACKAGE_NAME

import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin

class FirebaseCloudMessagingPluginRegistrant {
    companion object {
        fun registerWith(registry: PluginRegistry) {
            if (alreadyRegisteredWith(registry)) {
                return;
            }
            FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"))
        }

        fun alreadyRegisteredWith(registry: PluginRegistry): Boolean {
            val key = FirebaseCloudMessagingPluginRegistrant::class.java.name
            if (registry.hasPlugin(key)) {
                return true
            }
            registry.registrarFor(key)
            return false
        }
    }
}

可以将其添加到 MainActivity 所在的示例目录中。

请按照此处提到的步骤尝试此操作:Firebase 消息传递 https://pub.dev/packages/firebase_messaging

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Flutter firebase_message 插件设置错误 的相关文章

随机推荐

  • 如何在(非 Android)Java 应用程序中检索 Firebase 数据?

    有没有办法将数据从 Firebase 检索到 Java 应用程序 我有一个 Android 应用程序 它将数据存储到 Firebase 数据库中 我需要在 Java 应用程序中读取该数据 我在网上搜索过 但没有找到明确的答案 不是浏览器的U
  • 错误:样式父级的资源类型“attr”无效

    将我的 Android 项目升级到 Studio 3 0 1 和 Gradle 3 0 后 我在构建过程中遇到了一个问题 MAFLogonUI 3 15 6 aar 46f903e9d58266c453b893b9caa03deb res
  • 如何转换该对象数组?

    我有个问题 我有该对象数组 const iHaveThis question What s your name answer dda form filled key 15 question What s your e mail answer
  • 查找每个 pandas 数据帧行中前 n 个最高值列的名称

    我有以下数据框 id p1 p2 p3 p4 1 0 9 1 4 2 0 2 3 4 3 1 3 10 7 4 1 5 3 1 5 2 3 7 10 我需要以一种方式重塑数据框 对于每个 id 来说 它将具有具有最高值的前 3 列 结果会是
  • 有条件跳过 TestNG 测试

    我对 TestNG 注释没有太多经验 但是我正在尝试使用 TestNG 框架和 POM 设计模式为零售网站构建测试套件 我计划使用数据驱动的方法 我的计划是通过 Excel 驱动我的测试场景 而不是使用 testng xml 例如 我将拥有
  • 本地计算机的 Onenote API

    这是我在 Stackoverflow 上的第一篇文章 我正在寻找有关 OneNote API 的指导 我期待开发一个主要用于 自己使用 的解决方案 在我的本地计算机上创建 打开和关闭 OneNote 部分 不使用云等 请指导是否可以 我希望
  • 如何获取真实的屏幕高度和宽度?

    DisplayMetrics metrics new DisplayMetrics this getWindowManager getDefaultDisplay getMetrics metrics screenWidth metrics
  • 如何以 OO 风格打开管道?

    我用新风格重写了旧代码 如下所示 old style open FD file new style fh IO File gt new file r 文件没问题 但我不知道如何打开管道 read from pipes open PIPE s
  • dotnet 技术中的类库和安全性

    假设我已经开发了类库 并且我希望我只能在我的项目中使用这个库 但是如果有人尝试复制 dll 文件并想在他的项目中使用它 那么他将无法这样做 所以我只是想知道如何在 dll 文件中嵌入这种类型的安全性 请告诉我所有方法 谢谢 您可以在程序集中
  • Hibernate NoCacheRegionFactoryAvailableException

    我遇到了一个奇怪的 Hibernate 异常 我无法解释 它告诉我我正在使用二级缓存 但没有在哪里hibernate cfg xml我是否指定二级缓存 这是例外情况 org hibernate cache NoCacheRegionFact
  • Redux Toolkit RTK Query 发送查询参数

    如何使用 Redux Toolkit RTK 查询将查询参数传递到 api import createApi fetchBaseQuery from reduxjs toolkit query react const baseUrl xxx
  • 仅对 Visual Studio 团队服务强制执行拉取请求

    有一种方法可以强制某些人只能通过拉取请求为一个分支做出贡献 我希望他们能够接受拉取请求 但不能直接推送更改 这将很有用 因为我们有一些分支策略 对于接受拉取请求的人来说 例如构建必须通过 问题是 如果我们允许某人接受拉取请求 我们将允许他们
  • Enterprise Java Bean 到底是什么?

    Tomcat FAQ 上说 Tomcat 不是 EJB 服务器 Tomcat 不是完整的 J2EE 服务器 但如果我 使用 Spring 提供应用程序上下文 使用 JPA 注释我的实体 注释 并使用 Hibernate 作为 JPA 提供商
  • 在 SSIS 中插入新记录之前如何清空目标表?

    我使用 SSIS 生成和转换新数据 以便以后在新系统中使用 每次运行 SSIS 包时我都会遇到问题 它不断将新记录插入到我的目标表中 如何先清空目标表 OLE DB Destination 然后插入新生成的记录 目前此问题的解决方法是执行d
  • 访问Firebase规则中的电子邮件地址[重复]

    这个问题在这里已经有答案了 我正在使用 Firebase 3 编写 firebase 规则时 auth 对象仅包含 uid 和提供程序 有什么方法可以增强此功能以提供电子邮件地址吗 我试图解决的问题是 我正在处理的网站的所有者希望根据用户的
  • HTML 语义:将链接(锚元素)放在标题中是个好主意吗?

    以这种方式建立新闻列表是错误的吗 h1 a href article 1 html That happened a h1 div class short Just like planned div 我应该更喜欢这个吗 h1 That hap
  • 现代 CNN(卷积神经网络)作为 DetectNet 旋转不变吗?

    众所周知 用于目标检测的 nVidia DetectNet CNN 卷积神经网络 基于 Yolo DenseBox 的方法 https devblogs nvidia com parallelforall deep learning obj
  • MSTest 命令行设置

    我们需要能够将服务器地址传递到 MSTest 命令行中 以便由 TeamCity 持续集成 CI 软件调用我们的测试套件 之前我们在 C 单元测试中创建了 if 常量 这在 MSBuild 中进行了更改 有没有更好的办法 这种方式看起来很h
  • Azure WebApp - 无法自动检测应用程序的运行时堆栈

    我正在尝试创建只有静态 HTML 的 Web 应用程序 我正在关注这个链接https learn microsoft com en us azure app service app service web get started html
  • Flutter firebase_message 插件设置错误

    我想在我的 Flutter 应用程序中尝试云消息传递 但我总是遇到错误 而且我还没有找到解决方案 我按照此处所述的步骤进行操作 firebase 消息传递 https github com FirebaseExtended flutterf