繁体   English   中英

(KOTLIN) 要求在创建 PendingIntent 时指定 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一

[英](KOTLIN) Requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent

在我的帐户收到的每条新消息中,应用程序都会自行重启。 然后它给出了这个错误。我试图找到解决方案但失败了。我在底行写了错误代码。

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.edusoft.teacher.myeducare, PID: 7367
    java.lang.RuntimeException: Unable to start receiver com.onesignal.GcmBroadcastReceiver: java.lang.IllegalArgumentException: com.edusoft.teacher.myeducare: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
    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.
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:4438)
        at android.app.ActivityThread.access$1700(ActivityThread.java:265)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2114)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:210)
        at android.os.Looper.loop(Looper.java:299)
        at android.app.ActivityThread.main(ActivityThread.java:8168)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1037)
     Caused by: java.lang.IllegalArgumentException: com.edusoft.teacher.myeducare: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
    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.
        at android.app.PendingIntent.checkFlags(PendingIntent.java:378)
        at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:648)
        at android.app.PendingIntent.getBroadcast(PendingIntent.java:635)
        at com.onesignal.GenerateNotification.getNewActionPendingIntent(GenerateNotification.java:195)
        at com.onesignal.GenerateNotification.createGenericPendingIntentsForNotif(GenerateNotification.java:404)
        at com.onesignal.GenerateNotification.showNotification(GenerateNotification.java:388)
        at com.onesignal.GenerateNotification.fromJsonPayload(GenerateNotification.java:116)
        at com.onesignal.NotificationBundleProcessor.ProcessJobForDisplay(NotificationBundleProcessor.java:115)
        at com.onesignal.NotificationBundleProcessor.ProcessFromGCMIntentService(NotificationBundleProcessor.java:98)
        at com.onesignal.GcmBroadcastReceiver.startGCMService(GcmBroadcastReceiver.java:138)
        at com.onesignal.GcmBroadcastReceiver.processOrderBroadcast(GcmBroadcastReceiver.java:129)
        at com.onesignal.GcmBroadcastReceiver.onReceive(GcmBroadcastReceiver.java:70)
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:4425)
            ... 9 more

我找到了这段代码,但要么我没有正确使用它,要么它不能正常工作

val updatedPendingIntent = PendingIntent.getActivity(
   applicationContext,
   NOTIFICATION_REQUEST_CODE,
   updatedIntent,
   PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT // setting the mutability flag 
)

如果您的应用以 Android 12 为目标,您必须指定您的应用创建的每个 PendingIntent object 的可变性。 此附加要求可提高您应用的安全性。

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

我们正在构造一个标准类型的 Intent 来打开我们的应用程序,然后在将其添加到我们的通知之前将其简单地包装在 PendingIntent 中。在这种情况下,由于我们有一个我们知道要执行的确切操作,我们构造一个我们通过使用名为 FLAG_IMMUTABLE 的标志将其传递给的应用程序无法修改的 PendingIntent。

如果您的目标是 Android S+ 并且您已将 FCM 集成到您的应用程序中,则必须进行此更改。

如果您打算迁移与 Android 12 兼容的应用程序,您可以查看此博客

用这个

val updatedPendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
    } else {
        PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT)
    }

在您的日志中显示错误,*

Caused by: java.lang.IllegalArgumentException: com.edusoft.teacher.myeducare: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
    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.

Android 应用程序针对 Firebase 消息(PendingIntent)或通知 function中报告了更多崩溃(仅 Android OS 12 用户)的 31 个应用程序。 当应用程序处于暂停/后台 state 时收到通知时崩溃。

此外,您可能需要将实现"androidx.work:work-runtime-ktx:2.5.0"的版本增加到最新更新

参考链接

 val updatedPendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        PendingIntent.getActivity(
            applicationContext, NOTIFICATION_REQUEST_CODE, updatedIntent, PendingIntent.FLAG_IMMUTABLE
        )
    } else {
        PendingIntent.getActivity(
            applicationContext, NOTIFICATION_REQUEST_CODE, updatedIntent, PendingIntent.FLAG_ONE_SHOT
        )
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM