首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >(KOTLIN)要求在创建FLAG_IMMUTABLE或FLAG_MUTABLE时指定PendingIntent之一

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

Stack Overflow用户
提问于 2022-11-21 14:08:53
回答 3查看 121关注 0票数 0

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

代码语言:javascript
运行
复制
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

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

代码语言:javascript
运行
复制
val updatedPendingIntent = PendingIntent.getActivity(
   applicationContext,
   NOTIFICATION_REQUEST_CODE,
   updatedIntent,
   PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT // setting the mutability flag 
)
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2022-11-21 16:21:13

如果您的应用程序以Android12为目标,则必须指定应用程序创建的每个PendingIntent对象的可变性。这一额外要求提高了应用程序的安全性。

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

我们正在构建一种标准的意图类型,它将打开我们的应用程序,然后简单地包装在PendingIntent中,然后将它添加到我们的notification.In (这个例子)中,因为我们知道我们想要执行一个确切的操作,所以我们构造了一个不能被我们传递给它的应用程序修改的PendingIntent,它使用一个名为FLAG_IMMUTABLE的标志。

如果你的目标是安卓S+,并且你已经在你的应用程序中集成了FCM,那么这个改变是必须的。

如果您计划迁移与Android12兼容的应用程序,您可以检查这个博客

票数 2
EN

Stack Overflow用户

发布于 2022-11-21 14:16:17

使用这个

代码语言:javascript
运行
复制
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)
    }
票数 0
EN

Stack Overflow用户

发布于 2022-11-21 14:33:50

在日志中显示错误,*

代码语言:javascript
运行
复制
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.

安卓应用程序的目标是31,据报道,在Firebase消息传递(PendingIntent)或通知功能中有更多的崩溃(只有Android 12用户)。当应用程序在通知上处于暂停/背景状态时,接收其崩溃。

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

参考链接

代码语言:javascript
运行
复制
 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
        )
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74520198

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档