3.1. Android: Sending Push Notifications via Firebase

🚧

Before you begin, make sure you’ve completed these steps:

šŸ‘

You've successfully completed the step "Sending Push Notifications on Android via Firebase", if:

Push notification has been sent from Maestra and is displayed on the device. Use this instruction to check if the push notifications are sent.

šŸ“˜

1. Integrate your app with Firebase

From the official Firebase integration guide, the following steps must be completed:

  • register your app with Firebase;
  • download the google-services.json file and place it as instructed;
  • enable Google Play services in your app;
  • add firebase-messaging to Gradle;
  • Provide the Firebase Server Key to your Maestra Forward Deployed Marketer.
dependencies{
implementation platform('com.google.firebase:firebase-bom:29.3.1')
implementation 'com.google.firebase:firebase-analytics-ktx'
implementation 'com.google.firebase:firebase-messaging-ktx'
...

}

2. Add Mindbox SDK explicitly to build.gradle

For the Mindbox SDK to function properly, add the dependency to the build.gradle file (app level).

šŸ“˜

Starting from version 2.10.0, you don't need to explicitly specify the Firebase version. Instead, specify the dependency without a version:

implementation 'cloud.mindbox:mindbox-firebase'

dependencies {  
...
implementation 'cloud.mindbox:mindbox-firebase' # since 2.10.0
...
}
dependencies {
		...
    implementation 'cloud.mindbox:mobile-sdk:{the latest vertion}'
    implementation 'cloud.mindbox:mindbox-firebase:{the latest vertion}'
		...
}

3. Pass the Firebase token to the SDK

  1. Create a new Kotlin class named MindboxFirebaseMessagingService.
  2. Make this class inherit from FirebaseMessagingService().
  3. Implement the onNewToken method.
import android.util.Log
import cloud.mindbox.mobile_sdk.Mindbox
import cloud.mindbox.mindbox_firebase.MindboxFirebase
import com.google.firebase.messaging.*

class MindboxFirebaseMessagingService: FirebaseMessagingService() {
override fun onNewToken(token: String) {
// Passing the token to Mindbox SDK
Mindbox.updatePushToken(applicationContext, token, MindboxFirebase) // since version 2.8.2
// Mindbox.updatePushToken(applicationContext, token) before version 2.8.2
}
}

4. Implement notification display

4.1. Create an ID, name, and description for the push notification channel

Once you add your first push notification, Maestra creates your own push notification channel. Consult your marketing department on how to name and describe it. For more details on Android notification channels, read here.

Use any unique string for your app as an ID.

class MindboxFirebaseMessagingService: FirebaseMessagingService() {

		...
    override fun onMessageReceived(remoteMessage: RemoteMessage) {

        val channelId = "< CHANNEL ID >" // "my_android_app_channel"
        val channelName = "< CHANNEL NAME >" // "Marketing mailings"
        val channelDescription = "< CHANNEL DESCRIPTION >"  // "Mailings containing ads"
				val pushSmallIcon = "< NOTIFICATION ICON >" // R.mipmap.ic_launcher
       }
}

4.2. Call the method to render push notifications

class MindboxFirebaseMessagingService: FirebaseMessagingService() {

		...
    override fun onMessageReceived(remoteMessage: RemoteMessage) {

       Mindbox.handleRemoteMessage(
            context = applicationContext,
            message = remoteMessage,
            activities = mapOf(),
            channelId = channelId, 
            channelName = channelName,
            pushSmallIcon = pushSmallIcon, 
            defaultActivity = MainActivity::class.java,
            channelDescription = channelDescription
        )
    }
}

The method returns TRUE if a push notification is processed and displayed successfully, or FALSE if the push notification isn't processed. You can link this status to the logic that applies different methods to process push notifications.
Example

import cloud.mindbox.mobile_sdk.Mindbox
import cloud.mindbox.mindbox_firebase.MindboxFirebase
import com.google.firebase.messaging.*

class MindboxFirebaseMessagingService: FirebaseMessagingService() {
    override fun onNewToken(token: String) {
        // Passing the token to Mindbox SDK
        Mindbox.updatePushToken(applicationContext, token, MindboxFirebase) // since version 2.8.2
        // Mindbox.updatePushToken(applicationContext, token) before version 2.8.2
    }

    override fun onMessageReceived(remoteMessage: RemoteMessage) {

        val channelId = "< CHANNEL ID >"      // "my_android_app_channel"
        val channelName = "< CHANNEL NAME >"         // "Marketing mailings"
        val channelDescription = "< CHANNEL DESCRIPTION >"  // "Mailings containing ads"
        val pushSmallIcon = R.mipmap.ic_launcher

        // The method returns a Boolean value to enable a fallback to process push notifications
        val messageWasHandled = Mindbox.handleRemoteMessage(
            context = applicationContext,
            message = remoteMessage,
            activities = mapOf(),
            channelId = channelId, 
            channelName = channelName,
            pushSmallIcon = pushSmallIcon, 
            defaultActivity = MainActivity::class.java,
            channelDescription = channelDescription
        )

        if (!messageWasHandled) {
            // You can code a fallback to process a push notification that is received from a non-Mindbox source or that contains incorrect data
        }
    }
}

4.3. Register the push notification processing service in AndroidManifest.xml

Add the following lines to the AndroidManifest.xml files:

<application ...>
  ...

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

  ...
</application>

5. Update your init method to always display push notifications

In the Application.onCreate method call Mindbox.initPushServices, passing MindboxFirebase as a parameter.
If your project doesn't have this class, you need to create it:

  1. New → Kotlin class.
  2. Insert the code from the example below.
  3. Register the class in AndroidManifest.xml.
import cloud.mindbox.mindbox_firebase.MindboxFirebase
import cloud.mindbox.mobile_sdk.Mindbox
import cloud.mindbox.mobile_sdk.pushes.MindboxPushService
  
class MainApplication : Application(), ReactApplication {

	override fun onCreate() {
    super.onCreate()
    Mindbox.initPushServices(this, listOf(MindboxFirebase))
    // Mindbox.initPushServices(this, listOf(MindboxHuawei, MindboxFirebase)) to work with both services
  }
}
šŸ‘

You've successfully completed the step "Sending Push Notifications on Android via Firebase", if:

Push notification has been sent from Maestra and is displayed on the device. Use this instruction to check if the push notifications are sent.