fix(notification): suppress only when active chat is visible

This commit is contained in:
Jens Reinemann 2026-05-18 17:59:54 +02:00
parent 4ce585971d
commit 33c7ddb9ab
2 changed files with 34 additions and 14 deletions

View file

@ -95,8 +95,7 @@ internal class NotificationHelper @Inject constructor(
senderId: String,
senderUsername: String
): Boolean {
// Suppress only while app is foreground and the related chat/messaging UI is visible.
if (isAppInForeground && (isMessagingAreaVisible || senderId == activeChatPartnerId)) return false
if (shouldSuppressNotification(senderId)) return false
val chatIntent = Intent(context, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP
@ -168,6 +167,15 @@ internal class NotificationHelper @Inject constructor(
return true
}
internal fun shouldSuppressNotification(senderId: String): Boolean {
// Suppress only when the app is foreground and the exact chat is currently visible.
return isAppInForeground && isMessagingAreaVisible && senderId == activeChatPartnerId
}
internal fun setAppInForegroundForTest(isForeground: Boolean) {
isAppInForeground = isForeground
}
/// Entfernt Benachrichtigungen für einen bestimmten Absender (wenn der Chat geöffnet wird).
fun cancelNotificationForSender(senderId: String) {
val notificationManager = NotificationManagerCompat.from(context)

View file

@ -5,7 +5,7 @@ import android.content.Context
import io.mockk.every
import io.mockk.mockk
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
@ -28,36 +28,48 @@ class NotificationHelperTest {
@Test
fun test_showNewMessageNotification_activeChatMatchesSender_returnsFalse() {
// Given
notificationHelper.setAppInForegroundForTest(true)
notificationHelper.setActiveChat("user-123")
notificationHelper.setMessagingAreaVisible(true)
// When
val result = notificationHelper.showNewMessageNotification(
senderId = "user-123",
senderUsername = "Bob"
)
val result = notificationHelper.shouldSuppressNotification("user-123")
// Then
assertFalse(result)
assertTrue(result)
}
@Test
fun test_setActiveChat_setsAndClears() {
// Given
notificationHelper.setAppInForegroundForTest(true)
notificationHelper.setActiveChat("user-123")
notificationHelper.setMessagingAreaVisible(true)
// When message from same user is suppressed
val suppressed = notificationHelper.showNewMessageNotification(
senderId = "user-123",
senderUsername = "Bob"
)
val suppressed = notificationHelper.shouldSuppressNotification("user-123")
// Then
assertFalse(suppressed)
assertTrue(suppressed)
// When clear active chat
notificationHelper.setActiveChat(null)
// Then message from same user is no longer suppressed at the guard
// (actual notification display needs Android runtime, tested via instrumentation)
assertFalse(notificationHelper.shouldSuppressNotification("user-123"))
}
@Test
fun test_showNewMessageNotification_activeChatNotVisible_notSuppressedByGuard() {
// Given
notificationHelper.setAppInForegroundForTest(true)
notificationHelper.setActiveChat("user-123")
notificationHelper.setMessagingAreaVisible(false)
// When
val result = notificationHelper.shouldSuppressNotification("user-123")
// Then
assertFalse(result)
}
}