Skip to content

Commit

Permalink
[Add] #6 FCM 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
KakaoTocs committed Aug 25, 2021
1 parent 57fdd45 commit 1a4d81d
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion BMDJ/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()

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

0 comments on commit 1a4d81d

Please sign in to comment.