From 69ea76a04cc30217022b1fd24cfbdab8007e5148 Mon Sep 17 00:00:00 2001 From: mueller-ma Date: Tue, 9 Jul 2024 20:17:25 +0200 Subject: [PATCH] Fix PendingIntent Signed-off-by: mueller-ma --- .../core/NotificationHandlingReceiver.kt | 42 +++++++++++++------ .../habdroid/core/NotificationHelper.kt | 16 ++----- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/mobile/src/main/java/org/openhab/habdroid/core/NotificationHandlingReceiver.kt b/mobile/src/main/java/org/openhab/habdroid/core/NotificationHandlingReceiver.kt index e04aa65de7..082df09701 100644 --- a/mobile/src/main/java/org/openhab/habdroid/core/NotificationHandlingReceiver.kt +++ b/mobile/src/main/java/org/openhab/habdroid/core/NotificationHandlingReceiver.kt @@ -13,6 +13,7 @@ package org.openhab.habdroid.core +import android.app.PendingIntent import android.content.BroadcastReceiver import android.content.Context import android.content.Intent @@ -23,6 +24,7 @@ import org.openhab.habdroid.BuildConfig import org.openhab.habdroid.background.BackgroundTasksManager import org.openhab.habdroid.model.CloudNotificationAction import org.openhab.habdroid.ui.MainActivity +import org.openhab.habdroid.util.PendingIntent_Immutable import org.openhab.habdroid.util.openInBrowser class NotificationHandlingReceiver : BroadcastReceiver() { @@ -72,20 +74,34 @@ class NotificationHandlingReceiver : BroadcastReceiver() { } } - fun createActionIntent(context: Context, notificationId: Int, cna: CloudNotificationAction): Intent { - val cnaAction = cna.action - return if (cnaAction is CloudNotificationAction.Action.UiCommandAction) { - Intent(context, MainActivity::class.java).apply { - action = Intent.ACTION_VIEW - flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP or - Intent.FLAG_ACTIVITY_NEW_TASK - putExtra(MainActivity.EXTRA_UI_COMMAND, cnaAction.command) + fun createActionPendingIntent(context: Context, notificationId: Int, cna: CloudNotificationAction): PendingIntent { + return when (val cnaAction = cna.action) { + is CloudNotificationAction.Action.UiCommandAction -> { + val intent = Intent(context, MainActivity::class.java).apply { + action = Intent.ACTION_VIEW + flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP or + Intent.FLAG_ACTIVITY_NEW_TASK + putExtra(MainActivity.EXTRA_UI_COMMAND, cnaAction.command) + } + PendingIntent.getActivity( + context, + notificationId + cna.hashCode(), + intent, + PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent_Immutable + ) } - } else { - Intent(context, NotificationHandlingReceiver::class.java).apply { - action = ACTION_NOTIF_ACTION - putExtra(EXTRA_NOTIFICATION_ID, notificationId) - putExtra(EXTRA_NOTIFICATION_ACTION, cna) + else -> { + val intent = Intent(context, NotificationHandlingReceiver::class.java).apply { + action = ACTION_NOTIF_ACTION + putExtra(EXTRA_NOTIFICATION_ID, notificationId) + putExtra(EXTRA_NOTIFICATION_ACTION, cna) + } + PendingIntent.getBroadcast( + context, + notificationId + cna.hashCode(), + intent, + PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent_Immutable + ) } } } diff --git a/mobile/src/main/java/org/openhab/habdroid/core/NotificationHelper.kt b/mobile/src/main/java/org/openhab/habdroid/core/NotificationHelper.kt index db8170c9eb..0b0ad0a890 100644 --- a/mobile/src/main/java/org/openhab/habdroid/core/NotificationHelper.kt +++ b/mobile/src/main/java/org/openhab/habdroid/core/NotificationHelper.kt @@ -30,7 +30,6 @@ import org.openhab.habdroid.R import org.openhab.habdroid.background.NotificationUpdateObserver import org.openhab.habdroid.core.connection.ConnectionFactory import org.openhab.habdroid.model.CloudNotification -import org.openhab.habdroid.model.CloudNotificationAction import org.openhab.habdroid.model.IconResource import org.openhab.habdroid.ui.MainActivity import org.openhab.habdroid.util.HttpClient @@ -94,16 +93,6 @@ class NotificationHelper(private val context: Context) { ) } - private fun createActionIntent(action: CloudNotificationAction, notificationId: Int): PendingIntent { - val intent = NotificationHandlingReceiver.createActionIntent(context, notificationId, action) - return PendingIntent.getBroadcast( - context, - notificationId + action.hashCode(), - intent, - PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent_Immutable - ) - } - private fun createChannelForSeverity(severity: String?) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { return @@ -143,7 +132,7 @@ class NotificationHelper(private val context: Context) { val contentIntent = if (message.onClickAction == null) { makeNotificationClickIntent(message.id, notificationId) } else { - createActionIntent(message.onClickAction, notificationId) + NotificationHandlingReceiver.createActionPendingIntent(context, notificationId, message.onClickAction) } val channelId = getChannelId(message.severity) @@ -167,7 +156,8 @@ class NotificationHelper(private val context: Context) { .setPublicVersion(publicVersion) message.actions?.forEach { - val action = NotificationCompat.Action(null, it.label, createActionIntent(it, notificationId)) + val pi = NotificationHandlingReceiver.createActionPendingIntent(context, notificationId, it) + val action = NotificationCompat.Action(null, it.label, pi) builder.addAction(action) }