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