Push Notifications Setup for Firebase

🚧

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

👍

Expected result of the “Push Notifications Setup for Firebase” step:

You can send a mobile push notification from Maestra and it is displayed on the target device.

To check that your push notifications are sent correctly, please refer to this guide.

📘

1. Integrate your app with Firebase

Follow these steps from Google’s guidelines on how to Add Firebase to Your Android Project:

  • register your app with Firebase;
  • download and add the google-services.json configuration as instructed;
  • enable Google Play services in your app;
  • add firebase-messaging to build.gradle;
  • send the Firebase server key to your Maestra Forward Deployed Marketer.
dependencies {
    implementation platform('com.google.firebase:firebase-bom:33.7.0')
    implementation 'com.google.firebase:firebase-analytics-ktx'
    implementation 'com.google.firebase:firebase-messaging-ktx'
   ...
}

If your app uses targetSdk 33, you also need to add a permission to receive notifications (more details here, implementation example here).

In AndroidManifest.xml:

<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

In MainActivity.kt file:


private val requestPermissionLauncher =
    registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
        if (isGranted) {
            Mindbox.updateNotificationPermissionStatus(this)
        }
    }
 if (ContextCompat.checkSelfPermission(
                applicationContext,
                Manifest.permission.POST_NOTIFICATIONS
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
        }

2. Set up Notification Display

Suitable if NO custom logic is required.


Suitable when custom logic IS required.

📘

Use [setMessageHandling](https://developers.maestra.io/docs/android-sdk-methods) to define how the SDK should behave when image downloads fail, or to implement custom handling logic.

3. Register a push notification processing service to AndroidManifest.xml

Add the following code to AndroidManifest.xml:

<application ...>
  ...

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

  ...
</application>

4. Update your init method to always display push notifications

4.1 Add a library to process push notifications

Add a library to process push notifications to the dependencies block in app/build.gradle (Module: app).
It’s recommended to use a fixed version to keep updates under control. For the latest release, refer to the Maven Central Repository Search.

dependencies {
   ...
    implementation 'cloud.mindbox:mobile-sdk:{version}' 
    implementation 'cloud.mindbox:mindbox-firebase'
   ...
}

4.2 Specify the library in the init method

In the Mindbox.init method, which should be called from your app’s onCreate callback, specify that this build uses MindboxFirebase.

import cloud.mindbox.mobile_sdk.Mindbox
import cloud.mindbox.mobile_sdk.MindboxConfiguration
import cloud.mindbox.mindbox_firebase.MindboxFirebase

class MyApplication : Application() {
  override fun onCreate() {
        super.onCreate()
        // .... 

        // Before 
				// Mindbox.init(applicationContext, configuration, listOf())  
        
        // You need
        Mindbox.init(applicationContext, configuration, listOf(MindboxFirebase))  

        // ...

    }
}

Add a call to Mindbox.initPushServices in your app’s onCreate callback.

import cloud.mindbox.mobile_sdk.Mindbox
import cloud.mindbox.mindbox_firebase.MindboxFirebase
class MyApplication : Application() {
  override fun onCreate() {
        super.onCreate()
        // ....
        Mindbox.initPushServices(applicationContext, listOf(MindboxFirebase))
        Mindbox.init(applicationContext, configuration, listOf(MindboxFirebase))
        // ...
    }
}
👍

Verify the result of the “Push Notifications Setup for Firebase” step:

A mobile push notification is sent from Maestra and is displayed on the device.

You can use this guide to verify that push notifications are being sent.