Skip to content

Commit

Permalink
✨ :: [#284] FCM Push Notification 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
Xixn2 committed Dec 8, 2024
1 parent fecf474 commit 584b4c9
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 45 deletions.
28 changes: 14 additions & 14 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/google/abseil-cpp-binary.git",
"state" : {
"revision" : "748c7837511d0e6a507737353af268484e1745e2",
"version" : "1.2024011601.1"
"revision" : "194a6706acbd25e4ef639bcaddea16e8758a3e27",
"version" : "1.2024011602.0"
}
},
{
Expand All @@ -23,17 +23,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/google/app-check.git",
"state" : {
"revision" : "076b241a625e25eac22f8849be256dfb960fcdfe",
"version" : "10.19.1"
"revision" : "61b85103a1aeed8218f17c794687781505fbbef5",
"version" : "11.2.0"
}
},
{
"identity" : "firebase-ios-sdk",
"kind" : "remoteSourceControl",
"location" : "https://github.com/firebase/firebase-ios-sdk.git",
"state" : {
"revision" : "9d17b500cd98d9a7009751ad62f802e152e97021",
"version" : "10.26.0"
"revision" : "2e02253fd1ce99145bcbf1bb367ccf61bd0ca46b",
"version" : "11.6.0"
}
},
{
Expand All @@ -50,35 +50,35 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/google/GoogleAppMeasurement.git",
"state" : {
"revision" : "16244d177c4e989f87b25e9db1012b382cfedc55",
"version" : "10.25.0"
"revision" : "4f234bcbdae841d7015258fbbf8e7743a39b8200",
"version" : "11.4.0"
}
},
{
"identity" : "googledatatransport",
"kind" : "remoteSourceControl",
"location" : "https://github.com/google/GoogleDataTransport.git",
"state" : {
"revision" : "a637d318ae7ae246b02d7305121275bc75ed5565",
"version" : "9.4.0"
"revision" : "617af071af9aa1d6a091d59a202910ac482128f9",
"version" : "10.1.0"
}
},
{
"identity" : "googleutilities",
"kind" : "remoteSourceControl",
"location" : "https://github.com/google/GoogleUtilities.git",
"state" : {
"revision" : "57a1d307f42df690fdef2637f3e5b776da02aad6",
"version" : "7.13.3"
"revision" : "53156c7ec267db846e6b64c9f4c4e31ba4cf75eb",
"version" : "8.0.2"
}
},
{
"identity" : "grpc-binary",
"kind" : "remoteSourceControl",
"location" : "https://github.com/google/grpc-binary.git",
"state" : {
"revision" : "e9fad491d0673bdda7063a0341fb6b47a30c5359",
"version" : "1.62.2"
"revision" : "f56d8fc3162de9a498377c7b6cea43431f4f5083",
"version" : "1.65.1"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ let package = Package(
.package(url: "https://github.com/GSM-MSG/GAuthSignin-Swift", from: "0.0.3"),
.package(url: "https://github.com/dmrschmidt/QRCode", from: "1.0.0"),
.package(url: "https://github.com/yannickl/QRCodeReader.swift.git", from: "10.1.0"),
.package(url: "https://github.com/firebase/firebase-ios-sdk.git", from: "10.22.0")
.package(url: "https://github.com/firebase/firebase-ios-sdk.git", from: "11.6.0")
]
)
79 changes: 51 additions & 28 deletions Projects/App/Sources/Application/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
//
// AppDelegate.swift
// GOMS-iOS-V2
//
// Created by 새미 on 1/10/24.
// Copyright © 2024 HARIBO. All rights reserved.
//

import UIKit
import Firebase
import UserNotifications
Expand All @@ -17,22 +9,22 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
// 파이어베이스 설정
FirebaseApp.configure()

// 앱 실행 시 사용자에게 알림 허용 권한을 받음
UNUserNotificationCenter.current().delegate = self

let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] // 필요한 알림 권한을 설정
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: { _, _ in }
completionHandler: { granted, _ in
if granted {
self.scheduleWeeklyNotifications()
}
}
)

// UNUserNotificationCenterDelegate를 구현한 메서드를 실행시킴
application.registerForRemoteNotifications()

// 파이어베이스 Messaging 설정
Messaging.messaging().delegate = self

return true
Expand All @@ -51,36 +43,67 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
_ application: UIApplication,
didDiscardSceneSessions sceneSessions: Set<UISceneSession>
) {}

func scheduleWeeklyNotifications() {
let center = UNUserNotificationCenter.current()

let daysOfWeek = [2, 4]

for day in daysOfWeek {
let content = UNMutableNotificationContent()
content.title = "[GOMS] 개발팀"
content.body = "외출제 시행하는 날입니다!\n추가사항은 GSM 디코 공지채널을 확인해주세요!"
content.sound = .default

var dateComponents = DateComponents()
dateComponents.hour = 16
dateComponents.minute = 20
dateComponents.weekday = day

let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: "weeklyNotification\(day)", content: content, trigger: trigger)

center.add(request) { (error) in
if let error = error {
print("Error scheduling weekly notification: \(error)")
}
}
}
}
}

extension AppDelegate: UNUserNotificationCenterDelegate {

// 백그라운드에서 푸시 알림을 탭했을 때 실행
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("APNS token: \(deviceToken)")
Messaging.messaging().apnsToken = deviceToken

// APNS 토큰을 설정한 후 FCM 토큰 요청
Messaging.messaging().token { token, error in
if let error = error {
print("Error fetching FCM registration token: \(error)")
} else if let token = token {
print("FCM registration token: \(token)")
}
}
}

// Foreground(앱 켜진 상태)에서도 알림 오는 설정
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.list, .banner])
completionHandler([.list, .banner, .sound])
}
}

extension AppDelegate: MessagingDelegate {

// 파이어베이스 MessagingDelegate 설정
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
print("Firebase registration token: \(String(describing: fcmToken))")

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.
// Note: This callback is fired at each app startup and whenever a new token is generated.
print("Firebase registration token: \(String(describing: fcmToken))")

let dataDict: [String: String] = ["token": fcmToken ?? ""]
NotificationCenter.default.post(
name: Notification.Name("FCMToken"),
object: nil,
userInfo: dataDict
)
}
}
2 changes: 1 addition & 1 deletion Projects/ThirdPartyLib/Project.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import ProjectDescriptionHelpers

let project = Project.makeModule(
name: "ThirdPartyLib",
product: .framework,
product: .staticFramework,
packages: [],
dependencies: [
.external(name: "RxSwift"),
Expand Down
2 changes: 1 addition & 1 deletion Tuist/ProjectDescriptionHelpers/Project+Templates.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public extension Project {
resources: resources,
dependencies: dependencies
)

let targets: [Target] = [appTarget]

return Project(
Expand Down

0 comments on commit 584b4c9

Please sign in to comment.