Firebase Cloud Messaging Configuration and Sending Mobile Push Notifications in Connectif

To send and receive mobile push notifications via the Android Mobile SDK, you will need to configure Firebase Cloud Messaging. This article explains the necessary steps to complete the configuration.

 

1. Create and configure the project in Firebase

1.1. Create and configure a Firebase project

The first step is to have a project set up in the Firebase console. If you don't have one, you can follow Google's guide for creating and configuring Firebase on Android. 

Once this step is completed, you will have the `google-services.json` file, Google Play Service at the project and app levels, and the Firebase dependency in your app-level `Build.gradle` files.

 

1.2. Configuring FirebaseMessagingService

In your Android project, you will need to implement the FirebaseMessagingService, which allows you to receive notifications via onMessageReceived and register push token changes with onNewToken.

a. Receiving notifications in onMessageReceived

In the `onMessageReceived` call, you will receive notifications generated from your Connectif workflows. To handle these pushes, use Connectif.handlePushNotification, which will return a boolean indicating whether the push was handled by Connectif. As seen in the implementation, this allows you to handle all other pushes that don't come from Connectif.

override fun onMessageReceived(remoteMessage: RemoteMessage) {
if (!Connectif.handlePushNotification(
remoteMessage.data,
applicationContext
)

) {
// Other push providers
}

}

If you want to check whether the push came from Connectif but don't want to handle it at that moment, you can use Connectif.isConnectifPushNotification.

b. Registering push token changes

The first time the app starts, a new token is generated, which can be registered through your service with onNewToken.

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

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 data.

Here is what the implementation of FirebaseMessagingService 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)
}
}

If it's not the app's first start, `onNewToken` will not run, so it is recommended to get the current push token from the Firebase instance and send it to Connectif. The best places to do this are in the `onCreate` method of your `Application` object or your main `Activity`.

class SampleApp : Application() {

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

}
}

 

c. Adding FirebaseMessagingService to the AndroidManifest file

To use the messaging service, it must be added to your AndroidManifest file.

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

 

2. Configure Android in the Connectif Mobile App Integration

To send pushes from Connectif, you will need to provide the private key (JSON file) of the Service Account for Google Cloud. The steps to create this are as follows.

From the Firebase Console, go to your project, and within "Project Settings," access the "Service Accounts" section.

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

 

Keeping the check on Node.js, click on "Generate new private key." This action will generate a JSON file that you will need later.

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

 

localhost_3002_store_64886d8852c77f00f7b2379e_store-edit_tabId=mobileApp.png

 

This is how your integration will look once you have successfully uploaded the file.

 

localhost_3002_store_64886d8852c77f00f7b2379e_store-edit_tabId=mobileApp (1).png

 

After completing these steps, your Firebase Cloud Messaging integration will be ready to send pushes from your workflows.

 

3. Additional configuration

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

3.1. Icon 

By default, we will assign an icon to your push notifications:

ic_default_notification default_push_icon.png 

If you want, you can customize it by specifying the resource in the pushSmallIcon property of ConnectifConfig when starting the SDK. Remember that it must contain only white and transparent areas to display correctly.

3.2. Notification channel

Since Android 8, it is mandatory to create a channel to display notifications. By default, Connectif will assign the name "Default Channel" and the channel identifier "connectif_channel" to notifications.

You can provide your own custom channel name and identifier using the pushChannelName and pushChannelId properties of ConnectifConfig when initializing the SDK.

 

 

 

Congratulations!
You’ve reached the end of the lesson.

  

Still have unresolved questions?
Remember that our Connectif specialists are available to help. To contact them, just open a support ticket by clicking the blue “Help” button on your dashboard.


Keep learning!

To take full advantage of your Connectif account, we recommend continuing with the following articles:

Was this article helpful?
0 out of 0 found this helpful