From e8749debec3ecdc361b29acc5f52a8a07d22641e Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Thu, 4 Aug 2022 22:25:52 +0100 Subject: [PATCH] ios: fix notification badge count (#903) --- apps/ios/Shared/Model/ChatModel.swift | 5 ++++- apps/ios/Shared/SimpleXApp.swift | 1 + apps/ios/SimpleX NSE/NotificationService.swift | 9 +++++++-- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/apps/ios/Shared/Model/ChatModel.swift b/apps/ios/Shared/Model/ChatModel.swift index 5cbc708bbc..8a25d57de2 100644 --- a/apps/ios/Shared/Model/ChatModel.swift +++ b/apps/ios/Shared/Model/ChatModel.swift @@ -162,7 +162,10 @@ final class ChatModel: ObservableObject { if case .rcvNew = cItem.meta.itemStatus { DispatchQueue.main.asyncAfter(deadline: .now() + 1) { if self.chatId == cInfo.id { - Task { await apiMarkChatItemRead(cInfo, cItem) } + Task { + await apiMarkChatItemRead(cInfo, cItem) + NtfManager.shared.decNtfBadgeCount() + } } } } diff --git a/apps/ios/Shared/SimpleXApp.swift b/apps/ios/Shared/SimpleXApp.swift index 949a10bda5..8187de64ec 100644 --- a/apps/ios/Shared/SimpleXApp.swift +++ b/apps/ios/Shared/SimpleXApp.swift @@ -58,6 +58,7 @@ struct SimpleXApp: App { enteredBackground = ProcessInfo.processInfo.systemUptime } doAuthenticate = false + NtfManager.shared.setNtfBadgeCount(chatModel.totalUnreadCount()) case .active: if chatModel.chatRunning == true { ChatReceiver.shared.start() diff --git a/apps/ios/SimpleX NSE/NotificationService.swift b/apps/ios/SimpleX NSE/NotificationService.swift index e107a8bdf2..be039a65e5 100644 --- a/apps/ios/SimpleX NSE/NotificationService.swift +++ b/apps/ios/SimpleX NSE/NotificationService.swift @@ -63,8 +63,6 @@ class NotificationService: UNNotificationServiceExtension { override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { logger.debug("NotificationService.didReceive") - badgeCount = ntfBadgeCountGroupDefault.get() + 1 - ntfBadgeCountGroupDefault.set(badgeCount) setBestAttemptNtf(request.content.mutableCopy() as? UNMutableNotificationContent) self.contentHandler = contentHandler registerGroupDefaults() @@ -72,9 +70,11 @@ class NotificationService: UNNotificationServiceExtension { switch appState { case .suspended: logger.debug("NotificationService: app is suspended") + setBadgeCount() receiveNtfMessages(request, contentHandler) case .suspending: logger.debug("NotificationService: app is suspending") + setBadgeCount() Task { var state = appState for _ in 1...5 { @@ -131,6 +131,11 @@ class NotificationService: UNNotificationServiceExtension { deliverBestAttemptNtf() } + func setBadgeCount() { + badgeCount = ntfBadgeCountGroupDefault.get() + 1 + ntfBadgeCountGroupDefault.set(badgeCount) + } + func setBestAttemptNtf(_ ntf: UNMutableNotificationContent?) { logger.debug("NotificationService.setBestAttemptNtf") bestAttemptNtf = ntf