To send and receive mobile push notifications using the Android Mobile SDK, you will need to have Firebase Cloud Messaging configured. This article explains the necessary steps to complete the setup.
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 for the initial creation and configuration of Firebase on Android.
Once this step is completed, you will have obtained the google-services.json file and will have Google Play Services at both the project and app levels, and the Firebase dependency at the app level (within your Build.gradle files).
1.2. Configuration of FirebaseMessagingService
Within your Android project, you will need to implement the FirebaseMessagingService, which will allow you to receive notifications through onMessageReceived and register push token changes with onNewToken.
a. Receiving Notifications in onMessageReceived
In the onMessageReceived callback, you will receive notifications generated from your Workflows in Connectif. To handle these pushes, we use Connectif.handlePushNotification, which returns a boolean indicating whether the push was handled by Connectif. As shown in the implementation, we use this 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 wish to handle it at that moment, you can use Connectif.isConnectifPushNotification.
b. Register Push Token Changes
The first time your app starts, it generates a new token, which you can register through your service using onNewToken.
override fun onNewToken(token: String) {
Connectif.addPushToken(token)
}
Important note about permissions: When invoking the method Connectif.addPushToken(token), the Connectif SDK will automatically send, along with the new token, the current status of the user's push notification permissions 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 reset on a new device.
- The user uninstalls and reinstalls the app.
- The user clears the app data.
This will be the appearance of our FirebaseMessagingService implementation.
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)
}
}You should keep in mind that if it is not the first app launch, onNewToken will not be executed, so it is advisable to obtain the current push token from the Firebase instance and send it to Connectif. Suitable places to do this are during onCreate in your Application object or in your main Activity.
class SampleApp : Application() {
override fun onCreate() {
super.onCreate()
FirebaseMessaging.getInstance().token.addOnSuccessListener {
Connectif.addPushToken(it, applicationContext)
}
}
}
c. Add FirebaseMessagingService to the AndroidManifest File
To use the messaging service, you must add it 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 be able to send push notifications from Connectif, you will need to provide us with the private key (JSON file) of the Service Account for Google Cloud. The steps for creating this are as follows.
From the Firebase Console, access your project, then go to "Project Settings" and access the "Service Accounts" section.
Keep the Node.js checkbox selected and click "Generate new private key". This action will generate a JSON file which you will need later.
In Connectif, go to your store settings, to the Mobile App channel section. You will find an Android section that allows you to upload the previously downloaded Firebase file.
This is what your integration will look like once you have successfully uploaded your file.
After completing these steps, the Firebase Cloud Messaging integration will be ready to send push notifications from your 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 is assigned to your push notifications:
ic_default_notification
If you want, you can customize it by specifying the resource in the pushSmallIcon property of ConnectifConfig when initializing the SDK. Remember that to display correctly, it should 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 assign the notifications shown by Connectif the channel name "Default Channel" and "connectif_channel" as the channel identifier.
You can specify your custom name and channel using the pushChannelName and pushChannelId properties of ConnectifConfig when initializing the SDK.
3.3. Updating the Notification Permissions Status
If you need to force an update of the user's push notification permission status (for example, if the user has changed permissions from the Android system settings and you want to immediately reflect that change in Connectif), you can use the updatePushPermissionStatus() method.
This method allows you to send the current notification permission status of the device to the SDK at any time.
Connectif.updatePushPermissionStatus()Keep learning!
To make the most of your Connectif account, we recommend continuing with the following articles:
- Android SDK Get Started, to add Connectif Mobile SDK to your Android project.
- Complete Guide to Integrate Connectif with Your Mobile App, to thoroughly understand all the changes in this integration.
- iOS SDK Get Started, to add Connectif Mobile SDK to your iOS project.
- Apple Push Notifications Service Setup, to enable sending and receiving push notifications through the iOS Mobile SDK.