-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/458-fcm' into develop
- Loading branch information
Showing
4 changed files
with
122 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// | ||
// AppDelegate.swift | ||
// HappyAnding | ||
// | ||
// Created by kimjimin on 2023/09/02. | ||
// | ||
|
||
import Firebase | ||
import FirebaseMessaging | ||
|
||
class AppDelegate: NSObject, UIApplicationDelegate { | ||
|
||
let gcmMessageIDKey = "gcm.message_id" | ||
|
||
// APN 등록 및 사용자 권한 받기 | ||
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { | ||
|
||
// 원격 알림 등록 | ||
UNUserNotificationCenter.current().delegate = self | ||
|
||
let authOption: UNAuthorizationOptions = [.alert, .badge, .sound] | ||
UNUserNotificationCenter.current().requestAuthorization( | ||
options: authOption, | ||
completionHandler: { _, _ in } | ||
) | ||
|
||
application.registerForRemoteNotifications() | ||
|
||
// 메시지 대리자 설정 | ||
Messaging.messaging().delegate = self | ||
|
||
return true | ||
} | ||
|
||
// APNs 토큰과 등록 토큰 매핑 | ||
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { | ||
Messaging.messaging().apnsToken = deviceToken | ||
} | ||
|
||
// 주제 메시지 수신 및 처리 | ||
func application(_ application: UIApplication, | ||
didReceiveRemoteNotification userInfo: [AnyHashable: Any], | ||
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { | ||
|
||
Messaging.messaging().appDidReceiveMessage(userInfo) | ||
|
||
completionHandler(UIBackgroundFetchResult.newData) | ||
} | ||
} | ||
|
||
// 토큰 갱신 모니터링 | ||
extension AppDelegate: MessagingDelegate { | ||
|
||
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) { | ||
|
||
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. | ||
|
||
} | ||
} | ||
|
||
// 알림 처리 | ||
extension AppDelegate: UNUserNotificationCenterDelegate { | ||
|
||
func userNotificationCenter(_ center: UNUserNotificationCenter, | ||
willPresent notification: UNNotification, | ||
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { | ||
|
||
let userInfo = notification.request.content.userInfo | ||
|
||
// 알림 수신 정보를 애널리틱스에 전달 | ||
Messaging.messaging().appDidReceiveMessage(userInfo) | ||
|
||
completionHandler([[.banner, .badge, .sound]]) | ||
} | ||
|
||
func userNotificationCenter(_ center: UNUserNotificationCenter, | ||
didReceive response: UNNotificationResponse, | ||
withCompletionHandler completionHandler: @escaping () -> Void) { | ||
|
||
let userInfo = response.notification.request.content.userInfo | ||
|
||
Messaging.messaging().appDidReceiveMessage(userInfo) | ||
|
||
completionHandler() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters