Skip to content

Commit

Permalink
feat: add push message
Browse files Browse the repository at this point in the history
  • Loading branch information
leeyoonchae committed Sep 30, 2024
1 parent b49bb56 commit 1f2e27c
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 2 deletions.
1 change: 0 additions & 1 deletion Projects/App/Project.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ let project = Project(
.project(target: "Repository", path: .relativeToRoot("Projects/Data")),
.project(target: "DIContainer", path: .relativeToRoot("Projects/DIContainer")),
.target(name: "DodamDodamWidget"),
.external(name: "FirebaseAnalytics"),
.external(name: "FirebaseMessaging")
]
),
Expand Down
106 changes: 106 additions & 0 deletions Projects/App/iOS/Source/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
//
// AppDelegate.swift
// DodamDodam
//
// Created by dgsw8th61 on 9/19/24.
//

import SwiftUI
import Firebase
import FirebaseMessaging

class AppDelegate: NSObject, UIApplicationDelegate{

let gcmMessageIDKey = "gcm.message_id"

// 앱이 켜졌을 때
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {

// 파이어베이스 설정
FirebaseApp.configure()

// Setting Up Notifications...
// 원격 알림 등록
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self

let authOption: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOption,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}

application.registerForRemoteNotifications()

// Setting Up Cloud Messaging...
// 메세징 델리겟
Messaging.messaging().delegate = self
UNUserNotificationCenter.current().delegate = self
return true
}

// fcm 토큰이 등록 되었을 때
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
}

}

// Cloud Messaging...
extension AppDelegate: MessagingDelegate {

// fcm 등록 토큰을 받았을 때
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {

print("토큰을 받았다")
// Store this token to firebase and retrieve when to send message to someone...
let dataDict: [String: String] = ["token": fcmToken ?? ""]

// Store token in Firestore For Sending Notifications From Server in Future...
print(dataDict)

}
}

// User Notifications...[AKA InApp Notification...]

@available(iOS 10, *)
extension AppDelegate: UNUserNotificationCenterDelegate {

// 푸시 메세지가 앱이 켜져있을 때 나올떄
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions)
-> Void) {

let userInfo = notification.request.content.userInfo

// Do Something With MSG Data...
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

// Do Something With MSG Data...
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
print(userInfo)
completionHandler()
}
}
2 changes: 2 additions & 0 deletions Projects/App/iOS/Source/AppMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import FlowKit
@main
struct AppMain: App {

@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

init() {
Pretendard.register()
DependencyProvider.shared.register()
Expand Down
1 change: 0 additions & 1 deletion Tuist/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ let package = Package(
.package(url: "https://github.com/Mercen-Lee/FlowKit", branch: "main"),
.package(url: "https://github.com/Mercen-Lee/SignKit", exact: "0.0.2"),
.package(url: "https://github.com/lorenzofiamingo/swiftui-cached-async-image", exact: "2.1.1"),
.package(url: "https://github.com/bestswlkh0310/SwiftBok", exact: "1.2.0"),
.package(url: "https://github.com/firebase/firebase-ios-sdk.git", from: "10.0.0")
]
)

0 comments on commit 1f2e27c

Please sign in to comment.