From c88c977a2934c3f720f089c83ea3d22f399634de Mon Sep 17 00:00:00 2001 From: Doy Kim Date: Sun, 26 Feb 2023 18:29:08 +0900 Subject: [PATCH] =?UTF-8?q?[Setting]=20#40=20APNS=20=EB=93=B1=EB=A1=9D,=20?= =?UTF-8?q?=ED=8C=8C=EC=9D=B4=EC=96=B4=EB=B2=A0=EC=9D=B4=EC=8A=A4=20?= =?UTF-8?q?=EB=A9=94=EC=8B=9C=EC=A7=80=20=EB=8C=80=EB=A6=AC=EC=9E=90=20?= =?UTF-8?q?=EC=84=A4=EC=A0=95,=20FCM=20=ED=86=A0=ED=81=B0=20=EA=B0=B1?= =?UTF-8?q?=EC=8B=A0=20=EB=AA=A8=EB=8B=88=ED=84=B0=EB=A7=81=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wowmate/App/AppDelegate.swift | 51 +++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/wowmate/App/AppDelegate.swift b/wowmate/App/AppDelegate.swift index 6a247bb..5a06460 100644 --- a/wowmate/App/AppDelegate.swift +++ b/wowmate/App/AppDelegate.swift @@ -8,6 +8,7 @@ import UIKit import IQKeyboardManagerSwift import FirebaseCore +import FirebaseMessaging @main class AppDelegate: UIResponder, UIApplicationDelegate { @@ -19,8 +20,31 @@ class AppDelegate: UIResponder, UIApplicationDelegate { IQKeyboardManager.shared.enable = true IQKeyboardManager.shared.enableAutoToolbar = false IQKeyboardManager.shared.shouldResignOnTouchOutside = true // 화면 아무 곳이나 터치 시 키보드 dismiss + + /** APNS에 등록 */ + UNUserNotificationCenter.current().delegate = self + + let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] + UNUserNotificationCenter.current().requestAuthorization( + options: authOptions, + completionHandler: { _, _ in } + ) + application.registerForRemoteNotifications() + /** Firebase */ FirebaseApp.configure() + // 메시지 대리자 설정 + Messaging.messaging().delegate = self + // 현재 등록된 토큰 가져오기 + Messaging.messaging().token { token, error in + if let error = error { + print("Error fetching FCM registration token: \(error)") + } else if let token = token { + print("FCM registration token: \(token)") + //self.fcmRegTokenMessage.text = "Remote FCM registration token: \(token)" + } + } + return true } @@ -40,4 +64,31 @@ class AppDelegate: UIResponder, UIApplicationDelegate { } +extension AppDelegate: UNUserNotificationCenterDelegate { + func application(application: UIApplication, + didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { + Messaging.messaging().apnsToken = deviceToken + } + // 포그라운드 상태에서도 알림: 로컬/푸시 동일 + func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { + // .banner, .list: iOS 14+부터 사용가능 + completionHandler([.badge, .banner, .list]) + } +} +extension AppDelegate: MessagingDelegate { + // 토큰 갱신 모니터링 + func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) { + print("Firebase registration token: \(String(describing: fcmToken))") + + let dataDict: [String: String] = ["token": fcmToken ?? ""] + NotificationCenter.default.post( + name: Notification.Name("FCMToken"), + object: nil, + userInfo: dataDict + ) + // TODO: If necessary send token to application server. + // Note: This callback is fired at each app startup and whenever a new token is generated. + } + +}