Update the app badge after receiving a notification by reading and using the unreadCount from the request payload

This commit is contained in:
Stefan Ceriu 2023-09-05 15:35:45 +03:00 committed by Stefan Ceriu
parent e9a945cfe4
commit 3bc474fb70

View File

@ -60,7 +60,8 @@ class NotificationServiceExtension: UNNotificationServiceExtension {
Task { Task {
await run(with: credentials, await run(with: credentials,
roomId: roomId, roomId: roomId,
eventId: eventId) eventId: eventId,
unreadCount: request.unreadCount)
} }
} }
@ -73,7 +74,8 @@ class NotificationServiceExtension: UNNotificationServiceExtension {
private func run(with credentials: KeychainCredentials, private func run(with credentials: KeychainCredentials,
roomId: String, roomId: String,
eventId: String) async { eventId: String,
unreadCount: Int?) async {
MXLog.info("\(tag) run with roomId: \(roomId), eventId: \(eventId)") MXLog.info("\(tag) run with roomId: \(roomId), eventId: \(eventId)")
do { do {
@ -102,6 +104,12 @@ class NotificationServiceExtension: UNNotificationServiceExtension {
modifiedContent = latestContent modifiedContent = latestContent
} }
// We still notify, but without the media attachment if it fails to load // 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() return notify()
} catch { } catch {
MXLog.error("NSE run error: \(error)") MXLog.error("NSE run error: \(error)")