From 3f1df27f34801b01498cedf76f5ca996fcbf5ba8 Mon Sep 17 00:00:00 2001 From: mueller-ma Date: Sun, 21 Jul 2024 09:03:35 +0200 Subject: [PATCH] Notification: Fix website action when app is in background (#3776) Signed-off-by: mueller-ma --- .../core/NotificationHandlingReceiver.kt | 16 ++++++++++++---- .../org/openhab/habdroid/util/ExtensionFuncs.kt | 5 +---- 2 files changed, 13 insertions(+), 8 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 b15ce302a4..d19ecd1a41 100644 --- a/mobile/src/main/java/org/openhab/habdroid/core/NotificationHandlingReceiver.kt +++ b/mobile/src/main/java/org/openhab/habdroid/core/NotificationHandlingReceiver.kt @@ -17,7 +17,6 @@ import android.app.PendingIntent import android.content.BroadcastReceiver import android.content.Context import android.content.Intent -import android.content.Intent.FLAG_ACTIVITY_NEW_TASK import android.util.Log import androidx.core.content.IntentCompat import androidx.core.net.toUri @@ -27,7 +26,6 @@ import org.openhab.habdroid.model.CloudNotificationAction import org.openhab.habdroid.model.CloudNotificationId import org.openhab.habdroid.ui.MainActivity import org.openhab.habdroid.util.PendingIntent_Immutable -import org.openhab.habdroid.util.openInBrowser class NotificationHandlingReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { @@ -54,8 +52,6 @@ class NotificationHandlingReceiver : BroadcastReceiver() { when (val action = cna.action) { is CloudNotificationAction.Action.ItemCommandAction -> BackgroundTasksManager.enqueueNotificationAction(context, action) - is CloudNotificationAction.Action.UrlAction -> - action.url.toUri().openInBrowser(context, FLAG_ACTIVITY_NEW_TASK) is CloudNotificationAction.Action.NoAction -> { // no-op } @@ -110,6 +106,18 @@ class NotificationHandlingReceiver : BroadcastReceiver() { PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent_Immutable ) } + is CloudNotificationAction.Action.UrlAction -> { + val intent = Intent(Intent.ACTION_VIEW, cnaAction.url.toUri()).apply { + flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP or + Intent.FLAG_ACTIVITY_NEW_TASK + } + PendingIntent.getActivity( + context, + notificationId.notificationId + cna.hashCode(), + intent, + PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent_Immutable + ) + } else -> { val intent = Intent(context, NotificationHandlingReceiver::class.java).apply { action = ACTION_NOTIF_ACTION diff --git a/mobile/src/main/java/org/openhab/habdroid/util/ExtensionFuncs.kt b/mobile/src/main/java/org/openhab/habdroid/util/ExtensionFuncs.kt index 62c30f9916..9f4818cae0 100644 --- a/mobile/src/main/java/org/openhab/habdroid/util/ExtensionFuncs.kt +++ b/mobile/src/main/java/org/openhab/habdroid/util/ExtensionFuncs.kt @@ -129,14 +129,11 @@ fun String?.toNormalizedUrl(): String? { fun String?.orDefaultIfEmpty(defaultValue: String) = if (isNullOrEmpty()) defaultValue else this -fun Uri?.openInBrowser(context: Context, intentFlags: Int = 0) { +fun Uri?.openInBrowser(context: Context) { if (this == null) { return } val intent = Intent(Intent.ACTION_VIEW, this) - if (intentFlags != 0) { - intent.addFlags(intentFlags) - } try { context.startActivity(intent) } catch (e: ActivityNotFoundException) {