From 9ad0ce6f1497f425db907908be1b125e6ed83dbc Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Wed, 6 Apr 2022 13:59:33 +0200 Subject: [PATCH] fix(local-notifications): Add FLAG_MUTABLE to pending intents for SDK 31 support (#914) --- .../LocalNotificationManager.java | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationManager.java b/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationManager.java index 0c2161fdc..4ab410e14 100644 --- a/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationManager.java +++ b/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationManager.java @@ -253,13 +253,11 @@ private void buildNotification(NotificationManagerCompat notificationManager, Lo private void createActionIntents(LocalNotification localNotification, NotificationCompat.Builder mBuilder) { // Open intent Intent intent = buildIntent(localNotification, DEFAULT_PRESS_ACTION); - - PendingIntent pendingIntent = PendingIntent.getActivity( - context, - localNotification.getId(), - intent, - PendingIntent.FLAG_CANCEL_CURRENT - ); + int flags = PendingIntent.FLAG_CANCEL_CURRENT; + if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + flags = flags | PendingIntent.FLAG_MUTABLE; + } + PendingIntent pendingIntent = PendingIntent.getActivity(context, localNotification.getId(), intent, flags); mBuilder.setContentIntent(pendingIntent); // Build action types @@ -273,7 +271,7 @@ private void createActionIntents(LocalNotification localNotification, Notificati context, localNotification.getId() + notificationAction.getId().hashCode(), actionIntent, - PendingIntent.FLAG_CANCEL_CURRENT + flags ); NotificationCompat.Action.Builder actionBuilder = new NotificationCompat.Action.Builder( R.drawable.ic_transparent, @@ -295,7 +293,11 @@ private void createActionIntents(LocalNotification localNotification, Notificati dissmissIntent.putExtra(ACTION_INTENT_KEY, "dismiss"); LocalNotificationSchedule schedule = localNotification.getSchedule(); dissmissIntent.putExtra(NOTIFICATION_IS_REMOVABLE_KEY, schedule == null || schedule.isRemovable()); - PendingIntent deleteIntent = PendingIntent.getBroadcast(context, localNotification.getId(), dissmissIntent, 0); + flags = 0; + if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + flags = PendingIntent.FLAG_MUTABLE; + } + PendingIntent deleteIntent = PendingIntent.getBroadcast(context, localNotification.getId(), dissmissIntent, flags); mBuilder.setDeleteIntent(deleteIntent); } @@ -330,12 +332,11 @@ private void triggerScheduledNotification(Notification notification, LocalNotifi Intent notificationIntent = new Intent(context, TimedNotificationPublisher.class); notificationIntent.putExtra(NOTIFICATION_INTENT_KEY, request.getId()); notificationIntent.putExtra(TimedNotificationPublisher.NOTIFICATION_KEY, notification); - PendingIntent pendingIntent = PendingIntent.getBroadcast( - context, - request.getId(), - notificationIntent, - PendingIntent.FLAG_CANCEL_CURRENT - ); + int flags = PendingIntent.FLAG_CANCEL_CURRENT; + if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + flags = flags | PendingIntent.FLAG_MUTABLE; + } + PendingIntent pendingIntent = PendingIntent.getBroadcast(context, request.getId(), notificationIntent, flags); // Schedule at specific time (with repeating support) Date at = schedule.getAt(); @@ -373,7 +374,7 @@ private void triggerScheduledNotification(Notification notification, LocalNotifi if (on != null) { long trigger = on.nextTrigger(new Date()); notificationIntent.putExtra(TimedNotificationPublisher.CRON_KEY, on.toMatchString()); - pendingIntent = PendingIntent.getBroadcast(context, request.getId(), notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); + pendingIntent = PendingIntent.getBroadcast(context, request.getId(), notificationIntent, flags); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && schedule.allowWhileIdle()) { alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC, trigger, pendingIntent); } else { @@ -399,7 +400,11 @@ public void cancel(PluginCall call) { private void cancelTimerForNotification(Integer notificationId) { Intent intent = new Intent(context, TimedNotificationPublisher.class); - PendingIntent pi = PendingIntent.getBroadcast(context, notificationId, intent, 0); + int flags = 0; + if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + flags = PendingIntent.FLAG_MUTABLE; + } + PendingIntent pi = PendingIntent.getBroadcast(context, notificationId, intent, flags); if (pi != null) { AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarmManager.cancel(pi);