From 3bc474fb702f51822de07da18044436ca8126c5b Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Tue, 5 Sep 2023 15:35:45 +0300 Subject: [PATCH] Update the app badge after receiving a notification by reading and using the unreadCount from the request payload --- NSE/Sources/NotificationServiceExtension.swift | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/NSE/Sources/NotificationServiceExtension.swift b/NSE/Sources/NotificationServiceExtension.swift index 3e283b66b..eef6b5d9a 100644 --- a/NSE/Sources/NotificationServiceExtension.swift +++ b/NSE/Sources/NotificationServiceExtension.swift @@ -60,7 +60,8 @@ class NotificationServiceExtension: UNNotificationServiceExtension { Task { await run(with: credentials, roomId: roomId, - eventId: eventId) + eventId: eventId, + unreadCount: request.unreadCount) } } @@ -73,7 +74,8 @@ class NotificationServiceExtension: UNNotificationServiceExtension { private func run(with credentials: KeychainCredentials, roomId: String, - eventId: String) async { + eventId: String, + unreadCount: Int?) async { MXLog.info("\(tag) run with roomId: \(roomId), eventId: \(eventId)") do { @@ -102,6 +104,12 @@ class NotificationServiceExtension: UNNotificationServiceExtension { modifiedContent = latestContent } // We still notify, but without the media attachment if it fails to load + + // Finally update the app badge + if let unreadCount { + modifiedContent?.badge = NSNumber(value: unreadCount) + } + return notify() } catch { MXLog.error("NSE run error: \(error)")