【问题标题】:Flutter firebase_messaging android app crash on .getToken() callFlutter firebase_messaging android 应用程序在 .getToken() 调用上崩溃
【发布时间】:2021-08-04 22:32:58
【问题描述】:

我正在尝试将 firebase_messaging 包与我的颤振应用程序集成。通常它适用于我正在使用的代码(版本 7.0.3)(见下文),但由于发布了空安全版本,我的代码不再工作。我想知道是否有人可以帮助我找出我做错了什么?

错误导致代码

try {
    fcmToken = await _firebaseMessaging.getToken();
} catch (error) {
    print(error);
}

错误输出

E/AndroidRuntime( 8942): java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/firebase/iid/Metadata;
E/AndroidRuntime( 8942):    at io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingPlugin.lambda$getToken$1$FlutterFirebaseMessagingPlugin(FlutterFirebaseMessagingPlugin.java:165)
E/AndroidRuntime( 8942):    at io.flutter.plugins.firebase.messaging.-$$Lambda$FlutterFirebaseMessagingPlugin$p8YQXDuFBNIxl8PFaPCQJtYc3sw.call(Unknown Source:4)
E/AndroidRuntime( 8942):    at com.google.android.gms.tasks.zzv.run(Unknown Source:2)
E/AndroidRuntime( 8942):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
E/AndroidRuntime( 8942):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
E/AndroidRuntime( 8942):    at java.lang.Thread.run(Thread.java:923)
E/AndroidRuntime( 8942): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.firebase.iid.Metadata" on path: DexPathList[[zip file "/data/app/~~uCa7QFzhvu5i_TZpnqat0w==/com.example.****-1cbY-LKuCoOsfZybV-Gq6g==/base.apk"],nativeLibraryDirectories=[/data/app/~~uCa7QFzhvu5i_TZpnqat0w==/com.example.****-1cbY-LKuCoOsfZybV-Gq6g==/lib/x86, /data/app/~~uCa7QFzhvu5i_TZpnqat0w==/com.example.****-1cbY-LKuCoOsfZybV-Gq6g==/base.apk!/lib/x86, /system/lib, /system_ext/lib]]
E/AndroidRuntime( 8942):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:207)
E/AndroidRuntime( 8942):    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
E/AndroidRuntime( 8942):    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
E/AndroidRuntime( 8942):    ... 6 more

app -> src -> main -> kotlin -> Application.kt

import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry

import io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingPlugin

class Application() : FlutterApplication(), PluginRegistry.PluginRegistrantCallback {
    override fun registerWith(registry: PluginRegistry?) {
        val key: String? = FlutterFirebaseMessagingPlugin::class.java.canonicalName
        if (!registry?.hasPlugin(key)!!) {
            FlutterFirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingPlugin"));
        }
    }
}

app -> src -> main -> kotlin -> MainActivity.kt

import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant

class MainActivity: FlutterActivity() {
    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
    }
}

【问题讨论】:

    标签: android firebase flutter dart firebase-cloud-messaging


    【解决方案1】:

    解决方案

    出于某种原因,我将 onBackgroundMessageHandler 作为类方法放在了我的项目中。通过简单地将方法放置为 顶级函数 即可解决问题。

    之前

    class MyNotificationClass {
          Future<dynamic> onBackgroundMessageHandler(RemoteMessage message) async {
            print('on Background $message');
          }
    
          // ...
        }
    

    之后

    Future<dynamic> onBackgroundMessageHandler(RemoteMessage message) async {
                print('on Background $message');
              }
    
    class MyNotificationClass {
              // ...
            }
    

    android -> app -> src -> build.gradle

    dependencies {
        implementation 'com.google.android.material:material:1.3.0'
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    
        // Import the Firebase BoM
        implementation platform('com.google.firebase:firebase-bom:26.3.0')
    
        // Add the dependency for the Firebase SDK for Google Analytics
        // When using the BoM, don't specify versions in Firebase dependencies
        implementation 'com.google.firebase:firebase-analytics'
        implementation 'com.google.firebase:firebase-auth'
        implementation 'com.google.firebase:firebase-messaging'
    }
    // ADD THIS AT THE BOTTOM
    apply plugin: 'com.google.gms.google-services'
    

    android -> build.gradle

    buildscript {
        ext.kotlin_version = '1.3.50'
        repositories {
            google()
            jcenter()
        }
    
        dependencies {
            //classpath 'com.android.tools.build:gradle:4.1.0'
            classpath 'com.android.tools.build:gradle:3.5.3' // 3.5.0
            classpath 'com.google.gms:google-services:4.3.2' // 4.3.2
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        }
    }
    
    allprojects {
        repositories {
            google()
            jcenter()
        }
    }
    
    rootProject.buildDir = '../build'
    subprojects {
        project.buildDir = "${rootProject.buildDir}/${project.name}"
        project.evaluationDependsOn(':app')
    }
    
    task clean(type: Delete) {
        delete rootProject.buildDir
    }
    

    pubspec.yaml

      cloud_firestore: ^1.0.5 #^0.14.4 #^0.13.5                     
      firebase_database: ^6.1.2 #4.4.0                             
      firebase_core: ^1.0.3 #^0.5.3 #^0.4.4+3                       
      firebase_auth: ^1.1.0 #0.18.4+1 #^0.16.0                     
      firebase_messaging: ^9.1.1 #7.0.3 #^6.0.15                   
      firebase_dynamic_links: ^2.0.0 #0.6.3 #^0.5.0+11             
      firebase_analytics: ^8.0.0 #6.3.0 #^5.0.11                   
      cloud_functions: ^1.0.3 #0.7.2 #^0.5.0                       
    

    AndroidManifest.xml

    <application
            android:name=".Application"
            android:label="app name"
            android:usesCleartextTraffic="true"
            android:icon="@mipmap/ic_launcher">
    
            <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="android.intent.action.MAIN"/>
                    <category android:name="android.intent.category.LAUNCHER"/>
                </intent-filter>
    
                <intent-filter>
                    <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>
    
            <meta-data
                android:name="com.google.firebase.messaging.default_notification_icon"
                android:resource="@mipmap/ic_notification" />
    
            <!-- Don't delete the meta-data below.
                 This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
            <meta-data
                android:name="flutterEmbedding"
                android:value="2" />
        </application>
    

    【讨论】:

    • 我遇到了同样的问题,我根本没有使用 onBackgroundMessageHandler,但我遇到了同样的崩溃
    • 嗯。我可以发布我的配置文件,您可以尝试编辑您的文件,看看它是否有效?希望对您有所帮助。
    • 它通过从 com.google.firebase:firebase-bom:28.0.1 降级到 27.0.0 来工作
    • 不错!我很高兴你设法解决了它。编码愉快!
    【解决方案2】:

    对我来说,它是通过降级来工作的

    com.google.firebase:firebase-bom:28.0.1

    com.google.firebase:firebase-bom:27.0.0

    提示:这是这些版本的临时解决方案:

    firebase_messaging: ^9.1.4 firebase_core: ^1.1.1

    【讨论】:

    • 我认为 bom28 有问题,而且 getToken 现在需要 vapidKey。在 bom26 中没有看到
    • 降级到 27.0.0 也解决了我的问题。谢谢。
    • 我的问题出在 react.js 上,但是用 27.0.0 替换库确实解决了这个问题。谢谢。
    猜你喜欢
    • 2017-12-31
    • 2020-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多