안녕하세요~ 지호구입니다
오늘은 SwiftUI에서 FCM(Firebase Cloud Messaging)을 사용하는 방법에 대해 알아보겠습니다
프로젝트를 진행하다 보면 푸시 알림 기능을 요구하는 경우가 있을 겁니다
사용자 간 상호작용 없이 로컬 알림만 사용한다면 UNUserNotificationCenter만 활용하면 되지만
SNS에서의 좋아요, DM과 같은 상호작용이 있다면 서버에서 이를 구현해주어야 합니다.
서버에서 구현하려면 푸시 전용 서버를 만들고 이것저것 설정을 하는 것에 상당히 많은 리소스가 들어가기 때문에 어려움이 있는데,
이를 해소해 주는 것이 FCM 입니다
Firebase Cloud Messaging
Firebase 클라우드 메시징(FCM)은 메시지를 안정적으로 전송할 수 있는 크로스 플랫폼 메시징 솔루션입니다.
firebase.google.com
Xcode와 Firebase 프로젝트를 생성했다는 전제하에 시작하겠습니다
준비물
- Apple 개발자 계정
- Firebase 개발자 계정
만약 Apple 개발자 계정이 없다면 아쉽게도 함께할 수 없습니다..
Firebase에서 FCM 초기 세팅을 할 때 .p8 파일이 필요하기 때문에 개발자 계정이 필수입니다.
1. Apple 개발자 계정 초기 세팅
로그인 - Apple
idmsa.apple.com
첫 번째로 identifiers 탭에서 내가 진행 중인 프로젝트가 등록되어 있는지를 확인합니다
보통 프로젝트를 빌드하면 자동으로 등록되는 것 같은데, 만약 등록되어있지 않다면 수동으로 등록하면 됩니다
등록을 해야 한다면

Identifiers 옆에 파란색 + 버튼을 누르고
App IDs > App 선택 > Bundle ID 입력 및 Push Notifications 선택
Key 생성
준비물에 .p8 파일이 필요하다 했었는데, 이것을 생성하려면 위의 링크에서
Keys > Key 이름 설정, Apple Push Notifications service 체크 > 생성 과정을 거치면 됩니다
생성이 완료되면 .p8 파일을 다운로드할 수 있는데, 한 번 다운로드하면 다시는 못 받으니 잘 저장해두어야 합니다!!
여기까지 진행했다면 Apple 개발자 계정에서의 세팅은 끝입니다
2. Firebase 초기 세팅
Firebase 프로젝트 콘솔 > 좌측 상단의 설정 버튼 > 클라우드 메시징 탭
Apple 앱 구성 > 다운받아뒀던 p8 파일 등록
여기까지 진행하면 Firebase 세팅은 끝입니다
3. 프로젝트 세팅
Targets > Signing & Capabilities > + Capability > Push Notifications, Background Modes 추가
Targets > Signing & Capabilities > Background Modes > Background fetch, Remote notifications 체크
여기까지 진행했다면 프로젝트 세팅은 끝입니다
4. 코드 구현
UNUserNotificationCenter과 FCM delegate는 AppDelegate에서만 사용할 수 있는데
SwiftUI에선 AppDelegate를 구현하고 App에 추가해주어야 합니다
AppDelegate에서 Message delegate를 통해 받아온 토큰을 서버에 전달해 주면 FCM 구현은 끝입니다.
딥링크는 추가적으로 구현해야 합니다
AppDelegate
import SwiftUI
import FirebaseCore
import FirebaseMessaging
class AppDelegate: NSObject, UIApplicationDelegate {
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil
) -> Bool {
FirebaseApp.configure()
UNUserNotificationCenter.current().delegate = self
Messaging.messaging().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: { _, _ in }
)
application.registerForRemoteNotifications()
return true
}
func application(
_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
) {
// Firebase에 device token 등록
Messaging.messaging().apnsToken = deviceToken
}
}
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(
_ center: UNUserNotificationCenter,
willPresent notification: UNNotification
) async -> UNNotificationPresentationOptions {
// 앱이 활성화 되어있을 때 푸시를 받은 경우 호출되는 메서드
let userInfo = notification.request.content.userInfo
return [[.banner, .sound]]
}
func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse
) async {
// 푸시를 클릭해서 앱으로 들어왔을 때 호출되는 메서드
// 여기서 딥링크 구현
let userInfo = response.notification.request.content.userInfo
print(userInfo)
}
}
extension AppDelegate: MessagingDelegate {
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
// FCM 토큰이 갱신됐을 때 호출되는 메서드
// 여기서 받은 FCM 토큰을 서버에 전달해주어야함
print("Firebase registration token: \(String(describing: fcmToken))")
UserDefaultsKey.User.fcmToken = fcmToken
let dataDict: [String: String] = ["token": fcmToken ?? ""]
NotificationCenter.default.post(
name: Notification.Name("FCMToken"),
object: nil,
userInfo: dataDict
)
}
}
App
import SwiftUI
@main
struct MyApp: App {
// AppDelegate를 등록해줌
@UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
var body: some Scene {
WindowGroup {
...
}
}
}
참고
Apple 플랫폼에서 Firebase 클라우드 메시징 클라이언트 앱 설정 | Firebase Cloud Messaging
Cloud Next 2025에서 Firebase의 최신 소식을 확인하세요. 자세히 알아보기 의견 보내기 Apple 플랫폼에서 Firebase 클라우드 메시징 클라이언트 앱 설정 컬렉션을 사용해 정리하기 내 환경설정을 기준으
firebase.google.com
'iOS > SwiftUI' 카테고리의 다른 글
[SwiftUI] AsyncImage에서 이미지 캐싱 처리하기 (0) | 2025.04.19 |
---|---|
[SwiftUI] shadow 효과 주는 방법 (0) | 2024.08.12 |
[SwiftUI] ZStack, overlay, background (0) | 2024.06.20 |
[SwiftUI] 화면 터치시 키보드 내리는 방법 (0) | 2024.06.16 |