Skip to content

Commit

Permalink
Handle 'hideNotification' in notification list and poller (#3750)
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 16, 2024
1 parent 8a0f124 commit 922fbef
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import androidx.core.content.edit
import org.json.JSONArray
import org.json.JSONException
import org.openhab.habdroid.core.connection.ConnectionFactory
import org.openhab.habdroid.model.CloudNotificationType
import org.openhab.habdroid.model.toCloudNotification
import org.openhab.habdroid.util.HttpClient
import org.openhab.habdroid.util.PrefKeys
Expand Down Expand Up @@ -65,8 +66,18 @@ object NotificationPoller {
val newMessages = if (lastSeenIndex >= 0) messages.subList(0, lastSeenIndex) else messages
val notifHelper = NotificationHelper(context)

newMessages.forEach { message ->
notifHelper.showNotification(message)
// Reverse list, so old notifications are processed first and can be hidden by newer notifications.
newMessages.reversed().forEach { message ->
when (message.type) {
CloudNotificationType.NOTIFICATION -> notifHelper.showNotification(message)
CloudNotificationType.HIDE_NOTIFICATION -> {
if (!message.tag.isNullOrEmpty()) {
notifHelper.cancelNotificationsByTag(message.tag)
} else {
notifHelper.cancelNotificationById(message.id)
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import kotlinx.coroutines.runBlocking
import org.openhab.habdroid.model.CloudNotification
import org.openhab.habdroid.model.CloudNotificationAction
import org.openhab.habdroid.model.CloudNotificationId
import org.openhab.habdroid.model.CloudNotificationType
import org.openhab.habdroid.model.toCloudNotificationAction
import org.openhab.habdroid.model.toOH2IconResource
import org.openhab.habdroid.util.map
Expand Down Expand Up @@ -52,6 +53,7 @@ class FcmMessageListenerService : FirebaseMessagingService() {
?.map { it.toCloudNotificationAction() }
?.filterNotNull()
val cloudNotification = CloudNotification(
type = CloudNotificationType.NOTIFICATION,
id = CloudNotificationId(data["persistedId"].orEmpty(), data["reference-id"]),
title = data["title"].orEmpty(),
message = data["message"].orEmpty(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,21 @@ data class CloudNotificationId internal constructor(
val notificationId get() = (referenceId ?: persistedId).hashCode()
}

@Parcelize
enum class CloudNotificationType : Parcelable {
NOTIFICATION,
HIDE_NOTIFICATION
}

fun String.toCloudNotificationType() = when (this) {
"notification" -> CloudNotificationType.NOTIFICATION
"hideNotification" -> CloudNotificationType.HIDE_NOTIFICATION
else -> null
}

@Parcelize
data class CloudNotification internal constructor(
val type: CloudNotificationType,
val id: CloudNotificationId,
val title: String,
val message: String,
Expand Down Expand Up @@ -117,9 +130,11 @@ fun JSONObject.toCloudNotification(): CloudNotification {

val payload = optJSONObject("payload")
return CloudNotification(
// Old notifications don't contain "type", so fallback to normal notifications here.
type = payload?.optString("type")?.toCloudNotificationType() ?: CloudNotificationType.NOTIFICATION,
id = CloudNotificationId(getString("_id"), payload?.optStringOrNull("reference-id")),
title = payload?.optString("title").orEmpty(),
message = payload?.getString("message") ?: getString("message"),
message = payload?.optString("message") ?: optString("message"),
createdTimestamp = created,
icon = payload?.optStringOrNull("icon").toOH2IconResource() ?: optStringOrNull("icon").toOH2IconResource(),
tag = payload?.optStringOrNull("tag") ?: optStringOrNull("severity"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import org.json.JSONArray
import org.json.JSONException
import org.openhab.habdroid.R
import org.openhab.habdroid.core.connection.ConnectionFactory
import org.openhab.habdroid.model.CloudNotificationType
import org.openhab.habdroid.model.ServerConfiguration
import org.openhab.habdroid.model.toCloudNotification
import org.openhab.habdroid.util.HttpClient
Expand Down Expand Up @@ -151,10 +152,12 @@ class CloudNotificationListFragment : Fragment(), View.OnClickListener, SwipeRef
requestJob = activity.launch {
try {
val response = conn.httpClient.get(url).asText().response
val items = JSONArray(response).map { obj -> obj.toCloudNotification() }
Log.d(TAG, "Notifications request success, got ${items.size} items")
loadOffset += items.size
adapter.addLoadedItems(items, items.size == PAGE_SIZE)
val allItems = JSONArray(response).map { obj -> obj.toCloudNotification() }
val filteredItems = allItems
.filter { notification -> notification.type == CloudNotificationType.NOTIFICATION }
Log.d(TAG, "Notifications request success, got ${allItems.size} items")
loadOffset += allItems.size
adapter.addLoadedItems(filteredItems, allItems.size == PAGE_SIZE)
handleInitialHighlight()
updateViewVisibility(loading = false, loadError = false)
} catch (e: JSONException) {
Expand Down
11 changes: 10 additions & 1 deletion mobile/src/main/java/org/openhab/habdroid/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,8 @@ class MainActivity : AbstractBaseActivity(), ConnectionFactory.UpdateListener {
connection != null && SpeechRecognizer.isRecognitionAvailable(this)
val debugItems = listOf(
R.id.mainmenu_debug_crash,
R.id.mainmenu_debug_clear_mtm
R.id.mainmenu_debug_clear_mtm,
R.id.mainmenu_poll_notifications
)
debugItems.forEach {
menu.findItem(it).isVisible = BuildConfig.DEBUG
Expand Down Expand Up @@ -454,6 +455,14 @@ class MainActivity : AbstractBaseActivity(), ConnectionFactory.UpdateListener {
}
true
}
R.id.mainmenu_poll_notifications -> {
if (CloudMessagingHelper.needsPollingForNotifications(this)) {
launch {
CloudMessagingHelper.pollForNotifications(this@MainActivity)
}
}
true
}
else -> super.onOptionsItemSelected(item)
}
}
Expand Down
6 changes: 6 additions & 0 deletions mobile/src/main/res/menu/main_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
android:title="@string/mainmenu_openhab_voice_recognition"
app:showAsAction="always"
app:iconTint="?android:textColorSecondary" />
<item
android:id="@+id/mainmenu_poll_notifications"
android:orderInCategory="1000"
android:title="Poll notifications"
app:showAsAction="never"
tools:ignore="HardcodedText" />
<item
android:id="@+id/mainmenu_debug_crash"
android:orderInCategory="1000"
Expand Down

0 comments on commit 922fbef

Please sign in to comment.