Skip to content

Commit

Permalink
[Setting] #40 APNS 등록, 파이어베이스 메시지 대리자 설정, FCM 토큰 갱신 모니터링 메서드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
heydoy committed Feb 26, 2023
1 parent 82b179f commit c88c977
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions wowmate/App/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import UIKit
import IQKeyboardManagerSwift
import FirebaseCore
import FirebaseMessaging

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
Expand All @@ -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
}

Expand All @@ -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.
}

}

0 comments on commit c88c977

Please sign in to comment.