From 27cdd980dcce083b939935521fdcc9a754e18244 Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Sun, 25 Oct 2026 15:38:37 +0200 Subject: [PATCH] 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. --- .../UserSessionFlowCoordinator.swift | 1 + .../Service/NotificationService.swift | 8 ++------ .../NotificationServiceTests.swift | 15 +++++++++++++++ 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/ElementX/Sources/FlowCoordinators/UserSessionFlowCoordinator.swift b/ElementX/Sources/FlowCoordinators/UserSessionFlowCoordinator.swift index 6eafb981e..34844901f 100644 --- a/ElementX/Sources/FlowCoordinators/UserSessionFlowCoordinator.swift +++ b/ElementX/Sources/FlowCoordinators/UserSessionFlowCoordinator.swift @@ -546,6 +546,7 @@ class UserSessionFlowCoordinator: FlowCoordinatorProtocol { private func setupUnreadNotificationsObserver() { userSession.notificationService.badgeCountPublisher + .removeDuplicates() .weakAssign(to: \.badgeCount, on: chatsTabDetails) .store(in: &cancellables) } diff --git a/ElementX/Sources/Services/Notification/Service/NotificationService.swift b/ElementX/Sources/Services/Notification/Service/NotificationService.swift index 2e307cdb3..8d3933b88 100644 --- a/ElementX/Sources/Services/Notification/Service/NotificationService.swift +++ b/ElementX/Sources/Services/Notification/Service/NotificationService.swift @@ -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 } diff --git a/UnitTests/Sources/NotificationManager/NotificationServiceTests.swift b/UnitTests/Sources/NotificationManager/NotificationServiceTests.swift index 732e3d6de..a14d0f848 100644 --- a/UnitTests/Sources/NotificationManager/NotificationServiceTests.swift +++ b/UnitTests/Sources/NotificationManager/NotificationServiceTests.swift @@ -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