Skip to content

Commit e0de052

Browse files
committed
fix: update last notified correctly, notification delay (WPB-22701)
1 parent 1ef3574 commit e0de052

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

app/src/main/kotlin/com/wire/android/notification/MessageNotificationManager.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import com.wire.kalium.logic.data.id.ConversationId
4040
import com.wire.kalium.logic.data.id.QualifiedID
4141
import com.wire.kalium.logic.data.notification.LocalNotification
4242
import com.wire.kalium.logic.data.notification.LocalNotificationUpdateMessageAction
43+
import kotlinx.coroutines.delay
4344
import javax.inject.Inject
4445
import javax.inject.Singleton
4546

@@ -52,14 +53,16 @@ class MessageNotificationManager
5253
private val notificationManager: NotificationManager,
5354
private val lockCodeTimeManager: LockCodeTimeManager
5455
) {
55-
56-
fun handleNotification(newNotifications: List<LocalNotification>, userId: QualifiedID, userName: String) {
56+
suspend fun handleNotification(newNotifications: List<LocalNotification>, userId: QualifiedID, userName: String) {
5757
if (newNotifications.isEmpty()) return
5858

5959
addNotifications(newNotifications, userId, userName)
6060
updateNotifications(newNotifications, userId)
6161
removeSeenNotifications(newNotifications, userId)
6262

63+
// This delay is required to let notification manager update activeNotifications list
64+
delay(NOTIFICATION_UPDATE_DELAY)
65+
6366
appLogger.i("$TAG: handled notifications: newNotifications size ${newNotifications.size}; ")
6467
}
6568

@@ -456,6 +459,7 @@ class MessageNotificationManager
456459
companion object {
457460
private const val TAG = "MessageNotificationManager"
458461
private const val MESSAGE_ID_EXTRA = "message_id"
462+
private const val NOTIFICATION_UPDATE_DELAY = 100L
459463

460464
/**
461465
* Update notification by adding [replyText] to the end of messages list with "You" as sender.

app/src/main/kotlin/com/wire/android/notification/WireNotificationManager.kt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import kotlinx.coroutines.flow.stateIn
6363
import kotlinx.coroutines.launch
6464
import kotlinx.coroutines.sync.Mutex
6565
import kotlinx.coroutines.sync.withLock
66+
import kotlinx.datetime.Instant
6667
import java.net.UnknownHostException
6768
import java.util.concurrent.ConcurrentHashMap
6869
import java.util.concurrent.atomic.AtomicReference
@@ -476,9 +477,14 @@ class WireNotificationManager @Inject constructor(
476477
)
477478
}
478479

479-
newNotifications.map { it.conversationId }.distinct().forEach { notifiedConversationId ->
480-
markMessagesAsNotified(userId, notifiedConversationId)
481-
}
480+
newNotifications
481+
.filterIsInstance<LocalNotification.Conversation>()
482+
.filter { it.messages.isNotEmpty() }
483+
.forEach { conversation ->
484+
val lastNotified = conversation.messages.maxOf { it.time }
485+
markMessagesAsNotified(userId, conversation.id, lastNotified)
486+
}
487+
482488
markConnectionAsNotified(userId)
483489
}
484490
}
@@ -521,10 +527,11 @@ class WireNotificationManager @Inject constructor(
521527

522528
private suspend fun markMessagesAsNotified(
523529
userId: QualifiedID,
524-
conversationId: ConversationId? = null
530+
conversationId: ConversationId? = null,
531+
lastNotified: Instant? = null,
525532
) {
526533
val markNotified = conversationId?.let {
527-
MarkMessagesAsNotifiedUseCase.UpdateTarget.SingleConversation(conversationId)
534+
MarkMessagesAsNotifiedUseCase.UpdateTarget.SingleConversation(conversationId, lastNotified)
528535
} ?: MarkMessagesAsNotifiedUseCase.UpdateTarget.AllConversations
529536
coreLogic.getSessionScope(userId)
530537
.messages

0 commit comments

Comments
 (0)