Skip to content

fix: Clear notification AND update badge in background #1759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 35 additions & 14 deletions MailCore/Cache/RefreshAppBackgroundTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,10 @@ public class RefreshAppBackgroundTask: BackgroundTaskable {
return
}

let notificationCenter = UNUserNotificationCenter.current()
let deliveredNotifications = await notificationCenter.deliveredNotifications()

let mailboxObjectIds: [String] = deliveredNotifications.compactMap { notification in
let content = notification.request.content

guard let mailboxId = content.userInfo[NotificationsHelper.UserInfoKeys.mailboxId] as? Int,
let userId = content.userInfo[NotificationsHelper.UserInfoKeys.userId] as? Int else {
return nil
}
let mailboxWithNotificationsObjectIds = await getMailboxWithNotifications()
let mailboxWithUnreadObjectIds = getMailboxWithUnreadMessages()

return MailboxInfosManager.getObjectId(mailboxId: mailboxId, userId: userId)
}

let uniqueMailboxObjectIds = Set(mailboxObjectIds)
let uniqueMailboxObjectIds = Set(mailboxWithNotificationsObjectIds + mailboxWithUnreadObjectIds)

let mailboxManagersToRefresh: [(MailboxManager, Folder)] = uniqueMailboxObjectIds.compactMap { mailboxObjectId in
guard let mailbox = mailboxInfosManager.getMailbox(objectId: mailboxObjectId),
Expand Down Expand Up @@ -131,4 +120,36 @@ public class RefreshAppBackgroundTask: BackgroundTaskable {
}
}
}

private func getMailboxWithNotifications() async -> [String] {
let notificationCenter = UNUserNotificationCenter.current()
let deliveredNotifications = await notificationCenter.deliveredNotifications()

let mailboxObjectIds: [String] = deliveredNotifications.compactMap { notification in
let content = notification.request.content

guard let mailboxId = content.userInfo[NotificationsHelper.UserInfoKeys.mailboxId] as? Int,
let userId = content.userInfo[NotificationsHelper.UserInfoKeys.userId] as? Int else {
return nil
}

return MailboxInfosManager.getObjectId(mailboxId: mailboxId, userId: userId)
}

return mailboxObjectIds
}

private func getMailboxWithUnreadMessages() -> [String] {
let mailboxObjectIds: [String] = accountManager.accounts.compactMap { account in
for mailbox in mailboxInfosManager.getMailboxes(for: account.userId) {
if let mailboxManager = accountManager.getMailboxManager(for: mailbox),
(mailboxManager.getFolder(with: .inbox)?.unreadCount ?? 0) > 0 {
return mailbox.objectId
}
}
return nil
}

return mailboxObjectIds
}
}
Loading