-
Notifications
You must be signed in to change notification settings - Fork 0
[SOU-647] FCM 토큰 API 연동 #75
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
base: develop
Are you sure you want to change the base?
Changes from all commits
93f95e0
3dcdeb1
d0980a8
b3fde3d
12664ca
0e87720
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| final class AppContainer { | ||
| private static var stored: AppContainer? | ||
|
|
||
| static var shared: AppContainer { | ||
| guard let stored else { | ||
| fatalError("AppContainer.bootstrap(config:)를 먼저 호출해야 합니다.") | ||
| } | ||
| return stored | ||
| } | ||
|
|
||
| let factory: AppFactory | ||
|
|
||
| private lazy var pushTokenSyncerInstance: PushTokenSyncing = factory.makePushTokenSyncer() | ||
|
|
||
| var pushTokenSyncer: PushTokenSyncing { | ||
| pushTokenSyncerInstance | ||
| } | ||
|
|
||
| private init(config: AppConfiguration) { | ||
| factory = AppFactory(config: config) | ||
| } | ||
|
|
||
| static func bootstrap(config: AppConfiguration) { | ||
| guard stored == nil else { return } | ||
|
|
||
| stored = AppContainer(config: config) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import Domain | ||
| import FCM | ||
| import Logger | ||
| import Storage | ||
|
|
||
| protocol PushTokenSyncing: AnyObject { | ||
| func sync(fcmToken: String) async | ||
| func syncCurrentTokenIfAuthenticated() async | ||
| } | ||
|
|
||
| actor DefaultPushTokenSyncer: PushTokenSyncing { | ||
| private let registerFCMToken: RegisterFCMTokenUseCase | ||
| private let checkFullAuthentication: CheckFullAuthenticationUseCase | ||
| private let fcmTokenProvider: FCMTokenProviding | ||
| private let deviceIdStore: DeviceIdProviding | ||
|
|
||
| private struct RegistrationKey: Hashable { | ||
| let token: String | ||
| let deviceId: String | ||
| } | ||
|
|
||
| private var inFlightRegistrations = Set<RegistrationKey>() | ||
|
|
||
| init( | ||
| registerFCMToken: RegisterFCMTokenUseCase, | ||
| checkFullAuthentication: CheckFullAuthenticationUseCase, | ||
| fcmTokenProvider: FCMTokenProviding, | ||
| deviceIdStore: DeviceIdProviding | ||
| ) { | ||
| self.registerFCMToken = registerFCMToken | ||
| self.checkFullAuthentication = checkFullAuthentication | ||
| self.fcmTokenProvider = fcmTokenProvider | ||
| self.deviceIdStore = deviceIdStore | ||
| } | ||
|
|
||
| func sync(fcmToken: String) async { | ||
| await registerIfNeeded(fcmToken: fcmToken) | ||
| } | ||
|
|
||
| func syncCurrentTokenIfAuthenticated() async { | ||
| guard let fcmToken = await fcmTokenProvider.currentToken() else { return } | ||
|
|
||
| await registerIfNeeded(fcmToken: fcmToken) | ||
| } | ||
|
|
||
| private func registerIfNeeded(fcmToken: String) async { | ||
| guard await checkFullAuthentication.execute() else { return } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No FCM sync after loginMedium Severity FCM server registration runs only when the token stream emits or on Additional Locations (1)Reviewed by Cursor Bugbot for commit 0e87720. Configure here. |
||
|
|
||
| let deviceId: String | ||
| do { | ||
| deviceId = try await deviceIdStore.deviceId() | ||
| } catch { | ||
| Logger.shared.error( | ||
| "FCM 토큰 등록용 기기 ID 준비 실패: \(error.localizedDescription)", | ||
| category: .general | ||
| ) | ||
| return | ||
| } | ||
|
|
||
| let registrationKey = RegistrationKey(token: fcmToken, deviceId: deviceId) | ||
| guard !inFlightRegistrations.contains(registrationKey) else { return } | ||
| inFlightRegistrations.insert(registrationKey) | ||
| defer { inFlightRegistrations.remove(registrationKey) } | ||
|
|
||
| let deviceInfo = DeviceInfoProvider.current() | ||
| let registration = FCMRegistration( | ||
| token: fcmToken, | ||
| deviceId: deviceId, | ||
| deviceType: .ios, | ||
| deviceModel: deviceInfo.deviceModel, | ||
| osVersion: deviceInfo.osVersion, | ||
| appVersion: deviceInfo.appVersion | ||
| ) | ||
|
|
||
| do { | ||
| try await registerFCMToken.execute(registration: registration) | ||
| Logger.shared.info("FCM 토큰 서버 등록 완료", category: .general) | ||
| } catch { | ||
| Logger.shared.error( | ||
| "FCM 토큰 서버 등록 실패: \(error.localizedDescription)", | ||
| category: .general | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import UIKit | ||
| import Utils | ||
|
|
||
| struct DeviceInfo { | ||
| let deviceModel: String | ||
| let osVersion: String | ||
| let appVersion: String | ||
| } | ||
|
|
||
| enum DeviceInfoProvider { | ||
| static func current() -> DeviceInfo { | ||
| DeviceInfo( | ||
| deviceModel: deviceModel(), | ||
| osVersion: UIDevice.current.systemVersion, | ||
| appVersion: AppInfo.version | ||
| ) | ||
| } | ||
|
|
||
| private static func deviceModel() -> String { | ||
| var systemInfo = utsname() | ||
| uname(&systemInfo) | ||
|
|
||
| return withUnsafePointer(to: &systemInfo.machine) { | ||
| $0.withMemoryRebound(to: CChar.self, capacity: 1) { | ||
| String(cString: $0) | ||
| } | ||
| } | ||
| } | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| public protocol DeviceIdProviding: Sendable { | ||
| func deviceId() async throws -> String | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import Foundation | ||
| import Utils | ||
|
|
||
| public actor DeviceIdStore: DeviceIdProviding { | ||
| private let keychain: KeychainStorage | ||
| private var cachedDeviceId: String? | ||
|
|
||
| public init(keychain: KeychainStorage) { | ||
| self.keychain = keychain | ||
| } | ||
|
|
||
| public func deviceId() async throws -> String { | ||
| if let cachedDeviceId { | ||
| return cachedDeviceId | ||
| } | ||
|
|
||
| do { | ||
| if let stored: String = try await keychain.get(forKey: KeychainKey.deviceId) { | ||
| cachedDeviceId = stored | ||
| return stored | ||
| } | ||
| } catch KeychainError.itemNotFound { | ||
| // 저장된 ID가 없을 때만 새 ID를 발급합니다. | ||
| } catch { | ||
| throw error | ||
| } | ||
|
|
||
| let newDeviceId = UUID().uuidString | ||
| try await keychain.save(newDeviceId, forKey: KeychainKey.deviceId) | ||
| cachedDeviceId = newDeviceId | ||
| return newDeviceId | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| struct RegisterFCMTokenRequest: Encodable { | ||
| let token: String | ||
| let deviceId: String | ||
| let deviceType: String | ||
| let deviceModel: String? | ||
| let osVersion: String? | ||
| let appVersion: String? | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import Domain | ||
|
|
||
| enum FCMRequestMapper { | ||
| static func toRegisterRequest(_ registration: FCMRegistration) -> RegisterFCMTokenRequest { | ||
| RegisterFCMTokenRequest( | ||
| token: registration.token, | ||
| deviceId: registration.deviceId, | ||
| deviceType: registration.deviceType.rawValue, | ||
| deviceModel: registration.deviceModel, | ||
| osVersion: registration.osVersion, | ||
| appVersion: registration.appVersion | ||
| ) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import Networking | ||
|
|
||
| protocol FCMRemoteDataSource { | ||
| func register(request: RegisterFCMTokenRequest) async throws | ||
| func deactivate(deviceId: String) async throws | ||
| } | ||
|
|
||
| final class DefaultFCMRemoteDataSource: FCMRemoteDataSource { | ||
| private let authed: NetworkClient | ||
|
|
||
| init(authed: NetworkClient) { | ||
| self.authed = authed | ||
| } | ||
|
|
||
| func register(request: RegisterFCMTokenRequest) async throws { | ||
| let endpoint = FCMEndpoint.register( | ||
| token: request.token, | ||
| deviceId: request.deviceId, | ||
| deviceType: request.deviceType, | ||
| deviceModel: request.deviceModel, | ||
| osVersion: request.osVersion, | ||
| appVersion: request.appVersion | ||
| ) | ||
| let _: EmptyResponse = try await authed.request(endpoint) | ||
| } | ||
|
|
||
| func deactivate(deviceId: String) async throws { | ||
| let endpoint = FCMEndpoint.deactivate(deviceId: deviceId) | ||
| let _: EmptyResponse = try await authed.request(endpoint) | ||
| } | ||
| } |


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Split device ID stores desync
Medium Severity
Registration reads
deviceIdfrom aDeviceIdStorecreated inAppFactory, while logout deactivation uses a separate cached store inDataFactory. Two actors can each mint a UUID before either writes Keychain, so register and deactivate may use different IDs.Additional Locations (1)
Projects/Data/Sources/Util/Factory/DataFactory.swift#L208-L211Reviewed by Cursor Bugbot for commit 0e87720. Configure here.