FCM Setup and Configuration
There can be a circumstance when the user requests BSA authentication from a non-mobile platform such as the web. To receive the push notification in the mobile app that has implemented the iOS SDK, FCM registration and the token update feature is necessary.
i. FCM Server Key
In order to receive the push notifications, the Server Key generated by Firebase Cloud Messaging (FCM) must be sent together with the client issuance request. The FCM Server Key can be found in the process below.
- Create or load the project in the Firebase Console.
- In top left, click on "⚙️" icon located beside Project Overview and click Project Settings
- In the tabs, click on Cloud Messaging tab.
- Copy Server Key from Cloud Messaging API. If you do not have the server key yet, You can enable Cloud Messaging API and generate the key. More about Cloud Messaging API can be find in the linked documentation.
Important:
It is essential to note that the FCM server key is a crucial component of the process. This key plays a vital role in ensuring the smooth registration of clients on the platform.
ii. FCM Push Notification Token Registration
Registering the FCM push token is pivotal to enable effective notification of authentication requests.
It should be called within func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) in Firebase's MessagingDelegate.
Registration and update should only proceed if the registered FCM token and the FCM token received as a parameter are different.
Important:
The FCM push token may undergo changes due to factors like expiration. To accommodate such scenarios, it's important to update the FCM push token by utilizing the registerPushToken() function. This ensures that the most current and valid token is in use for seamless notification functionality.
- AppDelegate.swift
extension AppDelegate : MessagingDelegate {
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
// Mandatory SDK Call: FCM Token Registration and Refresh
BSA.FCM.tokenUpdate(fcmToken: fcmToken)
}
}
Parameter
Key | Value | Description |
---|---|---|
fcmToken | String | FCM token for receiving authentication push notifications. |
iii. Load FCM Data
It should be called from the UNUserNotificationCenterDelegate function (see code).
When calling BSA.FCM.load, if parsing of the data in the notification is successful, the callback registered with BSA.FCM.registerPushNotificationProcessor(callBack:) is immediately invoked.
- AppDelegate.swift
extension AppDelegate : UNUserNotificationCenterDelegate {
//When the device OS version is higher than iOS 13, it will receive the notification and handle by this method
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let content = notification.request.content
let userInfo = content.userInfo
UIApplication.shared.applicationIconBadgeNumber = 0
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
Messaging.messaging().appDidReceiveMessage(userInfo)
// Mandatory SDK Call: Loading Push Data
if BSA.FCM.load(notification: notification) {
print("userNotificationCenter willPresent : push load success")
completionHandler([.sound])
return
}
completionHandler([.sound, .badge, .banner])
}
//When the device OS version is higher than iOS 13 and user clicks the push notification, it will be handled by this method
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
// Mandatory SDK Call: Loading Push Data
if BSA.FCM.load(notification: response.notification) {
print("userNotificationCenter didReceive : push load success")
completionHandler()
return
}
defer {
completionHandler()
}
guard response.actionIdentifier == UNNotificationDefaultActionIdentifier else {
return
}
}
}
Parameter
BSA.FCM.load(notification: UNNotification)
Key | Value | Description |
---|---|---|
notification | UNNotification | Notification with Push Data. |
iv. Detect Authentication Requests
To handle authentication requests even when the app is entered by the user's own action or through screen transitions without launching the notification, BSA.FCM.checkRequestedAuthExists() must be called in SceneDelegate's sceneWillEnterForeground when an authentication push is received.
- SceneDelegate.swift
func sceneWillEnterForeground(_ scene: UIScene) {
// Mandatory SDK Call: Check for Authentication Requests When Switching from Background to Foreground
BSA.FCM.checkRequestedAuthExists { isExisted in
// Send UI update notification when necessary
let notification = Notification(name: NAME_CHECK_REQ_AUTH_EXISTS,
object: nil,
userInfo: ["isExisted": isExisted])
NotificationCenter.default.post(notification)
}
}
vi. Use FCM Push Data
If parsing is successful in BSA.FCM.load(notification: UNNotification), this function calls the registered callback function. BSA.FCM.PushData is used for authentication logic and should be stored separately or processed for authentication logic.
// Mandatory SDK Call: Register Callback for Push Message Loading Completion
BSA.FCM.registerPushNotificationProcessor { data in
// Check Push Type
switch data.target {
case .Request:
// Display Alert for Push Authentication Request
// showPushAuthenticationAlert(....)
// Request Authentication Logic Using Data
case .Change:
// Call Change When OTP Input Is Successful During OTP Authentication Process
default:
PCGPrint.d("push process : skip")
}
}
Parameter
public typealias Callback = (BSA.FCM.PushData)->()
BSA.FCM.registerPushNotificationProcessor(_ callback: @escaping Callback)
BSA.FCM.registerPushNotificationProcessor
Key | Value | Description |
---|---|---|
callback | Callback | Callback function that receives Push Data as a parameter. |
BSA.SDK.PushData
Mainly used values in BSA.SDK.PushData
Key | Value | Description |
---|---|---|
target | PushType | Types of push notifications: Request, Success, Cancel, Failed, Change. |
clientName | String | Name of the client requesting authentication. |
client_key | String | ClientKey of the client requesting authentication. |
siteUrl | String | Client's website address. |
block_key | String | Block key to be used for authentication. |
channel_key | String | Channel key to be used for authentication. |
otpAuth | Bool | Whether it is OTP authentication or not. |
identifier | String | An identifier that can be used for control in NotificationCenter. |