gpt4 book ai didi

Flutter firebase_message 插件设置错误

转载 作者:行者123 更新时间:2023-12-03 13:29:59 24 4
gpt4 key购买 nike

我想在我的 Flutter 应用程序中尝试云消息传递,但我总是遇到错误,而且我还没有找到解决方案。我按照此处所述的步骤操作: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 主函数中,我有这个:
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);
}
}

我的android主目录:
-main  
-java
-io.flutter.plugins
Apllication.java
GeneratedPluginRegistrant.java
-kotlin
-my.package.name
MainActivity.kt

安卓 list :
<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 messaging

关于Flutter firebase_message 插件设置错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57891871/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com