By M. Shahrukh
on 31 Jan 2019
  • FCM
  • Push Notification
  • Swift

Abstract

The purpose of writing this documentation is to explain the process of integrating FCM in the iOS app. This documentation will include steps related both Xcode, firebase, developer account and keychain access. We briefly discuss all other tools related to accomplishing the project.

Table of Content

  1. Introduction
    1. What is FCM
    2. How does it work
    3. About FCM messages

  2. Steps for integrating Push notification
    1. xCode setup
    2. Developer account

Introduction

a. What is Firebase Cloud Messaging(FCM)

Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you reliably deliver messages at no cost. Using FCM, you can notify a client app that new email or other data is available to sync. You can send notification messages to drive user re-engagement and retention. For use cases such as instant messaging, a message can transfer a payload of up to 4KB to a client app.

b. How does it Work?

An FCM implementation includes two main components for sending and receiving

1. A trusted environment such as Cloud Functions for Firebase or an app server on which to build, target, and send messages.

2. An iOS, Android, or web (JavaScript) client app that receives messages.

c. About FCM Messaging

Firebase Cloud Messaging (FCM) offers a broad range of messaging options and capabilities. The information in this page is intended to help you understand the different types of FCM and what you can do with them.

With FCM, you can send two types of messages to clients:

  • Notification messages sometimes thought of as "display messages." These are handled by the FCM SDK automatically.
  • Data messages, which are handled by the client app.

Steps for integrating Push notification

Xcode Set-up

  1. Create a project
  2. Manage code signing


3. Go to Capabilities and switch on push notifications and background mode (check remote notifications).


Settings in Developer Account

  1. Login to same developer account that you used in code signing.
  2. Go to certificate, id's and profiles.
    1. Go to App IDs
    2. Locate your bundle identifier that you used in you xCode project
    3. Open Your project bundle id


    4. Click Edit after opening bundle id




    5. Locate push notifications and click "create certificates"





  3. After clicking create certificate it will require you to create CSR file and select continue.
  4. Following the steps
    1. open key chain access from you system
    2. select keychain from toolbar go to "certificate assistant" and select "request the certificate authority"



    3. certificate assistant
      1. type your account email
      2. enter a common name
      3. select save to disk

  5. Choose the CSR file that you just have created using certificate assistant.

  6. After uploading download the certificate



  7. Repeat 5 and 6 processes for production also.

Keychain access

  1. After downloading double-click the certificate to add it into the keychain access.
  2. Open keychain access and locate the added certificate



  3. Right the certificate and export the certificate where ever you like it.
  4. After exporting it will create a .p12 file.
  5. repeat 1,2,3, and 4 for production downloaded certificate also.

Firebase integration

  1. Open firebase.google.com
  2. sign-in to your google account (if you do not have an account create one).
  3. click go to console on the top right corner of the web page
  4. Click add project

  5. set project and agree to terms and conditions.
  6. Now go to project settings exists in left side navigation drawer.

  7. Scroll down to add project iOS, Android or web project (click iOS one).



  8. Add bundle identifier(required), App nick name (optional) and app store id(optional) and click register.




  9. Download the google-services-info-plist file and add this into your project





  10. Create a podfile into your project folder using

    "pod init"

  11. Add this to your podfile

    pod 'Firebase/Core'

  12. now run command

    Pod install

  13. Now close your XCode project and open newly created file

    projectname.xcworkspace

  14. Now Go to app delegate file and import

    import Firebase
    import UserNotifications

  15. now add protocol extension in your app delegate class

    UNUserNotificationCenterDelegate


Use this code in your appDelegate did finish launch.

       if #available(iOS 10.0, *) {

// For iOS 10 display notification (sent via APNS)

UNUserNotificationCenter.current().delegate = self

let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]

UNUserNotificationCenter.current().requestAuthorization(

options: authOptions,

completionHandler: {_, _ in })

// For iOS 10 data message (sent via FCM)

//FIRMessaging.messaging().remoteMessageDelegate = self

} else {

let settings: UIUserNotificationSettings =

UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)

application.registerUserNotificationSettings(settings)

}

FirebaseApp.configure()

application.registerForRemoteNotifications()


  1. Add this function to your appDelegate file

      func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {



    let chars = (deviceToken as NSData).bytes.bindMemory(to: CChar.self, capacity: deviceToken.count)

    var token = ""

    for i in 0..<deviceToken.count {

    token += String(format: "%02.2hhx", arguments: [chars[i]])

    }

    if let localToken = InstanceID.instanceID().token() {

    self.fcmToken = localToken

    }

    print("Device Token = ", fcmToken)

    }


  2. Now go to Firebase project settings and select cloud messaging from the navigation



  3. Now add p12 that exported from keychain access and upload for both development and production.



  4. Tested notification screenshot by following the same steps.