Fix the app badge not resetting when the unread count is unchanged

The notification extension increments the app badge for every
notification it delivers, without the app knowing about it. The app
used to correct this drift by setting the badge from the room list on
every update, but since #6337 the NotificationService only publishes
the badge count when it changes.

Publish the badge count on every room list update again so the
NotificationManager always resets the app badge.
This commit is contained in:
Stefan Ceriu
2026-10-25 15:38:37 +02:00
committed by Mauro
parent 5c9e97a163
commit 27cdd980dc
3 changed files with 18 additions and 6 deletions
@@ -546,6 +546,7 @@ class UserSessionFlowCoordinator: FlowCoordinatorProtocol {
private func setupUnreadNotificationsObserver() {
userSession.notificationService.badgeCountPublisher
.removeDuplicates()
.weakAssign(to: \.badgeCount, on: chatsTabDetails)
.store(in: &cancellables)
}
@@ -95,7 +95,8 @@ final class NotificationService: NotificationServiceProtocol {
for await roomSummaries in roomListPublisher.values {
guard let self else { return }
updateBadgeCount(Int(clientProxy.totalUnreadNotifications))
// Sent even when unchanged as the extension may have moved the app badge since.
badgeCountSubject.send(Int(clientProxy.totalUnreadNotifications))
removeDeliveredNotificationsForFullyReadRooms(roomSummaries)
}
}
@@ -120,11 +121,6 @@ final class NotificationService: NotificationServiceProtocol {
}
}
private func updateBadgeCount(_ badgeCount: Int) {
guard badgeCount != badgeCountSubject.value else { return }
badgeCountSubject.send(badgeCount)
}
/// Sets the pusher when enabled and deletes it otherwise. Nothing to update until the device token is known.
private func updatePusher(isEnabled: Bool) async {
guard let deviceToken else { return }
@@ -165,6 +165,21 @@ final class NotificationServiceTests {
try await deferred.fulfill()
}
@Test
func roomListChangesRepublishAnUnchangedBadgeCount() async throws {
let roomListSubject = CurrentValueSubject<[RoomSummary], Never>([])
let roomSummaryProvider = RoomSummaryProviderMock(.init())
roomSummaryProvider.roomListPublisher = roomListSubject.asCurrentValuePublisher()
clientProxy.staticRoomSummaryProvider = roomSummaryProvider
clientProxy.totalUnreadNotifications = 3
let service = makeService()
// The extension bumps the app badge without the app knowing, so it needs resetting even when the count is the same.
let deferred = deferFulfillment(service.badgeCountPublisher.dropFirst()) { $0 == 3 }
roomListSubject.send([])
try await deferred.fulfill()
}
// MARK: - Delivered notifications
@Test