From 22972373e5a9a74250971eb869c626ae6397ad76 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Mon, 13 Sep 2021 21:57:45 -0700 Subject: Fix pending intent for Android 12 --- .../kotlin/com/pitchedapps/frost/services/FrostNotifications.kt | 8 ++++++-- .../kotlin/com/pitchedapps/frost/widgets/NotificationWidget.kt | 6 +++++- 2 files changed, 11 insertions(+), 3 deletions(-) (limited to 'app/src/main/kotlin/com/pitchedapps') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt b/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt index 5b260dee..e050ad5a 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt @@ -59,6 +59,10 @@ import kotlin.math.abs */ private val _40_DP = 40.dpToPx +private val pendingIntentFlagUpdateCurrent: Int + get() = PendingIntent.FLAG_UPDATE_CURRENT or + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE else 0 + /** * Enum to handle notification creations */ @@ -227,7 +231,7 @@ enum class NotificationType( putContentExtra(intent, content) val group = "${groupPrefix}_${data.id}" val pendingIntent = - PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) + PendingIntent.getActivity(context, 0, intent, pendingIntentFlagUpdateCurrent) val notifBuilder = context.frostNotification(channelId) .setContentTitle(title ?: context.string(R.string.frost_name)) .setContentText(text) @@ -266,7 +270,7 @@ enum class NotificationType( intent.data = Uri.parse(fbItem.url) val group = "${groupPrefix}_$userId" val pendingIntent = - PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) + PendingIntent.getActivity(context, 0, intent, pendingIntentFlagUpdateCurrent) val notifBuilder = context.frostNotification(channelId) .setContentTitle(context.string(R.string.frost_name)) .setContentText("$count ${context.string(fbItem.titleId)}") diff --git a/app/src/main/kotlin/com/pitchedapps/frost/widgets/NotificationWidget.kt b/app/src/main/kotlin/com/pitchedapps/frost/widgets/NotificationWidget.kt index 9c5fbb7d..d610a535 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/widgets/NotificationWidget.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/widgets/NotificationWidget.kt @@ -86,7 +86,7 @@ class NotificationWidget : AppWidgetProvider() { context, 0, type.createCommonIntent(context, userId), - PendingIntent.FLAG_UPDATE_CURRENT + pendingIntentFlagUpdateCurrent ) views.setPendingIntentTemplate(R.id.widget_notification_list, pendingIntentTemplate) @@ -96,6 +96,10 @@ class NotificationWidget : AppWidgetProvider() { appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.widget_notification_list) } + private val pendingIntentFlagUpdateCurrent: Int + get() = PendingIntent.FLAG_UPDATE_CURRENT or + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE else 0 + companion object { fun forceUpdate(context: Context) { val manager = AppWidgetManager.getInstance(context) -- cgit v1.2.3