Skip to content

FCM Setup and Configuration

In most cases, user will requests BSA authentication from a non-mobile platform, such as a web application, it's important to have the ability to receive push notifications on the associated mobile app that has integrated the Android SDK.

This functionality relies on the proper setup of FCM registration and the capability to update the FCM token. This way, the user can seamlessly receive notifications on their mobile device regardless of the platform they initiated the authentication from.

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.

  1. Create or load the project in the Firebase Console.
  2. In top left, click on "⚙️" icon located beside Project Overview and click Project Settings
  3. In the tabs, click on Cloud Messaging tab.
  4. 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. Utilizing the registerPushToken() function from the Android SDK facilitates the process of linking and updating the FCM push token generated on an Android device. This ensures a streamlined mechanism for handling authentication notifications.

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.

Parameter

KeyValueDescription
tokenStringFCM Push Notification

Example

kotlin
//FCM Push Notification Token Registered
FirebaseMessaging.getInstance().token.addOnSuccessListener {
                Log.d("ABCDEFG", "FirebaseMessaging token : $it")
                try {
                    BSASdk.getInstance().registerPushToken(it, object :
                        SdkResponseCallback<TokenResponse> {
                        override fun onSuccess(result: TokenResponse?) {
                            Log.d("ABCDEFG", "registerPushToken onSuccess : $result")
                        }
                        override fun onFailed(errorResult: ErrorResult?) {
                            Log.d("ABCDEFG", "registerPushToken onFailed : $errorResult")
                            Log.d("ABCDEFG", "registerPushToken onFailed rtcode :" + errorResult!!.errorCode)
                            Log.d("ABCDEFG", "registerPushToken onFailed message: +" + errorResult!!.errorMessage)
            }
        })
    } catch (exception: Exception) {
        // Handle any exceptions that may occur
    }
}
// FCM Push Notification Token Renewed
class FirebasePushService : FirebaseMessagingService() {
    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        // Handle incoming push notification message
    }
    override fun onNewToken(token: String) {
        super.onNewToken(token)
        BSASdk.getInstance().registerPushToken(token, object : SdkResponseCallback<ResultResponse> {
            override fun onSuccess(result: ResultResponse?) {
                Log.d("ABCDEFG", "Result code: ${result?.rtCode}")
            }

            override fun onFailed(errorResult: ErrorResult?) {
                Log.d("ABCDEFG", "Error code: ${errorResult?.errorCode}")
            }
        })
    }
}

Result Response

When the API call to register the FCM Push token is successful, the response's rtCode will be 0. This indicates that the registration process was completed without any errors.

keyValueDescription
rtCode0ReturnMessage
rtMsgStringResult Message

Error Result

KeyValueDescription
errorCodeIntError Code

iii. FCM Push Message Transfer

If requesting an authentication on web using user key, a message is transferred through FCM on mobile application. In case of receiving a push message after an authentication, it proceeds an authentication process by calling responsePushMessage of BSASdk.

Parameter

KeyValueDescription
remoteMessageMap<String.String>Authentication push message

Example

kotlin
class FirebasePushService : FirebaseMessagingService() {
    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        BSASdk.getInstance().responsePushMessage(remoteMessage.data)
    }

    override fun onNewToken(token: String) {
        // Leave the method empty or add your desired implementation here
    }
}