diff --git a/PLUB/AppDelegate.swift b/PLUB/AppDelegate.swift index 9f8132a03..29a8f31be 100644 --- a/PLUB/AppDelegate.swift +++ b/PLUB/AppDelegate.swift @@ -10,6 +10,7 @@ import UIKit import GoogleSignIn import KakaoSDKCommon import FirebaseCore +import FirebaseMessaging @main class AppDelegate: UIResponder, UIApplicationDelegate { @@ -24,6 +25,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { ) FirebaseApp.configure() + configureCloudMessaging(application) setupNavigationBarStyle() @@ -39,6 +41,18 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set) { } + // MARK: Configuration Methods + + private func configureCloudMessaging(_ application: UIApplication) { + UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { _, _ in + + } + + application.registerForRemoteNotifications() + + Messaging.messaging().delegate = self + } + private func setupNavigationBarStyle() { let appearance = UINavigationBarAppearance() @@ -60,3 +74,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate { } } +// MARK: - MessagingDelegate + +extension AppDelegate: MessagingDelegate { + func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) { + guard let fcmToken else { return } + Log.notice(fcmToken) + UserManager.shared.set(fcmToken: fcmToken) + } +} diff --git a/PLUB/Configuration/Manager/UserManager.swift b/PLUB/Configuration/Manager/UserManager.swift index 4ab4bfcee..5f35e8a8a 100644 --- a/PLUB/Configuration/Manager/UserManager.swift +++ b/PLUB/Configuration/Manager/UserManager.swift @@ -32,6 +32,9 @@ final class UserManager { @KeyChainWrapper(key: "refreshToken") private(set) var refreshToken + @KeyChainWrapper(key: "fcmToken") + private(set) var fcmToken + // MARK: - Social Login Type @UserDefaultsWrapper(key: "socialType") @@ -61,6 +64,11 @@ extension UserManager { self.signToken = signToken } + /// 플럽 푸시 알림에 필요한 `firebase cloud messaging Token`을 세팅합니다. + func set(fcmToken: String) { + self.fcmToken = fcmToken + } + /// 소셜로그인 타입을 세팅합니다. /// - Parameter socialType: 소셜로그인 타입(애플, 구글, 카카오) func set(socialType: SocialType) { @@ -77,6 +85,7 @@ extension UserManager { accessToken = nil refreshToken = nil signToken = nil + fcmToken = nil socialType = nil } diff --git a/PLUB/Sources/Models/Auth/Request/SignInRequest.swift b/PLUB/Sources/Models/Auth/Request/SignInRequest.swift index 1fc2e82ad..b32d668a4 100644 --- a/PLUB/Sources/Models/Auth/Request/SignInRequest.swift +++ b/PLUB/Sources/Models/Auth/Request/SignInRequest.swift @@ -10,6 +10,9 @@ import Foundation /// 소셜 로그인 요청 모델 struct SignInRequest: Codable { + /// Firebase Cloud Messaging Token + let fcmToken: String + /// 액세스 토큰(카카오, 애플) /// 애플의 경우 identityToken을 해당 값으로 처리해야합니다. let accessToken: String? diff --git a/PLUB/Sources/Models/Auth/Request/SignUpRequest.swift b/PLUB/Sources/Models/Auth/Request/SignUpRequest.swift index e3fcc1f9a..375defe2f 100644 --- a/PLUB/Sources/Models/Auth/Request/SignUpRequest.swift +++ b/PLUB/Sources/Models/Auth/Request/SignUpRequest.swift @@ -11,6 +11,10 @@ import Then /// 회원가입 요청 모델 struct SignUpRequest: Codable { + + /// Firebase Cloud Messaging Token + let fcmToken: String + /// sign token var signToken: String @@ -48,6 +52,7 @@ struct SignUpRequest: Codable { var marketing: Bool init() { + fcmToken = UserManager.shared.fcmToken! signToken = UserManager.shared.signToken! categoryList = [] birthday = "" @@ -66,6 +71,7 @@ struct SignUpRequest: Codable { extension SignUpRequest { enum CodingKeys: String, CodingKey { + case fcmToken case signToken case categoryList case profileImageLink = "profileImage" diff --git a/PLUB/Sources/Network/Services/AuthService.swift b/PLUB/Sources/Network/Services/AuthService.swift index 45679de69..2e3eeb5fc 100644 --- a/PLUB/Sources/Network/Services/AuthService.swift +++ b/PLUB/Sources/Network/Services/AuthService.swift @@ -23,7 +23,14 @@ extension AuthService { authorizationCode: String? ) -> PLUBResult { return sendRequest( - AuthRouter.socialLogin(SignInRequest(accessToken: token, authorizationCode: authorizationCode, socialType: socialType)), + AuthRouter.socialLogin( + SignInRequest( + fcmToken: UserManager.shared.fcmToken!, + accessToken: token, + authorizationCode: authorizationCode, + socialType: socialType + ) + ), type: SignInResponse.self ) }