Skip to main content
Documentation for version v73

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.plist file.



  • 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 .p8 key 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.plist file 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)

  1. Add the MessagingDelegate protocol to AppDelegate:

    class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate {}

This allows receiving a callback with the current FCM token through the messaging(_:didReceiveRegistrationToken:) method.

  1. Initialize Firebase in didFinishLaunchingWithOptions:

    FirebaseApp.configure()
    Messaging.messaging().delegate = self
    application.registerForRemoteNotifications()
  2. 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

  1. In the app target settings:

    • Info:

      • Add the key FirebaseAppDelegateProxyEnabled (Boolean) with the value NO in the app’s Info.


  1. The MessagingDelegate protocol is not required in this case. When FirebaseAppDelegateProxyEnabled is disabled, Firebase does not automatically swizzle AppDelegate, so the SDK handles APNs and FCM token setup and management manually. The MessagingDelegate is therefore not needed.

  2. Initialize Firebase in didFinishLaunchingWithOptions:

   func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
FirebaseApp.configure()
return true
}
  1. 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)
}
}
}
}