diff --git a/Mail/Helpers/Notifications/NotificationCenterDelegate.swift b/Mail/Helpers/Notifications/NotificationCenterDelegate.swift index 581f0c423..f866dd325 100644 --- a/Mail/Helpers/Notifications/NotificationCenterDelegate.swift +++ b/Mail/Helpers/Notifications/NotificationCenterDelegate.swift @@ -32,13 +32,14 @@ final class NotificationCenterDelegate: NSObject, UNUserNotificationCenterDelega @LazyInjectService private var accountManager: AccountManager @LazyInjectService private var messageActions: MessageActionHandlable @LazyInjectService private var mailboxInfosManager: MailboxInfosManager + @LazyInjectService private var remoteNotificationRegistrer: RemoteNotificationRegistrable + @LazyInjectService private var tokenStore: TokenStore /// Handles the actions related to mail notifications /// - Parameters: /// - identifier: the notification type string identifier /// - content: the notification content - internal func handleMailAction(for identifier: String, content: UNNotificationContent) async { - // precond for mail actions + func handleMailAction(for identifier: String, content: UNNotificationContent) async { guard let mailboxId = content.userInfo[NotificationsHelper.UserInfoKeys.mailboxId] as? Int, let userId = content.userInfo[NotificationsHelper.UserInfoKeys.userId] as? Int, let mailbox = mailboxInfosManager.getMailbox(id: mailboxId, userId: userId), @@ -48,6 +49,12 @@ final class NotificationCenterDelegate: NSObject, UNUserNotificationCenterDelega return } + let isUserConnected = tokenStore.tokenFor(userId: userId) != nil + guard isUserConnected else { + remoteNotificationRegistrer.unregister() + return + } + switch identifier { case UNNotificationDefaultActionIdentifier: try? await messageActions.handleTapOnNotification( diff --git a/Mail/Proxy/Implementation/RemoteNotificationRegistrer.swift b/Mail/Proxy/Implementation/RemoteNotificationRegistrer.swift index 35a754849..632c309cf 100644 --- a/Mail/Proxy/Implementation/RemoteNotificationRegistrer.swift +++ b/Mail/Proxy/Implementation/RemoteNotificationRegistrer.swift @@ -24,4 +24,8 @@ public final class RemoteNotificationRegistrer: RemoteNotificationRegistrable { public func register() { UIApplication.shared.registerForRemoteNotifications() } + + public func unregister() { + UIApplication.shared.unregisterForRemoteNotifications() + } } diff --git a/Mail/Proxy/Protocols/RemoteNotificationRegistrable.swift b/Mail/Proxy/Protocols/RemoteNotificationRegistrable.swift index d84c57682..3867f250b 100644 --- a/Mail/Proxy/Protocols/RemoteNotificationRegistrable.swift +++ b/Mail/Proxy/Protocols/RemoteNotificationRegistrable.swift @@ -21,4 +21,5 @@ import Foundation /// Something that can register remote notifications public protocol RemoteNotificationRegistrable { func register() + func unregister() } diff --git a/MailShareExtension/Proxy/RemoteNotificationRegistrer.swift b/MailShareExtension/Proxy/RemoteNotificationRegistrer.swift index c3ecc243e..7dc3edd1c 100644 --- a/MailShareExtension/Proxy/RemoteNotificationRegistrer.swift +++ b/MailShareExtension/Proxy/RemoteNotificationRegistrer.swift @@ -24,4 +24,8 @@ public final class RemoteNotificationRegistrer: RemoteNotificationRegistrable { public func register() { // NOOP in share extension } + + public func unregister() { + // NOOP in share extension + } }