fix(notification): suppress only when active chat is visible
This commit is contained in:
parent
4ce585971d
commit
33c7ddb9ab
2 changed files with 34 additions and 14 deletions
|
|
@ -95,8 +95,7 @@ internal class NotificationHelper @Inject constructor(
|
||||||
senderId: String,
|
senderId: String,
|
||||||
senderUsername: String
|
senderUsername: String
|
||||||
): Boolean {
|
): Boolean {
|
||||||
// Suppress only while app is foreground and the related chat/messaging UI is visible.
|
if (shouldSuppressNotification(senderId)) return false
|
||||||
if (isAppInForeground && (isMessagingAreaVisible || senderId == activeChatPartnerId)) return false
|
|
||||||
|
|
||||||
val chatIntent = Intent(context, MainActivity::class.java).apply {
|
val chatIntent = Intent(context, MainActivity::class.java).apply {
|
||||||
flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP
|
flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP
|
||||||
|
|
@ -168,6 +167,15 @@ internal class NotificationHelper @Inject constructor(
|
||||||
return true
|
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).
|
/// Entfernt Benachrichtigungen für einen bestimmten Absender (wenn der Chat geöffnet wird).
|
||||||
fun cancelNotificationForSender(senderId: String) {
|
fun cancelNotificationForSender(senderId: String) {
|
||||||
val notificationManager = NotificationManagerCompat.from(context)
|
val notificationManager = NotificationManagerCompat.from(context)
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ import android.content.Context
|
||||||
import io.mockk.every
|
import io.mockk.every
|
||||||
import io.mockk.mockk
|
import io.mockk.mockk
|
||||||
import org.junit.Assert.assertFalse
|
import org.junit.Assert.assertFalse
|
||||||
import org.junit.Assert.assertNull
|
import org.junit.Assert.assertTrue
|
||||||
import org.junit.Before
|
import org.junit.Before
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
|
|
||||||
|
|
@ -28,36 +28,48 @@ class NotificationHelperTest {
|
||||||
@Test
|
@Test
|
||||||
fun test_showNewMessageNotification_activeChatMatchesSender_returnsFalse() {
|
fun test_showNewMessageNotification_activeChatMatchesSender_returnsFalse() {
|
||||||
// Given
|
// Given
|
||||||
|
notificationHelper.setAppInForegroundForTest(true)
|
||||||
notificationHelper.setActiveChat("user-123")
|
notificationHelper.setActiveChat("user-123")
|
||||||
|
notificationHelper.setMessagingAreaVisible(true)
|
||||||
|
|
||||||
// When
|
// When
|
||||||
val result = notificationHelper.showNewMessageNotification(
|
val result = notificationHelper.shouldSuppressNotification("user-123")
|
||||||
senderId = "user-123",
|
|
||||||
senderUsername = "Bob"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Then
|
// Then
|
||||||
assertFalse(result)
|
assertTrue(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun test_setActiveChat_setsAndClears() {
|
fun test_setActiveChat_setsAndClears() {
|
||||||
// Given
|
// Given
|
||||||
|
notificationHelper.setAppInForegroundForTest(true)
|
||||||
notificationHelper.setActiveChat("user-123")
|
notificationHelper.setActiveChat("user-123")
|
||||||
|
notificationHelper.setMessagingAreaVisible(true)
|
||||||
|
|
||||||
// When – message from same user is suppressed
|
// When – message from same user is suppressed
|
||||||
val suppressed = notificationHelper.showNewMessageNotification(
|
val suppressed = notificationHelper.shouldSuppressNotification("user-123")
|
||||||
senderId = "user-123",
|
|
||||||
senderUsername = "Bob"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Then
|
// Then
|
||||||
assertFalse(suppressed)
|
assertTrue(suppressed)
|
||||||
|
|
||||||
// When – clear active chat
|
// When – clear active chat
|
||||||
notificationHelper.setActiveChat(null)
|
notificationHelper.setActiveChat(null)
|
||||||
|
|
||||||
// Then – message from same user is no longer suppressed at the guard
|
// 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue