From 5d0e982c0fbe0e2a8e074c326463b8c3436f23d9 Mon Sep 17 00:00:00 2001 From: jim4020key Date: Fri, 4 Aug 2023 02:13:00 +0900 Subject: [PATCH 1/8] [Chore] #458 - Add FirebaseMessaging --- HappyAnding/HappyAnding.xcodeproj/project.pbxproj | 8 ++++++++ HappyAnding/HappyAnding/Info.plist | 8 ++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/HappyAnding/HappyAnding.xcodeproj/project.pbxproj b/HappyAnding/HappyAnding.xcodeproj/project.pbxproj index a2ec4dac..67af6c56 100644 --- a/HappyAnding/HappyAnding.xcodeproj/project.pbxproj +++ b/HappyAnding/HappyAnding.xcodeproj/project.pbxproj @@ -15,6 +15,7 @@ 4D778A34290A53BA00C15AC4 /* UIApplication+Keyboard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D778A33290A53BA00C15AC4 /* UIApplication+Keyboard.swift */; }; 4D7D16072986BBD7008B3332 /* TextLiteral.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D7D16062986BBD7008B3332 /* TextLiteral.swift */; }; 4D7D16082986BBDE008B3332 /* TextLiteral.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D7D16062986BBD7008B3332 /* TextLiteral.swift */; }; + 4D93D0772A73C9330042CBA8 /* FirebaseMessaging in Frameworks */ = {isa = PBXBuildFile; productRef = 4D93D0762A73C9330042CBA8 /* FirebaseMessaging */; }; 4DAD635E292AB61700ABF8C1 /* UpdateShortcutView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4DAD635D292AB61700ABF8C1 /* UpdateShortcutView.swift */; }; 4DF62DD52A0550ED00A8B377 /* UIScreen+Size.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8795A16F292AB945004B765F /* UIScreen+Size.swift */; }; 87276C382933F6AB00C92F4C /* CustomTextEditor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87276C372933F6AB00C92F4C /* CustomTextEditor.swift */; }; @@ -305,6 +306,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + 4D93D0772A73C9330042CBA8 /* FirebaseMessaging in Frameworks */, F94B435D2907B19A00987819 /* FirebaseAnalytics in Frameworks */, F94B43632907B19A00987819 /* FirebaseFirestoreCombine-Community in Frameworks */, F94B435F2907B19A00987819 /* FirebaseAuth in Frameworks */, @@ -741,6 +743,7 @@ F94B43602907B19A00987819 /* FirebaseFirestore */, F94B43622907B19A00987819 /* FirebaseFirestoreCombine-Community */, 4D6A9EFE29A36E9C00D02522 /* WrappingHStack */, + 4D93D0762A73C9330042CBA8 /* FirebaseMessaging */, ); productName = HappyAnding; productReference = 87E99C6A28F94EA6009B691F /* HappyAnding.app */; @@ -1478,6 +1481,11 @@ package = 4D6A9EFD29A36E9C00D02522 /* XCRemoteSwiftPackageReference "WrappingHStack" */; productName = WrappingHStack; }; + 4D93D0762A73C9330042CBA8 /* FirebaseMessaging */ = { + isa = XCSwiftPackageProductDependency; + package = F94B435B2907B19A00987819 /* XCRemoteSwiftPackageReference "firebase-ios-sdk" */; + productName = FirebaseMessaging; + }; A3FC4746292A61550058BF26 /* FirebaseAnalytics */ = { isa = XCSwiftPackageProductDependency; package = F94B435B2907B19A00987819 /* XCRemoteSwiftPackageReference "firebase-ios-sdk" */; diff --git a/HappyAnding/HappyAnding/Info.plist b/HappyAnding/HappyAnding/Info.plist index dea0a695..9e64c459 100644 --- a/HappyAnding/HappyAnding/Info.plist +++ b/HappyAnding/HappyAnding/Info.plist @@ -2,8 +2,6 @@ - Localization native development region - Korea CFBundleURLTypes @@ -18,11 +16,17 @@ + Localization native development region + Korea NSAppTransportSecurity NSAllowsArbitraryLoads + UIBackgroundModes + + remote-notification + URL Schemes shortcutsZip From f2d1c4fe23120430e38261cb958a8ac2bb512172 Mon Sep 17 00:00:00 2001 From: jim4020key Date: Fri, 4 Aug 2023 02:26:54 +0900 Subject: [PATCH 2/8] [Feat] #458 - Add AppDelegate --- .../HappyAnding/Views/HappyAndingApp.swift | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/HappyAnding/HappyAnding/Views/HappyAndingApp.swift b/HappyAnding/HappyAnding/Views/HappyAndingApp.swift index 918638ef..b53b7ca2 100644 --- a/HappyAnding/HappyAnding/Views/HappyAndingApp.swift +++ b/HappyAnding/HappyAnding/Views/HappyAndingApp.swift @@ -7,8 +7,10 @@ import SwiftUI +import Firebase import FirebaseCore import FirebaseFirestore +import FirebaseMessaging @main @@ -30,6 +32,8 @@ struct HappyAndingApp: App { FirebaseApp.configure() } + @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate + var body: some Scene { WindowGroup { if isShowingLaunchScreen { @@ -99,3 +103,101 @@ struct HappyAndingApp: App { } } } + +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) { + + if let messageID = userInfo[gcmMessageIDKey] { + print("Message ID: \(messageID)") + } + + print(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. + + print("================== 토큰 =========================") + print(dataDict) + } +} + +// 알림 처리 +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)") + } + + + print(userInfo) + + completionHandler([[.banner, .badge, .sound]]) + } + + 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() + } +} From 1afdf20873ad7616c887edf0ccd7fb36a80b7b51 Mon Sep 17 00:00:00 2001 From: jim4020key Date: Sat, 2 Sep 2023 21:03:27 +0900 Subject: [PATCH 3/8] [Feat] #458 - Delete print --- HappyAnding/HappyAnding/Info.plist | 1 + .../HappyAnding/Views/HappyAndingApp.swift | 24 ++++--------------- 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/HappyAnding/HappyAnding/Info.plist b/HappyAnding/HappyAnding/Info.plist index 9e64c459..aa2b2c93 100644 --- a/HappyAnding/HappyAnding/Info.plist +++ b/HappyAnding/HappyAnding/Info.plist @@ -25,6 +25,7 @@ UIBackgroundModes + fetch remote-notification URL Schemes diff --git a/HappyAnding/HappyAnding/Views/HappyAndingApp.swift b/HappyAnding/HappyAnding/Views/HappyAndingApp.swift index b53b7ca2..39671d36 100644 --- a/HappyAnding/HappyAnding/Views/HappyAndingApp.swift +++ b/HappyAnding/HappyAnding/Views/HappyAndingApp.swift @@ -138,11 +138,7 @@ class AppDelegate: NSObject, UIApplicationDelegate { didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { - if let messageID = userInfo[gcmMessageIDKey] { - print("Message ID: \(messageID)") - } - - print(userInfo) + Messaging.messaging().appDidReceiveMessage(userInfo) completionHandler(UIBackgroundFetchResult.newData) } @@ -162,8 +158,6 @@ extension AppDelegate: MessagingDelegate { // TODO: If necessary send token to application server. - print("================== 토큰 =========================") - print(dataDict) } } @@ -176,13 +170,9 @@ extension AppDelegate: UNUserNotificationCenterDelegate { let userInfo = notification.request.content.userInfo - if let messageID = userInfo[gcmMessageIDKey] { - print("Message ID: \(messageID)") - } - - - print(userInfo) - + // 알림 수신 정보를 애널리틱스에 전달 + Messaging.messaging().appDidReceiveMessage(userInfo) + completionHandler([[.banner, .badge, .sound]]) } @@ -192,11 +182,7 @@ extension AppDelegate: UNUserNotificationCenterDelegate { let userInfo = response.notification.request.content.userInfo - if let messageID = userInfo[gcmMessageIDKey] { - print("Message ID: \(messageID)") - } - - print(userInfo) + Messaging.messaging().appDidReceiveMessage(userInfo) completionHandler() } From f7c0d9e436139443a8a811ba2e98e5d24e97203b Mon Sep 17 00:00:00 2001 From: jim4020key Date: Fri, 4 Aug 2023 02:13:00 +0900 Subject: [PATCH 4/8] [Chore] #458 - Add FirebaseMessaging --- HappyAnding/HappyAnding.xcodeproj/project.pbxproj | 8 ++++++++ HappyAnding/HappyAnding/Info.plist | 8 ++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/HappyAnding/HappyAnding.xcodeproj/project.pbxproj b/HappyAnding/HappyAnding.xcodeproj/project.pbxproj index 466f5195..e963bca0 100644 --- a/HappyAnding/HappyAnding.xcodeproj/project.pbxproj +++ b/HappyAnding/HappyAnding.xcodeproj/project.pbxproj @@ -19,6 +19,7 @@ 4D7D16082986BBDE008B3332 /* TextLiteral.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D7D16062986BBD7008B3332 /* TextLiteral.swift */; }; 4D93D06F2A5956E60042CBA8 /* ShowProfileViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D93D06E2A5956E60042CBA8 /* ShowProfileViewModel.swift */; }; 4D93D0752A61D0D10042CBA8 /* ReadShortcutViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D93D0742A61D0D10042CBA8 /* ReadShortcutViewModel.swift */; }; + 4D93D0772A73C9330042CBA8 /* FirebaseMessaging in Frameworks */ = {isa = PBXBuildFile; productRef = 4D93D0762A73C9330042CBA8 /* FirebaseMessaging */; }; 4DAD635E292AB61700ABF8C1 /* UpdateShortcutView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4DAD635D292AB61700ABF8C1 /* UpdateShortcutView.swift */; }; 4DF15D732A4ECC7D0014F854 /* ListShortcutViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4DF15D722A4ECC7D0014F854 /* ListShortcutViewModel.swift */; }; 4DF15D752A4ECE1F0014F854 /* ListCategoryShortcutViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4DF15D742A4ECE1F0014F854 /* ListCategoryShortcutViewModel.swift */; }; @@ -320,6 +321,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + 4D93D0772A73C9330042CBA8 /* FirebaseMessaging in Frameworks */, F94B435D2907B19A00987819 /* FirebaseAnalytics in Frameworks */, F94B43632907B19A00987819 /* FirebaseFirestoreCombine-Community in Frameworks */, F94B435F2907B19A00987819 /* FirebaseAuth in Frameworks */, @@ -763,6 +765,7 @@ F94B43602907B19A00987819 /* FirebaseFirestore */, F94B43622907B19A00987819 /* FirebaseFirestoreCombine-Community */, 4D6A9EFE29A36E9C00D02522 /* WrappingHStack */, + 4D93D0762A73C9330042CBA8 /* FirebaseMessaging */, ); productName = HappyAnding; productReference = 87E99C6A28F94EA6009B691F /* HappyAnding.app */; @@ -1508,6 +1511,11 @@ package = 4D6A9EFD29A36E9C00D02522 /* XCRemoteSwiftPackageReference "WrappingHStack" */; productName = WrappingHStack; }; + 4D93D0762A73C9330042CBA8 /* FirebaseMessaging */ = { + isa = XCSwiftPackageProductDependency; + package = F94B435B2907B19A00987819 /* XCRemoteSwiftPackageReference "firebase-ios-sdk" */; + productName = FirebaseMessaging; + }; A3FC4746292A61550058BF26 /* FirebaseAnalytics */ = { isa = XCSwiftPackageProductDependency; package = F94B435B2907B19A00987819 /* XCRemoteSwiftPackageReference "firebase-ios-sdk" */; diff --git a/HappyAnding/HappyAnding/Info.plist b/HappyAnding/HappyAnding/Info.plist index dea0a695..9e64c459 100644 --- a/HappyAnding/HappyAnding/Info.plist +++ b/HappyAnding/HappyAnding/Info.plist @@ -2,8 +2,6 @@ - Localization native development region - Korea CFBundleURLTypes @@ -18,11 +16,17 @@ + Localization native development region + Korea NSAppTransportSecurity NSAllowsArbitraryLoads + UIBackgroundModes + + remote-notification + URL Schemes shortcutsZip From 5f8fc8980cd29263d6e2fc0737200c221606b564 Mon Sep 17 00:00:00 2001 From: jim4020key Date: Fri, 4 Aug 2023 02:26:54 +0900 Subject: [PATCH 5/8] [Feat] #458 - Add AppDelegate --- .../HappyAnding/Views/HappyAndingApp.swift | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/HappyAnding/HappyAnding/Views/HappyAndingApp.swift b/HappyAnding/HappyAnding/Views/HappyAndingApp.swift index 918638ef..b53b7ca2 100644 --- a/HappyAnding/HappyAnding/Views/HappyAndingApp.swift +++ b/HappyAnding/HappyAnding/Views/HappyAndingApp.swift @@ -7,8 +7,10 @@ import SwiftUI +import Firebase import FirebaseCore import FirebaseFirestore +import FirebaseMessaging @main @@ -30,6 +32,8 @@ struct HappyAndingApp: App { FirebaseApp.configure() } + @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate + var body: some Scene { WindowGroup { if isShowingLaunchScreen { @@ -99,3 +103,101 @@ struct HappyAndingApp: App { } } } + +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) { + + if let messageID = userInfo[gcmMessageIDKey] { + print("Message ID: \(messageID)") + } + + print(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. + + print("================== 토큰 =========================") + print(dataDict) + } +} + +// 알림 처리 +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)") + } + + + print(userInfo) + + completionHandler([[.banner, .badge, .sound]]) + } + + 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() + } +} From c6d7f0417d50e29b368fadd2a7de5fb871f9a57f Mon Sep 17 00:00:00 2001 From: jim4020key Date: Sat, 2 Sep 2023 21:03:27 +0900 Subject: [PATCH 6/8] [Feat] #458 - Delete print --- HappyAnding/HappyAnding/Info.plist | 1 + .../HappyAnding/Views/HappyAndingApp.swift | 24 ++++--------------- 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/HappyAnding/HappyAnding/Info.plist b/HappyAnding/HappyAnding/Info.plist index 9e64c459..aa2b2c93 100644 --- a/HappyAnding/HappyAnding/Info.plist +++ b/HappyAnding/HappyAnding/Info.plist @@ -25,6 +25,7 @@ UIBackgroundModes + fetch remote-notification URL Schemes diff --git a/HappyAnding/HappyAnding/Views/HappyAndingApp.swift b/HappyAnding/HappyAnding/Views/HappyAndingApp.swift index b53b7ca2..39671d36 100644 --- a/HappyAnding/HappyAnding/Views/HappyAndingApp.swift +++ b/HappyAnding/HappyAnding/Views/HappyAndingApp.swift @@ -138,11 +138,7 @@ class AppDelegate: NSObject, UIApplicationDelegate { didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { - if let messageID = userInfo[gcmMessageIDKey] { - print("Message ID: \(messageID)") - } - - print(userInfo) + Messaging.messaging().appDidReceiveMessage(userInfo) completionHandler(UIBackgroundFetchResult.newData) } @@ -162,8 +158,6 @@ extension AppDelegate: MessagingDelegate { // TODO: If necessary send token to application server. - print("================== 토큰 =========================") - print(dataDict) } } @@ -176,13 +170,9 @@ extension AppDelegate: UNUserNotificationCenterDelegate { let userInfo = notification.request.content.userInfo - if let messageID = userInfo[gcmMessageIDKey] { - print("Message ID: \(messageID)") - } - - - print(userInfo) - + // 알림 수신 정보를 애널리틱스에 전달 + Messaging.messaging().appDidReceiveMessage(userInfo) + completionHandler([[.banner, .badge, .sound]]) } @@ -192,11 +182,7 @@ extension AppDelegate: UNUserNotificationCenterDelegate { let userInfo = response.notification.request.content.userInfo - if let messageID = userInfo[gcmMessageIDKey] { - print("Message ID: \(messageID)") - } - - print(userInfo) + Messaging.messaging().appDidReceiveMessage(userInfo) completionHandler() } From dad6372f5cd8c921524af45bf00eab026bf954f4 Mon Sep 17 00:00:00 2001 From: jim4020key Date: Sat, 2 Sep 2023 22:20:33 +0900 Subject: [PATCH 7/8] [Feat] #458 - Move AppDelegate --- .../HappyAnding.xcodeproj/project.pbxproj | 4 + .../HappyAnding/Views/AppDelegate.swift | 93 +++++++++++++++++++ .../HappyAnding/Views/HappyAndingApp.swift | 88 ------------------ 3 files changed, 97 insertions(+), 88 deletions(-) create mode 100644 HappyAnding/HappyAnding/Views/AppDelegate.swift diff --git a/HappyAnding/HappyAnding.xcodeproj/project.pbxproj b/HappyAnding/HappyAnding.xcodeproj/project.pbxproj index e963bca0..dff42bab 100644 --- a/HappyAnding/HappyAnding.xcodeproj/project.pbxproj +++ b/HappyAnding/HappyAnding.xcodeproj/project.pbxproj @@ -11,6 +11,7 @@ 4D061BBA2A475EE800F76835 /* ExploreShortcutViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D061BB92A475EE800F76835 /* ExploreShortcutViewModel.swift */; }; 4D3DBB88292E67E600DE8160 /* EditNicknameView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D3DBB87292E67E500DE8160 /* EditNicknameView.swift */; }; 4D3DBB962934E31A00DE8160 /* ShowProfileView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D3DBB952934E31A00DE8160 /* ShowProfileView.swift */; }; + 4D5889E82AA36A52000C4849 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D5889E72AA36A52000C4849 /* AppDelegate.swift */; }; 4D61A767291E1EE8000EF531 /* NavigationViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D61A766291E1EE8000EF531 /* NavigationViewModel.swift */; }; 4D6A9EFF29A36E9C00D02522 /* WrappingHStack in Frameworks */ = {isa = PBXBuildFile; productRef = 4D6A9EFE29A36E9C00D02522 /* WrappingHStack */; }; 4D6A9F0129A3A92F00D02522 /* wrappinghstack+license.txt in Resources */ = {isa = PBXBuildFile; fileRef = 4D6A9F0029A3A92E00D02522 /* wrappinghstack+license.txt */; }; @@ -199,6 +200,7 @@ 4D061BB92A475EE800F76835 /* ExploreShortcutViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExploreShortcutViewModel.swift; sourceTree = ""; }; 4D3DBB87292E67E500DE8160 /* EditNicknameView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EditNicknameView.swift; sourceTree = ""; }; 4D3DBB952934E31A00DE8160 /* ShowProfileView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShowProfileView.swift; sourceTree = ""; }; + 4D5889E72AA36A52000C4849 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 4D61A766291E1EE8000EF531 /* NavigationViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NavigationViewModel.swift; sourceTree = ""; }; 4D6A9F0029A3A92E00D02522 /* wrappinghstack+license.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "wrappinghstack+license.txt"; sourceTree = ""; }; 4D778A33290A53BA00C15AC4 /* UIApplication+Keyboard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIApplication+Keyboard.swift"; sourceTree = ""; }; @@ -522,6 +524,7 @@ isa = PBXGroup; children = ( 87E99C6D28F94EA6009B691F /* HappyAndingApp.swift */, + 4D5889E72AA36A52000C4849 /* AppDelegate.swift */, A04ACB0229027F4F004A85A6 /* TabView */, 87E99CC52901457B009B691F /* Components */, 87E99C9B28FFF1F4009B691F /* ExploreShortcutViews */, @@ -986,6 +989,7 @@ F99569182901DC4D0060AAEF /* UIFont+Extension.swift in Sources */, F91A72C32999160E00CA135A /* Alerter.swift in Sources */, 87E99CAD28FFF261009B691F /* ReadShortcutView.swift in Sources */, + 4D5889E82AA36A52000C4849 /* AppDelegate.swift in Sources */, A33F74AE2908D8C800B8D0D0 /* CheckBoxShortcutCell.swift in Sources */, 87E606B22910649B00C3DA13 /* SignInWithAppleView.swift in Sources */, F91F09DF29AE0B5E00E04FA0 /* GradeAlertView.swift in Sources */, diff --git a/HappyAnding/HappyAnding/Views/AppDelegate.swift b/HappyAnding/HappyAnding/Views/AppDelegate.swift new file mode 100644 index 00000000..51539fcc --- /dev/null +++ b/HappyAnding/HappyAnding/Views/AppDelegate.swift @@ -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() + } +} diff --git a/HappyAnding/HappyAnding/Views/HappyAndingApp.swift b/HappyAnding/HappyAnding/Views/HappyAndingApp.swift index 39671d36..232747a2 100644 --- a/HappyAnding/HappyAnding/Views/HappyAndingApp.swift +++ b/HappyAnding/HappyAnding/Views/HappyAndingApp.swift @@ -7,11 +7,7 @@ import SwiftUI -import Firebase import FirebaseCore -import FirebaseFirestore -import FirebaseMessaging - @main struct HappyAndingApp: App { @@ -103,87 +99,3 @@ struct HappyAndingApp: App { } } } - -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() - } -} From 0cb36c748cd5bc286150bc28c7c8e1b36b1b533c Mon Sep 17 00:00:00 2001 From: jim4020key Date: Tue, 31 Oct 2023 20:31:11 +0900 Subject: [PATCH 8/8] [Chore] #458 - Move file to new group --- HappyAnding/HappyAnding.xcodeproj/project.pbxproj | 10 +++++++++- .../HappyAnding/{Views => Support}/AppDelegate.swift | 0 2 files changed, 9 insertions(+), 1 deletion(-) rename HappyAnding/HappyAnding/{Views => Support}/AppDelegate.swift (100%) diff --git a/HappyAnding/HappyAnding.xcodeproj/project.pbxproj b/HappyAnding/HappyAnding.xcodeproj/project.pbxproj index dff42bab..4573b156 100644 --- a/HappyAnding/HappyAnding.xcodeproj/project.pbxproj +++ b/HappyAnding/HappyAnding.xcodeproj/project.pbxproj @@ -360,6 +360,14 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 4DB674822AF11B7900A83C0B /* Support */ = { + isa = PBXGroup; + children = ( + 4D5889E72AA36A52000C4849 /* AppDelegate.swift */, + ); + path = Support; + sourceTree = ""; + }; 8786B2DD29A7F8C8000B46A1 /* String */ = { isa = PBXGroup; children = ( @@ -475,6 +483,7 @@ 87E606AD2910623C00C3DA13 /* HappyAnding.entitlements */, 3D41EE06290A458B008BE986 /* Info.plist */, 87E99CC22901454D009B691F /* Extensions */, + 4DB674822AF11B7900A83C0B /* Support */, 87E99C9828FFF1D2009B691F /* Views */, 87E99CD729042503009B691F /* Model */, A0F822AA2910B8B900AF4448 /* ViewModel */, @@ -524,7 +533,6 @@ isa = PBXGroup; children = ( 87E99C6D28F94EA6009B691F /* HappyAndingApp.swift */, - 4D5889E72AA36A52000C4849 /* AppDelegate.swift */, A04ACB0229027F4F004A85A6 /* TabView */, 87E99CC52901457B009B691F /* Components */, 87E99C9B28FFF1F4009B691F /* ExploreShortcutViews */, diff --git a/HappyAnding/HappyAnding/Views/AppDelegate.swift b/HappyAnding/HappyAnding/Support/AppDelegate.swift similarity index 100% rename from HappyAnding/HappyAnding/Views/AppDelegate.swift rename to HappyAnding/HappyAnding/Support/AppDelegate.swift