java.lang.RuntimeException: 无法实例化服务 com.samriddh.partner.java.MyFirebaseMessagingService

4

我正在使用Firebase消息服务,但当我从Firebase控制台收到通知时,我的应用程序会因为以下异常而关闭:

java.lang.RuntimeException: 无法实例化服务 com.samriddh.partner.java.MyFirebaseMessagingService:java.lang.ClassNotFoundException: 在路径上没有找到类“com.samriddh.partner.java.MyFirebaseMessagingService”,DexPathList[[zip文件“/system/framework/org.apache.http.legacy.boot.jar”,zip文件“/data/app/com.samriddh.partner-ameKbTimiGALFObiP3aUeg==/base.apk”],nativeLibraryDirectories = [/ data / app / com.samriddh.partner-ameKbTimiGALFObiP3aUeg == / lib / arm64,/ data / app / com.samriddh.partner-ameKbTimiGALFObiP3aUeg == / base.apk!/lib/arm64-v8a,/system/lib64]]

请问有人能提供建议吗?我已经花费了两个多小时来解决这个问题。

我的Firebase消息版本是firebase_messaging: ^6.0.16

我的FirebaseMessaging依赖关系是 implementation 'com.google.firebase:firebase-messaging:20.2.3'

我的Android Manifest.xml文件如下:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.samriddh.partner">
    <!-- io.flutter.app.FlutterApplication is an android.app.Application that
         calls FlutterMain.startInitialization(this); in its onCreate method.
         In most cases you can leave this as-is, but you if you want to provide
         additional functionality it is fine to subclass or reimplement
         FlutterApplication and put your custom class here. -->
         <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <application
        android:name=".Application"
        android:label="samriddh partner"
        android:icon="@mipmap/launcher_icon">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <!-- Displays an Android View that continues showing the launch screen
                 Drawable until Flutter paints its first frame, then this splash
                 screen fades out. A splash screen is useful to avoid any visual
                 gap between the end of Android's launch screen and the painting of
                 Flutter's first frame. -->
            <meta-data
              android:name="io.flutter.embedding.android.SplashScreenDrawable"
              android:resource="@drawable/launch_background"
              />
          <intent-filter>
                <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                <category android:name="android.intent.category.DEFAULT" />
          </intent-filter>
          <intent-filter>
              <action android:name="android.intent.action.MAIN"/>
              <category android:name="android.intent.category.LAUNCHER"/>
          </intent-filter>
        </activity>

        <meta-data android:name="com.google.android.geo.API_KEY"
          android:value="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"/>

        <uses-library android:name="org.apache.http.legacy" android:required="false"/>
        
        <meta-data android:name="flutterEmbedding" android:value="2" />
        
        <service android:name=".service.MyFirebaseMessagingService"> </service>
        
        <service android:name=".java.MyFirebaseMessagingService" android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>    
        </service>    
    </application>
</manifest>

我的 Application.kt 文件如下

package com.samriddh.partner

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

class Application : FlutterApplication(), PluginRegistrantCallback {

    override fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
    }

    override fun registerWith(registry: PluginRegistry?) {
        io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
    }
}
5个回答

3

将类设置为公共类解决了我的问题。


2
    <!-- THIS IS CRASHING THE PUSH NOTIFICATION -->    
    <!-- <service
        android:name=".java.MyFirebaseMessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service> -->

2
错误提示是告诉您,Android 找不到您在清单文件中声明的服务,其完整类名为“com.samriddh.partner.java.MyFirebaseMessagingService”。可能是您没有添加此类,或者在清单文件中指定了错误的名称。

请问现在我该做什么?我是新手,虽然我的应用程序能够运行,但当应用程序接收到通知时,它会意外关闭。 - Keshav Suman
1
听起来你没有完成将 FCM 集成到你的应用程序中所记录的说明。 - Doug Stevenson

0

检查顶部的包是否正确。 例如:

package com.samriddh.partner

如果从示例中复制/粘贴(就像我这样的傻瓜)它会有一些其他的包。
package com.google.firebase.quickstart.fcm.kotlin

0

在撰写本文时,我正在使用Flutter的firebase_messaging包v14.2.3。为了在直接引导模式下(应用未启动时)接收FCM消息,我需要这个包。

我已经在Android Studio中打开了项目,并在导入的包中查找了FlutterFirebaseMessagingService类。

对我有效的修复方法是在android/app/src/main/AndroidManifest.xml中使用此类名:io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingService

        <service
            android:name="io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>        

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接