Skip to content

Commit

Permalink
Update summary notification when notification is dismissed
Browse files Browse the repository at this point in the history
Closes openhab#3591

Signed-off-by: mueller-ma <[email protected]>
  • Loading branch information
mueller-ma committed Jun 28, 2024
1 parent 2379297 commit 6e4a461
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ object NotificationPoller {

newMessages.forEach { message ->
notifHelper.showNotification(
message,
null,
null
message
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ class FcmMessageListenerService : FirebaseMessagingService() {
)

runBlocking {
notifHelper.showNotification(
cloudNotification,
null,
null
)
notifHelper.showNotification(cloudNotification)
}
}
"hideNotification" -> {
Expand Down
3 changes: 3 additions & 0 deletions mobile/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@
<receiver
android:name=".background.CopyToClipboardReceiver"
android:exported="false" />
<receiver
android:name=".core.NotificationDismissedReceiver"
android:exported="false" />

<activity
android:name=".background.NfcReceiveActivity"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2010-2024 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/

package org.openhab.habdroid.core

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import org.openhab.habdroid.core.NotificationHelper.Companion.NOTIFICATION_ID_EXTRA

class NotificationDismissedReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Log.d(TAG, "onReceive(): $intent")
val notificationId = intent.getIntExtra(NOTIFICATION_ID_EXTRA, -1)
if (notificationId < 0) {
return
}
Log.d(TAG, "Dismissed notification $notificationId")
NotificationHelper(context).updateGroupNotification()
}

companion object {
private val TAG = NotificationDismissedReceiver::class.java.simpleName
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,11 @@ import org.openhab.habdroid.util.getPrefs
class NotificationHelper(private val context: Context) {
private val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

suspend fun showNotification(
message: CloudNotification,
deleteIntent: PendingIntent?,
summaryDeleteIntent: PendingIntent?
) {
suspend fun showNotification(message: CloudNotification) {
createChannelForSeverity(message.severity)

val n = makeNotification(message, message.idHash, deleteIntent)

val n = makeNotification(message, message.idHash, createDeleteIntent(message.idHash))
notificationManager.notify(message.idHash, n)

if (HAS_GROUPING_SUPPORT) {
val count = countCloudNotifications(notificationManager.activeNotifications)
if (count > 1) {
notificationManager.notify(
SUMMARY_NOTIFICATION_ID,
makeSummaryNotification(count, message.createdTimestamp, summaryDeleteIntent)
)
}
}
updateGroupNotification()
}

fun cancelNotification(notificationId: Int) {
Expand All @@ -79,10 +64,36 @@ class NotificationHelper(private val context: Context) {
for (n in active) {
notificationManager.cancel(n.id)
}
} else {
updateGroupNotification()
}
}
}

fun updateGroupNotification() {
if (!HAS_GROUPING_SUPPORT) {
return
}
val count = countCloudNotifications(notificationManager.activeNotifications)
if (count > 1) {
notificationManager.notify(
SUMMARY_NOTIFICATION_ID,
makeSummaryNotification(count, System.currentTimeMillis(), createDeleteIntent(SUMMARY_NOTIFICATION_ID))
)
}
}

private fun createDeleteIntent(notificationId: Int): PendingIntent {
val intent = Intent(context, NotificationDismissedReceiver::class.java)
intent.putExtra(NOTIFICATION_ID_EXTRA, notificationId)
return PendingIntent.getBroadcast(
context,
notificationId,
intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent_Immutable
)
}

private fun createChannelForSeverity(severity: String?) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return
Expand Down Expand Up @@ -185,7 +196,7 @@ class NotificationHelper(private val context: Context) {
fun makeSummaryNotification(
subNotificationCount: Int,
timestamp: Long,
deleteIntent: PendingIntent?
deleteIntent: PendingIntent
): Notification {
val text = context.resources.getQuantityString(
R.plurals.summary_notification_text,
Expand Down Expand Up @@ -240,6 +251,7 @@ class NotificationHelper(private val context: Context) {

companion object {
private val TAG = NotificationHelper::class.java.simpleName
const val NOTIFICATION_ID_EXTRA = "notification_id"

private fun getChannelId(severity: String?) = if (severity.isNullOrEmpty()) {
NotificationUpdateObserver.CHANNEL_ID_MESSAGE_DEFAULT
Expand Down

0 comments on commit 6e4a461

Please sign in to comment.