Firebase Cloud Messaging
Step 1. Firebase Cloud Messaging project setup
-
Create a Firebase project.
-
Register a new app in the project:
-
Download the
GoogleSrvice-Info.plistfile.
-
Go to Project Settings → Cloud Messaging → Apple app configuration.
-
Add the required APNs Authentication Key data (the key must first be created in Apple Developer):
Enter the following data:
- upload the
.p8key file - specify the
key ID - specify the
team ID
Step 2. Altcraft resource configuration
- specify the
messagingSenderId - specify the
projectId - upload the Firebase private key file (.json). The file is created in Project settings → Service account.
Step 3. Integrating Firebase Cloud Messaging into the app project
-
Move the downloaded
GoogleSrvice-Info.plistfile into the application folder (app). -
Add the Firebase package using Swift Package Manager — repository Firebase. The Firebase package version must be compatible with your Xcode version.
-
General:
- Ensure that the FirebaseMessaging library is added under Frameworks, Libraries, and Embedded Content of the app target.
-
Signing & Capabilities:
- PushNotifications
Option 1: FirebaseAppDelegateProxyEnabled — YES (Default for Firebase Messaging)
-
Add the
MessagingDelegateprotocol toAppDelegate:class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate {}
This allows receiving a callback with the current FCM token through the messaging(_:didReceiveRegistrationToken:) method.
-
Initialize Firebase in
didFinishLaunchingWithOptions:FirebaseApp.configure()
Messaging.messaging().delegate = self
application.registerForRemoteNotifications() -
Implement the delegate method to receive the token:
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
guard let token = fcmToken else { return }
print(" FCM Token: \(token)")
}
Full example of Firebase initialization and FCM token retrieval
import SwiftUI
import FirebaseCore
import FirebaseMessaging
import Altcraft
@main
struct IOSQuickStartApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate, UNUserNotificationCenterDelegate {
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
/// Initialize Firebase
FirebaseApp.configure()
/// Set delegate
Messaging.messaging().delegate = self
application.registerForRemoteNotifications()
return true
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
if let token = fcmToken {
//FCM token
}
}
}
Option 2: FirebaseAppDelegateProxyEnabled - NO
-
In the app target settings:
-
Info:
- Add the key
FirebaseAppDelegateProxyEnabled (Boolean)with the valueNOin the app’s Info.
- Add the key
-
-
The
MessagingDelegateprotocol is not required in this case. WhenFirebaseAppDelegateProxyEnabledis disabled, Firebase does not automatically swizzleAppDelegate, so the SDK handles APNs and FCM token setup and management manually. TheMessagingDelegateis therefore not needed. -
Initialize Firebase in
didFinishLaunchingWithOptions:
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
FirebaseApp.configure()
return true
}
- Retrieve and save the APNs token in
didRegisterForRemoteNotificationsWithDeviceToken:
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
FirebaseApp.configure()
return true
}
func application(
_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken apnsToken: Data
) {
// Save the token for later use when requesting the FCM token
setAPNsTokenInUserDefault(apnsToken)
}
}
Step 4. Manually setting the APNs token in Firebase Messaging before requesting the FCM token
Example inside SDK provider
import FirebaseMessaging
import Altcraft
class FCMProvider: FCMInterface {
/// Retrieves the current FCM token
func getToken(completion: @escaping (String?) -> Void) {
/// APNs token retrieved from UserDefaults
let apnsToken = getAPNsTokenDataFromUserDefaults()
/// Manually set the APNs token before requesting the FCM token
Messaging.messaging().apnsToken = apnsToken
/// Request FCM token
Messaging.messaging().token { token, error in
if error != nil {
completion(nil)
} else {
completion(token)
}
}
}
/// Deletes the current FCM token
func deleteToken(completion: @escaping (Bool) -> Void) {
Messaging.messaging().deleteToken { error in
if error != nil {
completion(false)
} else {
completion(true)
}
}
}
}