Skip to content

Commit

Permalink
Notification: Fix website action when app is in background (#3776)
Browse files Browse the repository at this point in the history
Signed-off-by: mueller-ma <[email protected]>
  • Loading branch information
mueller-ma authored Jul 21, 2024
1 parent 3efdf1e commit 3f1df27
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 3f1df27

Please sign in to comment.