Overview

Skill Level: Advanced

This tutorial explains how to configure mobile app messages alongside FCM (Firebase Cloud Messaging) in iOS apps that are developed with Swift.

Prerequisites

This tutorial assumes you are familiar with mobile app messages in iOS apps developed with Swift. If not, follow the tutorial here.

 

Not what you’re looking for? Check out all our available tutorials for mobile app messaging here.

Step-by-step

  1. Download frameworks and relevant plugins

    Download iOS SDK frameworks, IBMMobilePush.framework, IBMMobilePushNotificationService.framework, relevant plugins and add them to your project.

  2. Exit Xcode and update for Firebase

    1. From the command line, in your current Xcode project folder do the following:

        $ 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 provide 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
    

    4. Pull the Firebase pods and libraries

    $ pod update
    
  3. Make the following modifications in your application

    1. Start Xcode and open the app workspace (from now on – do not open the project file but the workspace)
    2. Update Build Setting ->Linker Flags > `$(inherited)` to Other Linker flags

      Firebase using swift

    3. Update MceConfig.json file and add the appKey and all the other settings you normally do.
    4. Update Capabilities (Push, Background, Maps)
    5. Download GoogleService-Info.plist from FCM (https://console.firebase.google.com/). Move it to your project folder and add it to Xcode project.
    6. 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 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 token to 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)")
          }
      }
      
  4. 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:’
  5. Test your application for notification

    • Check MCE notification using Acoustic API or UI
    • Check FCM notification via https://console.firebase.google.com/project/{project-name}/notification

Expected outcome

Need more help? Check out all of our available tutorials for mobile app messaging here.

 

For Firebase Messaging documentation check here.

Join The Discussion

Your email address will not be published. Required fields are marked *