do a better job of dimissing empty group notification containers

This commit is contained in:
Egor 2026-02-27 09:46:12 -08:00
parent 04a1a457c1
commit 47e2a138c1
3 changed files with 17 additions and 4 deletions

View file

@ -1 +1 @@
0.2.2 0.2.3

View file

@ -1,12 +1,12 @@
package app.lonecloud.prism.receivers package app.lonecloud.prism.receivers
import android.app.NotificationManager
import android.content.BroadcastReceiver import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.util.Log import android.util.Log
import app.lonecloud.prism.PrismPreferences import app.lonecloud.prism.PrismPreferences
import app.lonecloud.prism.utils.HttpClientFactory import app.lonecloud.prism.utils.HttpClientFactory
import app.lonecloud.prism.utils.ManualAppNotifications
import app.lonecloud.prism.utils.TAG import app.lonecloud.prism.utils.TAG
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -41,8 +41,7 @@ class NotificationActionReceiver : BroadcastReceiver() {
Log.d(TAG, "Notification action triggered: $actionLabel ($actionID) for channel $channelID") Log.d(TAG, "Notification action triggered: $actionLabel ($actionID) for channel $channelID")
if (notificationTag.isNotEmpty() && notificationId != -1) { if (notificationTag.isNotEmpty() && notificationId != -1) {
(context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager) ManualAppNotifications.dismissNotification(context, notificationTag)
.cancel(notificationTag, notificationId)
} }
val pendingResult = goAsync() val pendingResult = goAsync()

View file

@ -22,6 +22,7 @@ private const val MANUAL_CHANNEL_PREFIX = "manual_app_"
object ManualAppNotifications { object ManualAppNotifications {
private val notificationIds = mutableMapOf<String, Int>() private val notificationIds = mutableMapOf<String, Int>()
private val notificationConnectorTokens = mutableMapOf<String, String>()
private val summaryNotificationIds = mutableMapOf<String, Int>() private val summaryNotificationIds = mutableMapOf<String, Int>()
private var nextNotificationId = NOTIFICATION_BASE_ID private var nextNotificationId = NOTIFICATION_BASE_ID
@ -48,6 +49,7 @@ object ManualAppNotifications {
val hasMessage = payload.message.isNotBlank() val hasMessage = payload.message.isNotBlank()
val notificationId = getNotificationId(payload.tag) val notificationId = getNotificationId(payload.tag)
notificationConnectorTokens[payload.tag] = app.connectorToken
val packageName = resolveTargetPackage(app) val packageName = resolveTargetPackage(app)
val contentTitle = if (hasTitle) payload.title else appTitle val contentTitle = if (hasTitle) payload.title else appTitle
@ -129,16 +131,28 @@ object ManualAppNotifications {
fun dismissNotification(context: Context, tag: String) { fun dismissNotification(context: Context, tag: String) {
val notificationId = notificationIds[tag] val notificationId = notificationIds[tag]
val connectorToken = notificationConnectorTokens[tag]
if (notificationId != null) { if (notificationId != null) {
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.cancel(tag, notificationId) notificationManager.cancel(tag, notificationId)
notificationIds.remove(tag) notificationIds.remove(tag)
notificationConnectorTokens.remove(tag)
connectorToken?.let { dismissSummaryIfGroupEmpty(context, it) }
Log.d(TAG, "Dismissed notification with tag: $tag") Log.d(TAG, "Dismissed notification with tag: $tag")
} else { } else {
Log.w(TAG, "Cannot dismiss notification - tag not found: $tag") Log.w(TAG, "Cannot dismiss notification - tag not found: $tag")
} }
} }
private fun dismissSummaryIfGroupEmpty(context: Context, connectorToken: String) {
val hasRemainingChildren = notificationConnectorTokens.values.any { it == connectorToken }
if (hasRemainingChildren) return
val summaryId = summaryNotificationIds.remove(connectorToken) ?: return
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.cancel(summaryId)
}
private fun createNotificationChannel( private fun createNotificationChannel(
context: Context, context: Context,
channelId: String, channelId: String,