diff --git a/SessionMessagingKit/Sending & Receiving/Notifications/Models/LegacyUnsubscribeRequest.swift b/SessionMessagingKit/Sending & Receiving/Notifications/Models/LegacyUnsubscribeRequest.swift new file mode 100644 index 000000000..663bafb17 --- /dev/null +++ b/SessionMessagingKit/Sending & Receiving/Notifications/Models/LegacyUnsubscribeRequest.swift @@ -0,0 +1,14 @@ +// Copyright © 2023 Rangeproof Pty Ltd. All rights reserved. + +import Foundation +import SessionSnodeKit + +extension PushNotificationAPI { + struct LegacyUnsubscribeRequest: Codable { + private let token: String + + init(token: String) { + self.token = token + } + } +} diff --git a/SessionMessagingKit/Sending & Receiving/Notifications/PushNotificationAPI.swift b/SessionMessagingKit/Sending & Receiving/Notifications/PushNotificationAPI.swift index 4d1c0148a..bb5d6ce8d 100644 --- a/SessionMessagingKit/Sending & Receiving/Notifications/PushNotificationAPI.swift +++ b/SessionMessagingKit/Sending & Receiving/Notifications/PushNotificationAPI.swift @@ -115,7 +115,42 @@ public enum PushNotificationAPI { } } ) - .map { _ in () } + .flatMap { _ in + guard UserDefaults.standard[.hasUnregisteredForLegacyPushNotifications] != true else { + return Just(()) + .setFailureType(to: Error.self) + .eraseToAnyPublisher() + } + + return PushNotificationAPI + .send( + request: PushNotificationAPIRequest( + endpoint: .legacyUnregister, + body: LegacyUnsubscribeRequest( + token: hexEncodedToken + ) + ) + ) + .retry(maxRetryCount) + .handleEvents( + receiveCompletion: { result in + switch result { + case .finished: + /// Save that we've already unsubscribed + /// + /// **Note:** The server can return an error (`response.code != 0`) but + /// that means the server properly processed the request and the error is likely + /// due to the device not actually being previously subscribed for notifications + /// rather than actually failing to unsubscribe + UserDefaults.standard[.hasUnregisteredForLegacyPushNotifications] = true + + case .failure: SNLog("Couldn't unsubscribe for legacy notifications.") + } + } + ) + .map { _ in () } + .eraseToAnyPublisher() + } .eraseToAnyPublisher() ].appending( // FIXME: Remove this once legacy groups are deprecated diff --git a/SessionUtilitiesKit/General/SNUserDefaults.swift b/SessionUtilitiesKit/General/SNUserDefaults.swift index 82f18bd64..910461a3d 100644 --- a/SessionUtilitiesKit/General/SNUserDefaults.swift +++ b/SessionUtilitiesKit/General/SNUserDefaults.swift @@ -31,6 +31,7 @@ public enum SNUserDefaults { case hasSeenCallIPExposureWarning case hasSeenCallMissedTips case isUsingFullAPNs + case hasUnregisteredForLegacyPushNotifications case wasUnlinked case isMainAppActive case isCallOngoing