应用程序在接收 Firebase 通知时崩溃:Flutter 应用程序中的 FlutterFirebaseMessagingService ClassNotFoundException

问题描述 投票:0回答:1

我正在开发一个 Flutter 应用程序,使用 Firebase Cloud Messaging (FCM) 进行推送通知。每次我收到通知时应用程序都会崩溃。这个问题是在实现FCM后开始出现的,我是根据Firebase的文档进行配置的。我尝试了各种解决方案,但仍然遇到此错误。

这是日志:

D/FLTFireMsgReceiver(19175): broadcast received for message
I/flutter (19175): Received a foreground message: 0:1730215904186659%6359b06d6359b06d
I/flutter (19175): Message title: notif title
I/flutter (19175): Message body: this a test for notification 22
W/eliteconect.app(19175): Accessing hidden method Landroid/os/WorkSource;->add(I)Z (unsupported,test-api, reflection, allowed)
W/eliteconect.app(19175): Accessing hidden method Landroid/os/WorkSource;->add(ILjava/lang/String;)Z (unsupported,test-api, reflection, allowed)
W/eliteconect.app(19175): Accessing hidden method Landroid/os/WorkSource;->get(I)I (unsupported, reflection, allowed)
W/eliteconect.app(19175): Accessing hidden method Landroid/os/WorkSource;->getName(I)Ljava/lang/String; (unsupported, reflection, allowed)
D/AndroidRuntime(19175): Shutting down VM
E/AndroidRuntime(19175): FATAL EXCEPTION: main
E/AndroidRuntime(19175): Process: com.eliteconect.app, PID: 19175
E/AndroidRuntime(19175): java.lang.RuntimeException: Unable to create service com.eliteconect.app.io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService: java.lang.ClassNotFoundException: Didn't find class "com.eliteconect.app.io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService" on path: DexPathList[[zip file "/data/app/~~cMje4QegG6aL8OluvEtslw==/com.eliteconect.app-0EBytU1U5_Ny1GUuuV6OaA==/base.apk"],nativeLibraryDirectories=[/data/app/~~cMje4QegG6aL8OluvEtslw==/com.eliteconect.app-0EBytU1U5_Ny1GUuuV6OaA==/lib/x86_64, /data/app/~~cMje4QegG6aL8OluvEtslw==/com.eliteconect.app-0EBytU1U5_Ny1GUuuV6OaA==/base.apk!/lib/x86_64, /system/lib64, /system_ext/lib64]]
E/AndroidRuntime(19175):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:4990)
E/AndroidRuntime(19175):    at android.app.ActivityThread.-$$Nest$mhandleCreateService(Unknown Source:0)
E/AndroidRuntime(19175):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2444)
E/AndroidRuntime(19175):    at android.os.Handler.dispatchMessage(Handler.java:107)
E/AndroidRuntime(19175):    at android.os.Looper.loopOnce(Looper.java:232)
E/AndroidRuntime(19175):    at android.os.Looper.loop(Looper.java:317)
E/AndroidRuntime(19175):    at android.app.ActivityThread.main(ActivityThread.java:8705)
E/AndroidRuntime(19175):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(19175):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
E/AndroidRuntime(19175):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:886)
E/AndroidRuntime(19175): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.eliteconect.app.io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService" on path: DexPathList[[zip file "/data/app/~~cMje4QegG6aL8OluvEtslw==/com.eliteconect.app-0EBytU1U5_Ny1GUuuV6OaA==/base.apk"],nativeLibraryDirectories=[/data/app/~~cMje4QegG6aL8OluvEtslw==/com.eliteconect.app-0EBytU1U5_Ny1GUuuV6OaA==/lib/x86_64, /data/app/~~cMje4QegG6aL8OluvEtslw==/com.eliteconect.app-0EBytU1U5_Ny1GUuuV6OaA==/base.apk!/lib/x86_64, /system/lib64, /system_ext/lib64]]
E/AndroidRuntime(19175):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:259)
E/AndroidRuntime(19175):    at java.lang.ClassLoader.loadClass(ClassLoader.java:637)
E/AndroidRuntime(19175):    at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
E/AndroidRuntime(19175):    at android.app.AppComponentFactory.instantiateService(AppComponentFactory.java:129)
E/AndroidRuntime(19175):    at androidx.core.app.CoreComponentFactory.instantiateService(CoreComponentFactory.java:74)
E/AndroidRuntime(19175):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:4949)
E/AndroidRuntime(19175):    ... 9 more
I/Process (19175): Sending signal. PID: 19175 SIG: 9

详情:

Flutter 3.24.4 • 通道稳定 • https://github.com/flutter/flutter.git 框架 • 修订版 603104015d(5 天前) • 2024-10-24 08:01:25 -0700 引擎 • 修订版 db49896cf2 工具 • Dart 3.5.4 • DevTools 2.37.3

依赖关系: firebase_core:^3.6.0 firebase_消息传递:^15.1.3 安卓清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.eliteconect.app">
    
    <!-- Permissions -->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:label="Elite Conect"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">
        
        <!-- Firebase Messaging Service -->
        <service
            android:name=".io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService"
            android:exported="true"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>

        <activity
            android:name=".MainActivity"
            android:exported="true"
            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">
            <meta-data
                android:name="io.flutter.embedding.android.NormalTheme"
                android:resource="@style/NormalTheme"/>
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

        <!-- UCropActivity declaration -->
        <activity
            android:name="com.yalantis.ucrop.UCropActivity"
            android:screenOrientation="portrait"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>

        <!-- FileProvider configuration -->
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"/>
        </provider>

        <!-- Flutter embedding metadata -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2"/>
            <meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
    </application>
</manifest>

MainActivity.kt中的代码:

import android.os.Bundle
import com.google.firebase.FirebaseApp
import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        FirebaseApp.initializeApp(this)
    }
}

故障排除步骤:

确认FirebaseMessagingService已在AndroidManifest.xml中注册。 尝试在 build.gradle 中设置各种依赖项。 多次清理和重建项目。 预期行为:应用程序应显示通知而不崩溃。

观察到的行为:应用程序在收到通知时崩溃,显示 FlutterFirebaseMessagingService 的 ClassNotFoundException。

任何解决此问题的意见或建议将不胜感激!

android flutter firebase firebase-cloud-messaging classnotfoundexception
1个回答
0
投票

从 console.firebase.google.com 上的文档中,您需要这样做才能获得工作通知:

enter image description here

那么你需要:

enter image description here

然后最后一步:

enter image description here

我不知道您遵循了哪些文档,但不需要在 main.kt 中执行任何操作,也不需要像您那样对 AndroidManifest 进行任何更改:)

希望回答对您有帮助!

欢呼,

© www.soinside.com 2019 - 2024. All rights reserved.