From 07f9d9c58c92dc72aa6a82cadb4ecf731d8b5b7b Mon Sep 17 00:00:00 2001 From: Danny Baumann Date: Thu, 11 Jul 2024 14:26:16 +0200 Subject: [PATCH] Cancel notification after invoking notification actions Signed-off-by: Danny Baumann --- .../habdroid/core/NotificationHandlingReceiver.kt | 11 +++++++++-- .../java/org/openhab/habdroid/ui/MainActivity.kt | 13 +++++++++++++ 2 files changed, 22 insertions(+), 2 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 14658876f7..d123518d33 100644 --- a/mobile/src/main/java/org/openhab/habdroid/core/NotificationHandlingReceiver.kt +++ b/mobile/src/main/java/org/openhab/habdroid/core/NotificationHandlingReceiver.kt @@ -31,9 +31,9 @@ import org.openhab.habdroid.util.openInBrowser class NotificationHandlingReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { Log.d(TAG, "onReceive(): $intent") - val notificationId = intent.getIntExtra(EXTRA_NOTIFICATION_ID, 0) when (intent.action) { ACTION_DISMISSED -> { + val notificationId = intent.getIntExtra(EXTRA_NOTIFICATION_ID, 0) Log.d(TAG, "Dismissed notification $notificationId") NotificationHelper(context).handleNotificationDismissed(notificationId) } @@ -43,6 +43,11 @@ class NotificationHandlingReceiver : BroadcastReceiver() { EXTRA_NOTIFICATION_ACTION, CloudNotificationAction::class.java ) ?: return + val notificationId = IntentCompat.getParcelableExtra( + intent, + EXTRA_NOTIFICATION_ID, + CloudNotificationId::class.java + ) ?: return Log.d(TAG, "Received action from $notificationId: $cna") when (val action = cna.action) { @@ -57,6 +62,7 @@ class NotificationHandlingReceiver : BroadcastReceiver() { throw IllegalArgumentException("Got unexpected action: $action") } } + NotificationHelper(context).cancelNotificationById(notificationId) } } } @@ -94,6 +100,7 @@ class NotificationHandlingReceiver : BroadcastReceiver() { 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) + putExtra(MainActivity.EXTRA_CLOUD_NOTIFICATION_ID, notificationId) } PendingIntent.getActivity( context, @@ -105,7 +112,7 @@ class NotificationHandlingReceiver : BroadcastReceiver() { else -> { val intent = Intent(context, NotificationHandlingReceiver::class.java).apply { action = ACTION_NOTIF_ACTION - putExtra(EXTRA_NOTIFICATION_ID, notificationId.notificationId) + putExtra(EXTRA_NOTIFICATION_ID, notificationId) putExtra(EXTRA_NOTIFICATION_ACTION, cna) } PendingIntent.getBroadcast( diff --git a/mobile/src/main/java/org/openhab/habdroid/ui/MainActivity.kt b/mobile/src/main/java/org/openhab/habdroid/ui/MainActivity.kt index a493c54455..d37d5d7ed6 100644 --- a/mobile/src/main/java/org/openhab/habdroid/ui/MainActivity.kt +++ b/mobile/src/main/java/org/openhab/habdroid/ui/MainActivity.kt @@ -50,6 +50,7 @@ import androidx.annotation.StringRes import androidx.appcompat.app.ActionBarDrawerToggle import androidx.appcompat.app.AlertDialog import androidx.core.content.ContextCompat +import androidx.core.content.IntentCompat import androidx.core.content.edit import androidx.core.graphics.drawable.DrawableCompat import androidx.core.graphics.drawable.toDrawable @@ -86,6 +87,7 @@ import org.openhab.habdroid.background.EventListenerService import org.openhab.habdroid.background.NotificationUpdateObserver import org.openhab.habdroid.background.PeriodicItemUpdateWorker import org.openhab.habdroid.core.CloudMessagingHelper +import org.openhab.habdroid.core.NotificationHelper import org.openhab.habdroid.core.OpenHabApplication import org.openhab.habdroid.core.UpdateBroadcastReceiver import org.openhab.habdroid.core.connection.CloudConnection @@ -96,6 +98,7 @@ import org.openhab.habdroid.core.connection.DemoConnection import org.openhab.habdroid.core.connection.NetworkNotAvailableException import org.openhab.habdroid.core.connection.NoUrlInformationException import org.openhab.habdroid.core.connection.WrongWifiException +import org.openhab.habdroid.model.CloudNotificationId import org.openhab.habdroid.model.LinkedPage import org.openhab.habdroid.model.ServerConfiguration import org.openhab.habdroid.model.ServerProperties @@ -852,6 +855,15 @@ class MainActivity : AbstractBaseActivity(), ConnectionFactory.UpdateListener { if (!intent.getStringExtra(EXTRA_UI_COMMAND).isNullOrEmpty()) { val command = intent.getStringExtra(EXTRA_UI_COMMAND) ?: return handleUiCommand(command, prefs.getPrimaryServerId()) + val notificationId = IntentCompat.getParcelableExtra( + intent, + EXTRA_CLOUD_NOTIFICATION_ID, + CloudNotificationId::class.java + ) + if (notificationId != null) { + // The invoking intent came from a notification click, so cancel the notification + NotificationHelper(this).cancelNotificationById(notificationId) + } } when (intent.action) { @@ -1652,6 +1664,7 @@ class MainActivity : AbstractBaseActivity(), ConnectionFactory.UpdateListener { const val EXTRA_LINK = "link" const val EXTRA_PERSISTED_NOTIFICATION_ID = "persistedNotificationId" const val EXTRA_UI_COMMAND = "uiCommand" + const val EXTRA_CLOUD_NOTIFICATION_ID = "cloudNotificationId" const val SNACKBAR_TAG_DEMO_MODE_ACTIVE = "demoModeActive" const val SNACKBAR_TAG_PRESS_AGAIN_EXIT = "pressAgainToExit"