设置未决意图的Mustability标志

发布于 2025-01-21 22:33:28 字数 5949 浏览 1 评论 0原文

我正面临着Android 12的Firebase Cloud Messaging通知。我先前面对针对S+(版本31及更高版本)要求在创建悬挂时指定flag_immutable或Flag_mutable之一。,然后设置一个设置。相应的可变性标志。设置标志后,当我的应用在后台时,我仍然会收到相同的错误。这是我的代码 -

    private void sendDishReadyNotification(String title, String description) {


        NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle();
        style.bigText(description);

        Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

        PendingIntent pendingIntent;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            pendingIntent = PendingIntent.getActivity
                    (this, 0, intent, PendingIntent.FLAG_MUTABLE);
        } else {
            pendingIntent = PendingIntent.getActivity
                    (this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        }

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        String NOTIFICATION_CHANNEL_ID = "Kitchen Display";


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            @SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification", NotificationManager.IMPORTANCE_MAX);

            AudioAttributes audioAttributes = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_ALARM)
                    .build();

            Uri soundUri = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.alert);

            //Configure Notification Channel
            notificationChannel.setDescription("Kitchen Display Notifications");
            notificationChannel.enableLights(true);
            notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
            notificationChannel.enableVibration(true);
            notificationChannel.setShowBadge(true);
            notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            notificationChannel.setSound(soundUri, audioAttributes);
            notificationManager.createNotificationChannel(notificationChannel);
        }

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_notification)
                .setVibrate(new long[]{0, 1000, 500, 1000})
                .setContentTitle(title)
                .setAutoCancel(true)
                .setSound(defaultSound)
                .setContentText(description)
                .setStyle(style)
                .setWhen(System.currentTimeMillis())
                //.setFullScreenIntent(pendingIntent,true)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setContentIntent(pendingIntent)
                .setDefaults(NotificationCompat.DEFAULT_ALL);


        notificationManager.notify(++NOTIFICATION_ID, notificationBuilder.build());
        AppData.isNotificationClicked = true;
    }

即使设置Mutability标志后,我仍会继续收到的错误 -

E/AndroidRuntime: FATAL EXCEPTION: Firebase-Messaging-Intent-Handle
    Process: com.quicklyservices.restaurants, PID: 6690
    java.lang.IllegalArgumentException: com.quicklyservices.restaurants: 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:375)
        at android.app.PendingIntent.getActivityAsUser(PendingIntent.java:458)
        at android.app.PendingIntent.getActivity(PendingIntent.java:444)
        at android.app.PendingIntent.getActivity(PendingIntent.java:408)
        at com.google.firebase.messaging.CommonNotificationBuilder.createContentIntent(com.google.firebase:firebase-messaging@@21.0.0:125)
        at com.google.firebase.messaging.CommonNotificationBuilder.createNotificationInfo(com.google.firebase:firebase-messaging@@21.0.0:27)
        at com.google.firebase.messaging.CommonNotificationBuilder.createNotificationInfo(com.google.firebase:firebase-messaging@@21.0.0:9)
        at com.google.firebase.messaging.DisplayNotification.handleNotification(com.google.firebase:firebase-messaging@@21.0.0:27)
        at com.google.firebase.messaging.FirebaseMessagingService.dispatchMessage(com.google.firebase:firebase-messaging@@21.0.0:55)
        at com.google.firebase.messaging.FirebaseMessagingService.passMessageIntentToSdk(com.google.firebase:firebase-messaging@@21.0.0:34)
        at com.google.firebase.messaging.FirebaseMessagingService.handleMessageIntent(com.google.firebase:firebase-messaging@@21.0.0:27)
        at com.google.firebase.messaging.FirebaseMessagingService.handleIntent(com.google.firebase:firebase-messaging@@21.0.0:17)
        at com.google.firebase.messaging.EnhancedIntentService.lambda$processIntent$0$EnhancedIntentService(com.google.firebase:firebase-messaging@@21.0.0:43)
        at com.google.firebase.messaging.EnhancedIntentService$$Lambda$0.run(Unknown Source:6)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at com.google.android.gms.common.util.concurrent.zza.run(com.google.android.gms:play-services-basement@@17.2.1:6)

I am facing an issue with firebase cloud messaging notifications for Android 12. I previously faced Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent. and then set the mutability flag accordingly. After setting the flag, I still receive the same error when my app is in the background. Here is my code -

    private void sendDishReadyNotification(String title, String description) {


        NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle();
        style.bigText(description);

        Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

        PendingIntent pendingIntent;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            pendingIntent = PendingIntent.getActivity
                    (this, 0, intent, PendingIntent.FLAG_MUTABLE);
        } else {
            pendingIntent = PendingIntent.getActivity
                    (this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        }

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        String NOTIFICATION_CHANNEL_ID = "Kitchen Display";


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            @SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification", NotificationManager.IMPORTANCE_MAX);

            AudioAttributes audioAttributes = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_ALARM)
                    .build();

            Uri soundUri = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.alert);

            //Configure Notification Channel
            notificationChannel.setDescription("Kitchen Display Notifications");
            notificationChannel.enableLights(true);
            notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
            notificationChannel.enableVibration(true);
            notificationChannel.setShowBadge(true);
            notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            notificationChannel.setSound(soundUri, audioAttributes);
            notificationManager.createNotificationChannel(notificationChannel);
        }

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_notification)
                .setVibrate(new long[]{0, 1000, 500, 1000})
                .setContentTitle(title)
                .setAutoCancel(true)
                .setSound(defaultSound)
                .setContentText(description)
                .setStyle(style)
                .setWhen(System.currentTimeMillis())
                //.setFullScreenIntent(pendingIntent,true)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setContentIntent(pendingIntent)
                .setDefaults(NotificationCompat.DEFAULT_ALL);


        notificationManager.notify(++NOTIFICATION_ID, notificationBuilder.build());
        AppData.isNotificationClicked = true;
    }

The error I keep receiving even after setting mutability flags -

E/AndroidRuntime: FATAL EXCEPTION: Firebase-Messaging-Intent-Handle
    Process: com.quicklyservices.restaurants, PID: 6690
    java.lang.IllegalArgumentException: com.quicklyservices.restaurants: 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:375)
        at android.app.PendingIntent.getActivityAsUser(PendingIntent.java:458)
        at android.app.PendingIntent.getActivity(PendingIntent.java:444)
        at android.app.PendingIntent.getActivity(PendingIntent.java:408)
        at com.google.firebase.messaging.CommonNotificationBuilder.createContentIntent(com.google.firebase:firebase-messaging@@21.0.0:125)
        at com.google.firebase.messaging.CommonNotificationBuilder.createNotificationInfo(com.google.firebase:firebase-messaging@@21.0.0:27)
        at com.google.firebase.messaging.CommonNotificationBuilder.createNotificationInfo(com.google.firebase:firebase-messaging@@21.0.0:9)
        at com.google.firebase.messaging.DisplayNotification.handleNotification(com.google.firebase:firebase-messaging@@21.0.0:27)
        at com.google.firebase.messaging.FirebaseMessagingService.dispatchMessage(com.google.firebase:firebase-messaging@@21.0.0:55)
        at com.google.firebase.messaging.FirebaseMessagingService.passMessageIntentToSdk(com.google.firebase:firebase-messaging@@21.0.0:34)
        at com.google.firebase.messaging.FirebaseMessagingService.handleMessageIntent(com.google.firebase:firebase-messaging@@21.0.0:27)
        at com.google.firebase.messaging.FirebaseMessagingService.handleIntent(com.google.firebase:firebase-messaging@@21.0.0:17)
        at com.google.firebase.messaging.EnhancedIntentService.lambda$processIntent$0$EnhancedIntentService(com.google.firebase:firebase-messaging@@21.0.0:43)
        at com.google.firebase.messaging.EnhancedIntentService$Lambda$0.run(Unknown Source:6)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at com.google.android.gms.common.util.concurrent.zza.run(com.google.android.gms:play-services-basement@@17.2.1:6)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

2025-01-28 22:33:28

尝试升级您的依赖

implementation platform('com.google.firebase:firebase-bom:29.3.1')

Try upgrade your dependency

implementation platform('com.google.firebase:firebase-bom:29.3.1')
ゃ懵逼小萝莉 2025-01-28 22:33:28

而不是:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

写下:

intent.setFlags(Intent.FLAG_MUTABLE | Intent.FLAG_ACTIVITY_CLEAR_TASK);

Instead of:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

write this:

intent.setFlags(Intent.FLAG_MUTABLE | Intent.FLAG_ACTIVITY_CLEAR_TASK);
离线来电— 2025-01-28 22:33:28

在升级我的依赖性之后,如上所述,我仍会收到错误。显然,这是工作经理库中的一个错误。将工作经理版本更新为2.7.0 -Alpha05或2.7.1,

implementation 'androidx.work:work-runtime:2.7.1'

for Kotlin-

 implementation "androidx.work:work-runtime-ktx:2.7.1"

注意,您仍然必须设置Mutability标志。

After upgrading my dependency as suggested by the answer above I was still receiving the error. Apparently, this is a bug in the work manager library. Updating the work manager version to 2.7.0-alpha05 or 2.7.1

implementation 'androidx.work:work-runtime:2.7.1'

For kotlin -

 implementation "androidx.work:work-runtime-ktx:2.7.1"

Note,you still have to set the mutability flags.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文