Flutter firebase_message 插件设置错误

Posted

技术标签:

【中文标题】Flutter firebase_message 插件设置错误【英文标题】:Flutter firebase_message plugin setup errors 【发布时间】:2020-01-13 10:55:50 【问题描述】:

我想在我的 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

AndroidManifest:

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

【问题讨论】:

您是否已将 Application 类添加到 AndroidManifest.xml 中?如果不是,则将 FlutterApplication 替换为您创建的 Applcation 类。 @HarishPenta 我已将其添加到清单中,但我不完全确定我是否正确执行。我已将清单附加到帖子中。 将您的应用程序类从 io.flutter.plugins 包替换到您的 kotlin my.package.name 然后更改应用程序类的路径AndroidManifest 文件,然后告诉我。 @HarishPenta 我仍然收到 Activity.getApplication() 的 NullPointerException 错误。但还有一件事是,如果我像教程中那样在配置中设置 onBackgroundMessage,它也会引发错误('NoSuchMethodError: The method 'toRawHandle' was called on null.') 您是否在 build.gradle 文件中设置了apply plugin: 'com.google.gms.google-services'?如果是这样,请在 GitHub 上发布指向您的应用程序的链接,我会看看。为了告诉您出了什么问题,我需要缩小并查看整个应用程序 - 某处的“操作顺序”有问题。 【参考方案1】:

在您的 AndroidManifest 中:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.package.name">
<application
    android:name=".Application" //edit this line
    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>

【讨论】:

【参考方案2】:

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

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

【讨论】:

经过数小时的尝试,这是唯一对我有用的,感谢您提供的重要信息和回答:)【参考方案3】:

后台通知设置不再是可选的。确保正确关注intructions。我对更改 AndroidManifest.xml 感到困惑,所以我想分享清晰的说明。 PluginRegistrantCallback is not set”表示在AndroidManifest.xml中Application.java文件没有被正确调用。修改如下:

First it looks like this

Change it to yourpackagename.Application like this

如果您收到“未找到默认活动”错误,请在项目文件夹中运行“flutter clean”命令并再次运行。

【讨论】:

【参考方案4】:

顺便说一句,您可以像 Unhandled Exception: NoSuchMethodError: The method 'toRawHandle' was called on null 这样创建 notification_handler.dart。 我已经发布了我的 notification_handler.dart 他们的。 尝试使用它进行配置,然后从您的 statefullWidget 调用如下,然后告诉我。

   @override
    void initState() 
      super.initState();
      new NotificationHandler().initializeFcmNotification();
    

【讨论】:

以上是关于Flutter firebase_message 插件设置错误的主要内容,如果未能解决你的问题,请参考以下文章

firebase_messaging/unknown:Flutter IOS 出现未知错误

Flutter firebase_message 插件设置错误

为啥在 Flutter 中使用 firebase_messaging 插件在 ios 中没有收到通知?

MissingPluginException(在通道 plugins.flutter.io/firebase_messaging 上找不到方法 getToken 的实现)

未处理的异常:MissingPluginException(在通道 plugins.flutter.io/firebase_messaging 上找不到方法 getToken 的实现)

Firebase_messaging onResume 和 onLaunch 回调不会在 Flutter 中调用