From 1a4d81d2bb55ee0e998a92e644263b44def4ebad Mon Sep 17 00:00:00 2001 From: KakaoTocs Date: Thu, 26 Aug 2021 01:48:34 +0900 Subject: [PATCH] =?UTF-8?q?[Add]=20#6=20FCM=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BMDJ/AppDelegate.swift | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/BMDJ/AppDelegate.swift b/BMDJ/AppDelegate.swift index 292c860..96773db 100644 --- a/BMDJ/AppDelegate.swift +++ b/BMDJ/AppDelegate.swift @@ -11,13 +11,21 @@ import Firebase @main class AppDelegate: UIResponder, UIApplicationDelegate { - + + let gcmMessageIDKey = "634NV3MK2C" var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { print(UserDefaultService.shared.token) FirebaseApp.configure() + UNUserNotificationCenter.current().delegate = self + Messaging.messaging().delegate = self + + let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] + UNUserNotificationCenter.current().requestAuthorization(options: authOptions) { _, _ in } + application.registerForRemoteNotifications() + window = UIWindow(frame: UIScreen.main.bounds) window?.makeKeyAndVisible() @@ -39,3 +47,35 @@ class AppDelegate: UIResponder, UIApplicationDelegate { } } +extension AppDelegate: UNUserNotificationCenterDelegate { + func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { + let userInfo = notification.request.content.userInfo + + if let messageID = userInfo[gcmMessageIDKey] { + print("Message ID: \(messageID)") + } + completionHandler([]) + } + + func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { + let userInfo = response.notification.request.content.userInfo + + if let messageID = userInfo[gcmMessageIDKey] { + print("Message ID: \(messageID)") + } + + print(userInfo) + + completionHandler() + } +} + +extension AppDelegate: MessagingDelegate { + func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) { + print("Firebase registration token: \(fcmToken)") + if let fcmToken = fcmToken { + let dataDict: [String: String] = ["token": fcmToken] + NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict) + } + } +}