Integrate iOS SDK with Firebase using Swift

Configure the iOS SDK with Firebase Cloud Messaging (FCM) in iOS apps developed with Swift.

  1. Download the following and add them to your project.
  • AcousticMobilePush.xcframework,
  • AcousticMobilePushNotification.xcframework,
  • and relevant plugins.
  1. To update for Firebase, exit Xcode. From the command line, in your current Xcode project folder, run $ pod init.
  2. Using a text editor, edit Podfile.
  3. Add Firebase pods to your app. For example, if using SwiftSample - the sample Swift app provided in the SDK:
target 'SwiftSample' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# Pods for SwiftSample
pod 'Firebase/Analytics'
pod 'Firebase/Messaging'
end
  1. Pull the Firebase pods and libraries $ pod update.
  2. Start Xcode and open the app workspace (from now on - do not open the project file but the workspace).
  3. Update Build Setting >Linker Flags > $(inherited) to Other Linker flags.
  4. Update the MceConfig.json file and add the appKey and the other config settings.
  5. Update Capabilities (Push, Background, Maps).
  6. Download GoogleService-Info.plist from FCM. Move it to your project folder and add it to the Xcode project.
  7. In the import section of AppDelegate.swift (in the target SwiftSample in the sample app), add:
// Firebase addition
import Firebase
import Messages
  1. Extend the AppDelegate class as follows:
@objc class AppDelegate : UIResponder, UIApplicationDelegate, UISplitViewControllerDelegate

{

    var window: UIWindow?

      ...

}
  1. In the class implementation of didFinishWithLaunching, add:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool
    {
        // Firebase addition
        FirebaseApp.configure()
        Messaging.messaging().delegate = self

    ...
}
  1. Add the following to the end of the AppDelegate class:
// Firebase addition
extension AppDelegate : MessagingDelegate {
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
        print("Firebase registration token: \(fcmToken)")
        
        let dataDict:[String: String] = ["token": fcmToken]
        NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
        // Note: This callback is fired at each app startup and whenever a new token is generated.
        // If necessary, send a token to the application server.
    }
    // Receive data messages on iOS 10+ directly from FCM (bypassing APNs) when the app is in the foreground.
    // To enable direct data messages, you can set Messaging.messaging().shouldEstablishDirectChannel to true.
    func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
        print("Received data message: \(remoteMessage.appData)")
    }
}
  1. Compile and run the application.
  • Check for MCE registration: search the log for `userId'.
  • Check for FCM registration: search the log for 'Firebase registration token'.
  1. Test your application for notification.
  • Check MCE notification using Acoustic API or UI.
  • Check FCM notification through Firebase console. For Firebase Messaging documentation, see here.

Did this page help you?