-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppDelegate.swift
More file actions
116 lines (102 loc) · 4.04 KB
/
AppDelegate.swift
File metadata and controls
116 lines (102 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//
// AppDelegate.swift
// DevLog
//
// Created by opfic on 5/7/25.
//
import UIKit
import Firebase
import GoogleSignIn
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate {
private let logger = Logger(category: "AppDelegate")
private let container = AppDIContainer.shared
func application(
_ app: UIApplication,
open url: URL,
options: [UIApplication.OpenURLOptionsKey: Any] = [:]
) -> Bool {
return GIDSignIn.sharedInstance.handle(url)
}
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
) -> Bool {
FirebaseApp.configure()
_ = container.resolve(FCMTokenSyncHandler.self)
_ = container.resolve(UserTimeZoneSyncHandler.self)
// 알림 권한 요청
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
if let error = error {
self.logger.error("Notification authorization error", error: error)
} else {
self.logger.info("Notification permission granted: \(granted)")
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
}
// 앱이 온그라운드로 되었을 때, 로그인 세션이 존재한다면 현재 유저의 timeZone 저장
NotificationCenter.default.post(name: .didRequestUserTimeZoneSync, object: nil)
// Firebase Messaging 설정
Messaging.messaging().delegate = self
// 앱이 완전 종료되어도, 알림을 통해 앱이 시작된 경우 처리
if let remoteNotification = launchOptions?[.remoteNotification] as? [AnyHashable: Any] {
Task { @MainActor in
PushNotificationRoute.shared.handlePushTap(userInfo: remoteNotification)
}
}
return true
}
// APNs 등록 성공
func application(
_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
) {
logger.info("APNs token: \(deviceToken.map { String(format: "%02.2hhx", $0) }.joined())")
Messaging.messaging().apnsToken = deviceToken
}
// APNs 등록 실패
func application(
_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error
) {
logger.error("Failed to register APNs token", error: error)
}
// FCMToken 갱신
func messaging(
_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?
) {
if let fcmToken = fcmToken {
logger.info("FCM token: \(fcmToken)")
NotificationCenter.default.post(
name: .didRefreshFCMToken,
object: nil,
userInfo: ["fcmToken": fcmToken]
)
}
}
}
extension AppDelegate: UNUserNotificationCenterDelegate {
// 앱이 포그라운드에 있을 때 알림 표시
func userNotificationCenter(
_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
) {
logger.info("Foreground notification: \(notification.request.content.userInfo)")
completionHandler([.banner, .sound, .badge])
}
// 알림 클릭 처리
func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
) {
logger.info("Tapped notification: \(response.notification.request.content.userInfo)")
let userInfo = response.notification.request.content.userInfo
Task { @MainActor in
PushNotificationRoute.shared.handlePushTap(userInfo: userInfo)
}
completionHandler()
}
}