Skip to content
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

fix: Unregister from APNS if user disconnected #971

Merged
merged 1 commit into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
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
11 changes: 9 additions & 2 deletions Mail/Helpers/Notifications/NotificationCenterDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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(
Expand Down
4 changes: 4 additions & 0 deletions Mail/Proxy/Implementation/RemoteNotificationRegistrer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ public final class RemoteNotificationRegistrer: RemoteNotificationRegistrable {
public func register() {
UIApplication.shared.registerForRemoteNotifications()
}

public func unregister() {
UIApplication.shared.unregisterForRemoteNotifications()
}
}
1 change: 1 addition & 0 deletions Mail/Proxy/Protocols/RemoteNotificationRegistrable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ import Foundation
/// Something that can register remote notifications
public protocol RemoteNotificationRegistrable {
func register()
func unregister()
}
4 changes: 4 additions & 0 deletions MailShareExtension/Proxy/RemoteNotificationRegistrer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ public final class RemoteNotificationRegistrer: RemoteNotificationRegistrable {
public func register() {
// NOOP in share extension
}

public func unregister() {
// NOOP in share extension
}
}
Loading