Flutter Firebase Messaging Crashing Targeting S+(版本 31 及以上)需要 FLAG_IMMUTABLE 之一

Flutter Firebase Messaging Crashing Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE

最近我们将 android 个应用程序目标发布到 31 个,其中报告了更多 Firebase 消息传递 (PendingIntent) 崩溃(仅 Android OS 12 个用户)。当应用程序在通知接收时处于 Pause/Background 状态时崩溃。 我已经尝试了可能的解决方案,但仍然没有运气崩溃。据我了解,我们必须在 PendingIntent start activity 上添加 PendingIntent.FLAG_IMMUTABLE 但不知道如何添加或修复它。

如果有人遇到同样的问题并已解决。为此分享解决方案。

提前致谢...

dependencies{
    implementation platform('com.google.firebase:firebase-bom')
    implementation 'com.google.firebase:firebase-messaging'
    implementation 'com.google.firebase:firebase-iid'
    implementation 'com.google.firebase:firebase-analytics:17.4.4'
    implementation 'com.google.firebase:firebase-crashlytics:17.0.0'
}
E/AndroidRuntime( 7058): java.lang.IllegalArgumentException: com.package: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
E/AndroidRuntime( 7058): Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
E/AndroidRuntime( 7058):    at android.app.PendingIntent.checkFlags(PendingIntent.java:375)
E/AndroidRuntime( 7058):    at android.app.PendingIntent.getActivityAsUser(PendingIntent.java:458)
E/AndroidRuntime( 7058):    at android.app.PendingIntent.getActivity(PendingIntent.java:444)
E/AndroidRuntime( 7058):    at android.app.PendingIntent.getActivity(PendingIntent.java:408)
E/AndroidRuntime( 7058):    at com.google.firebase.messaging.CommonNotificationBuilder.createContentIntent(com.google.firebase:firebase-messaging@@20.3.0:125)
E/AndroidRuntime( 7058):    at com.google.firebase.messaging.CommonNotificationBuilder.createNotificationInfo(com.google.firebase:firebase-messaging@@20.3.0:27)
E/AndroidRuntime( 7058):    at com.google.firebase.messaging.CommonNotificationBuilder.createNotificationInfo(com.google.firebase:firebase-messaging@@20.3.0:9)
E/AndroidRuntime( 7058):    at com.google.firebase.messaging.DisplayNotification.handleNotification(com.google.firebase:firebase-messaging@@20.3.0:27)
E/AndroidRuntime( 7058):    at com.google.firebase.messaging.FirebaseMessagingService.dispatchMessage(com.google.firebase:firebase-messaging@@20.3.0:65)
E/AndroidRuntime( 7058):    at com.google.firebase.messaging.FirebaseMessagingService.passMessageIntentToSdk(com.google.firebase:firebase-messaging@@20.3.0:44)
E/AndroidRuntime( 7058):    at com.google.firebase.messaging.FirebaseMessagingService.handleMessageIntent(com.google.firebase:firebase-messaging@@20.3.0:27)
E/AndroidRuntime( 7058):    at com.google.firebase.messaging.FirebaseMessagingService.handleIntent(com.google.firebase:firebase-messaging@@20.3.0:17)
E/AndroidRuntime( 7058):    at com.google.firebase.messaging.EnhancedIntentService.lambda$processIntent[=12=]$EnhancedIntentService(com.google.firebase:firebase-messaging@@20.3.0:43)
E/AndroidRuntime( 7058):    at com.google.firebase.messaging.EnhancedIntentService$$Lambda[=12=].run(Unknown Source:6)
E/AndroidRuntime( 7058):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
E/AndroidRuntime( 7058):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
E/AndroidRuntime( 7058):    at com.google.android.gms.common.util.concurrent.zza.run(com.google.android.gms:play-services-basement@@17.6.0:2)
E/AndroidRuntime( 7058):    at java.lang.Thread.run(Thread.java:920)
W/CrashlyticsCore( 7058): Cannot send reports. Settings are unavailable.
D/TransportRuntime.SQLiteEventStore( 7058): Storing event with priority=HIGHEST, name=FIREBASE_CRASHLYTICS_REPORT for destination cct

根据官方文档“如果您的应用面向 Android 12,您必须指定您的应用创建的每个 PendingIntent 对象的可变性。此附加要求提高了您应用的安全性。"

Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);

   
PendingIntent contentIntent = PendingIntent.getActivity(context,NOTIFICATION_REQUEST_CODE, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

我们正在构建一个标准类型的 Intent 来打开我们的应用程序,然后简单地将其包装在 PendingIntent 中,然后再将其添加到我们的 notification.In 这种情况下,因为我们有一个确切的动作,我们知道我们想要执行,我们构建一个 PendingIntent,我们通过使用名为 FLAG_IMMUTABLE.

的标志,将其传递给的应用程序无法对其进行修改

我终于解决了这个问题。它由于较低版本的 Firebase 插件而崩溃,并且在升级 Android 依赖项和 Flutter 插件后它得到解决。

第 1 步:在 android/app/build.gradle

来自

implementation 'com.google.firebase:firebase-messaging:20.1.5'

implementation platform('com.google.firebase:firebase-bom') // add this 
implementation 'com.google.firebase:firebase-messaging:23.0.0'
implementation 'com.google.firebase:firebase-iid:21.1.0' // add this

第 2 步:在 Flutter 中 pubspec.yaml

来自

firebase_core: ^0.5.0+1
firebase_messaging: ^7.0.3
firebase_remote_config: ^0.4.2
firebase_crashlytics: ^0.1.4+1
firebase_dynamic_links: ^0.6.3
firebase_auth: ^0.18.4+1

firebase_core: ^1.10.0
firebase_messaging: ^11.2.6
firebase_remote_config: ^2.0.0
firebase_crashlytics: ^2.5.0
firebase_dynamic_links: ^4.0.5
firebase_auth: ^3.3.6