Firebase Cloud Messaging setup and sending mobile push notifications in Connectif

To send and receive mobile push notifications using the Android Mobile SDK, you need to have Firebase Cloud Messaging configured.
This article explains the steps required to complete the setup.

 

Before you begin

Keep in mind that Connectif can identify the device to which notifications should be sent in two ways:

  • Registration token (push token): the classic Firebase Cloud Messaging method.
  • Firebase Installation ID (FID): available starting with SDK version X.Y.Z. Firebase keeps it associated with the app installation, so it is more stable than the token.
 

You must use only one of the two options. If you send both the token and the FID to Connectif, the device may receive duplicate notifications. The steps for each option are described in section 1.2.b.

 

1. Create and configure the project in Firebase

1.1. Create and configure a project in Firebase

The first step is to have a project configured in the Firebase console. If you do not have one, you can follow the Google guide to create and initially configure Firebase for Android. 

Once this step is complete, you will have obtained the google-services.json file, Google Play Services configured at the project and app levels, and the Firebase dependency at the app level (within your Build.gradle files).

 

1.2. Configuring FirebaseMessagingService

In your Android project, you need to have the FirebaseMessagingService service implemented, which will allow you to receive notifications through onMessageReceived and register the device identifier: the push token with onNewToken or the Firebase Installation ID with onRegistered.

 

a. Receiving notifications in onMessageReceived

In the onMessageReceived call, we will receive notifications generated from our Workflows in Connectif. To handle these pushes, use Connectif.handlePushNotification, which will return a Boolean indicating whether that push was handled by Connectif. As shown in the implementation, we would use it to handle all pushes that do not come from Connectif.

override fun onMessageReceived(remoteMessage: RemoteMessage) {
    if (!Connectif.handlePushNotification(
            remoteMessage.data,
            applicationContext
        )
    ) {
        // Other push providers
    }
}

If you want to identify whether the push comes from Connectif and do not want to handle it at that time, you can use Connectif.isConnectifPushNotification.

 

b. Registering the device: push token or Firebase Installation ID

Choose one of the following two options. Remember that you should use only one of them.

 

Option A: push token

The first time our app starts, it generates a new token, which we can register through our service using onNewToken.

override fun onNewToken(token: String) {
    Connectif.addPushToken(token)
}

 

Important note about permissions: When calling the Connectif.addPushToken(token) method, the Connectif SDK automatically sends, together with the new token, the user's current push notification permission status on the device. This ensures that Connectif always has up-to-date information about the user's ability to receive notifications in real time.

 

This token may change in the following cases: 

  • The app is restored on a new device.
  • The user uninstalls and reinstalls the app.
  • The user clears the app's data.

This is what our FirebaseMessagingService implementation will look like.

class MyFirebaseMessagingService : FirebaseMessagingService() {
    private val notificationManager by lazy {
        getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    }

    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        if (!Connectif.handlePushNotification(
                remoteMessage.data,
                notificationManager,
                applicationContext
            )
        ) {
            // Other push providers
        }
    }

    override fun onNewToken(token: String) {
        Connectif.addPushToken(token)
    }
}

Keep in mind that if this is not the first time the app has started, onNewToken will not run. Therefore, it is a good idea to obtain the current push token from the Firebase instance and send it to Connectif. Appropriate locations for this are onCreate in our Application object or in our main Activity.

class SampleApp : Application() {

    override fun onCreate() {
        super.onCreate()
            FirebaseMessaging.getInstance().token.addOnSuccessListener {
            Connectif.addPushToken(it, applicationContext)
        }
    }
}

 

Option B: Firebase Installation ID (FID)

Requirements:

  • Connectif Android SDK version X.Y.Z or higher.
  • Firebase Cloud Messaging version X.Y.Z or higher.

1. Enable the FID in the AndroidManifest by adding the metadata indicated in section c.

2. Register the app when it starts during onCreate in our Application object.

class SampleApp : Application() {

    override fun onCreate() {
        super.onCreate()
        FirebaseMessaging.getInstance().register()
    }
}

If your app has automatic Firebase Messaging startup disabled (firebase_messaging_auto_init_enabled), this call is essential: without it, the FID will not receive notifications.

3. Send the FID to Connectif from onRegistered in our FirebaseMessagingService. This is what our implementation will look like:

class MyFirebaseMessagingService : FirebaseMessagingService() {
    private val notificationManager by lazy {
        getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    }

    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        if (!Connectif.handlePushNotification(
                remoteMessage.data,
                notificationManager,
                applicationContext
            )
        ) {
            // Other push providers
        }
    }

    override fun onRegistered(installationId: String) {
        Connectif.addFirebaseInstallationId(installationId, applicationContext)
    }
}

Always send the FID you receive in onRegistered, because it is already registered to receive notifications. Do not use the one returned by FirebaseInstallations.getInstance().id: it may not be registered yet, and the notifications would not be delivered.

 

Important note about permissions: As with addPushToken, the Connectif.addFirebaseInstallationId method automatically sends, together with the FID, the user's current push notification permission status on the device.

 

The FID changes if the user uninstalls and reinstalls the app or clears its data. In that case, Firebase calls onRegistered again with the new FID.

Migration from the push token: If your app was already using option A, replace that implementation with these steps and remove all calls to addPushToken. Connectif will automatically remove the previous token the next time it is sent.

 

c. Add FirebaseMessagingService to the AndroidManifest file

To use the messaging service, we need to add it to our AndroidManifest file.

<service
    android:name=".mypackages.MyFirebaseMessagingService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

If you use option B (FID), also add the following metadata inside the <application> tag:

<meta-data
    android:name="firebase_messaging_installation_id_enabled"
    android:value="true" />

 

2. Configure Android in the Connectif Mobile App Integration

To send push notifications from Connectif, you need to provide us with the private key (JSON file) for the Google Cloud Service Account. The steps to create it are as follows.

From the Firebase Console, access your project. Under "Project settings," access "Service accounts."

console.firebase.google.com_u_3_project_sdk-test-app-51cdb_settings_serviceaccounts_adminsdk.png

 

Keeping Node.js selected, click "Generate new private key." This action will generate a JSON file that we will need later.

In Connectif, go to your store settings and the Mobile App channel section. You will find an Android section where you can upload the file previously downloaded from Firebase.

 

localhost_3002_store_64886d8852c77f00f7b2379e_store-edit_tabId=mobileApp.png

 

This is what our integration will look like once we have successfully uploaded our file.

 

localhost_3002_store_64886d8852c77f00f7b2379e_store-edit_tabId=mobileApp (1).png

 

Once these steps are complete, the Firebase Cloud Messaging integration will be ready to send push notifications from our workflows.

 

3. Additional configuration

Remember that when initializing the SDK, you can define some values using the ConnectifConfig parameter in Connectif.initialize().

 

3.1. Icon 

By default, an icon will be assigned to our push notifications:

ic_default_notification default_push_icon.png 

If you wish, you can customize it by specifying the resource in the pushSmallIcon property of ConnectifConfig when initializing the SDK. Remember that, to display correctly, it must contain only white color and transparency.

 

3.2. Notification channel

Starting with Android 8, it is mandatory to create a channel to display notifications. By default, we will assign "Default Channel" as the channel name and "connectif_channel" as the channel ID for notifications displayed by Connectif.

You can specify a custom name and channel using the pushChannelName and pushChannelId properties of ConnectifConfig when initializing the SDK.

 

3.3. Updating notification permission status

If you need to force an update of the user's push notification permission status (for example, if the user has changed the permissions in the Android system settings and you want to reflect that change immediately in Connectif), you can use the updatePushPermissionStatus() method.

This method allows you to send the device's current notification permission status to the SDK at any time.

Connectif.updatePushPermissionStatus()
 

Congratulations!
You have reached the end of the lesson.

  

Do you still have unanswered questions?
Remember that our Connectif specialists are available to help. To contact them, simply open a Support ticket by clicking the blue “Help” button on your dashboard.


Keep learning!

To make the most of your Connectif account, we recommend continuing with the following articles: