Integrate iOS SDK with Firebase using Swift
Configure the iOS SDK with Firebase Cloud Messaging (FCM) in iOS apps developed with Swift.
- Download the following and add them to your project.
- AcousticMobilePush.xcframework,
- AcousticMobilePushNotification.xcframework,
- and relevant plugins.
- To update for Firebase, exit Xcode. From the command line, in your current Xcode project folder, run
$ pod init
. - Using a text editor, edit Podfile.
- 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
- Pull the Firebase pods and libraries
$ pod update
. - Start Xcode and open the app workspace (from now on - do not open the project file but the workspace).
- Update Build Setting >Linker Flags > $(inherited) to Other Linker flags.
- Update the MceConfig.json file and add the
appKey
and the other config settings. - Update Capabilities (Push, Background, Maps).
- Download GoogleService-Info.plist from FCM. Move it to your project folder and add it to the Xcode project.
- In the import section of AppDelegate.swift (in the target SwiftSample in the sample app), add:
// Firebase addition
import Firebase
import Messages
- Extend the
AppDelegate
class as follows:
@objc class AppDelegate : UIResponder, UIApplicationDelegate, UISplitViewControllerDelegate
{
var window: UIWindow?
...
}
- 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
...
}
- 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)")
}
}
- 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'.
- 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.
Updated over 3 years ago