Skip to content

Commit

Permalink
Cancel notification after invoking notification actions
Browse files Browse the repository at this point in the history
Signed-off-by: Danny Baumann <[email protected]>
  • Loading branch information
maniac103 authored and mueller-ma committed Jul 11, 2024
1 parent e946543 commit 07f9d9c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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) {
Expand All @@ -57,6 +62,7 @@ class NotificationHandlingReceiver : BroadcastReceiver() {
throw IllegalArgumentException("Got unexpected action: $action")
}
}
NotificationHelper(context).cancelNotificationById(notificationId)
}
}
}
Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand Down
13 changes: 13 additions & 0 deletions mobile/src/main/java/org/openhab/habdroid/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 07f9d9c

Please sign in to comment.