3.1. Sending Push Notifications Using Firebase
Before you begin, make sure you’ve completed these steps:
By the end of this guide, you should see the following results:
You can send a mobile push notification from Mindbox 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 gradle; - send the Firebase server key to your project manager.
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. Pass the Firebase token to the SDK
- Create a new kotlin class named
MindboxFirebaseMessagingService
. - Make this class a subclass of
FirebaseMessagingService()
. - Implement the
onNewToken
method.
import android.util.Log
import cloud.mindbox.mobile_sdk.Mindbox
import com.google.firebase.messaging.*
class MindboxFirebaseMessagingService: FirebaseMessagingService() {
override fun onNewToken(token: String) {
// Passing the token to Mindbox SDK
Mindbox.updatePushToken(applicationContext, token)
}
}
3. Displaying notifications
3.1 Specify the URLs allowed in your push notifications and map them with app activities
Besides a URL set up in the Project control panel, every push notification can display up to 3 buttons, each with their own URLs.
Build a Map to ensure that app launches are processed correctly and the correct activity is opened. The Map should specify the URLs and activities to open.
To display standard push notifications, Mindbox offers the built-in Mindbox.handleRemoteMessage
method that processes the received push notification, downloads its image and displays the notification.
When building Map Activities, you can use a mask with the *
character to describe your URL.
For example, https://mindbox.cloud/push/*
can work for:
https://mindbox.cloud/push/
,https://mindbox.cloud/push/1
,https://mindbox.cloud/push/foo
,https://mindbox.cloud/push/foo?product=123
.
You can specify a single activity to open push notifications and buttons with such URLs.
class MindboxFirebaseMessagingService: FirebaseMessagingService() {
...
override fun onMessageReceived(remoteMessage: RemoteMessage) {
// List of URLs and activities to be launched with the URLs
val activities = mapOf(
"https://MY-SITU.com/CATALOG" to CatalogActivity,
"https://MY-SITU.com/PRODUCT" to ProductActivity,
)
}
}
3.2 Identifying an Activity invoked by default
This default Activity is called if a push notification contains a URL that is missing from the list.
class MindboxFirebaseMessagingService: FirebaseMessagingService() {
...
override fun onMessageReceived(remoteMessage: RemoteMessage) {
// Default Activity to be called when a URL comes that is missing from the list
val defaultActivity = MainActivity::class.java
}
}
3.3 Create an ID, title and description for your push notification channel
Once you add your first push notification, Mindbox will create your own push notification channel. Consult your marketing team on how you want to name and describe it. For more details on notification channels, please refer to this guide on Android Notification Channels.
Use any string that is unique 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 messages"
val channelDescription = "< CHANNEL DESCRIPTION >" // "Messages containing ads"
val pushSmallIcon = "< PUSH ICON >" // R.mipmap.ic_launcher
}
}
3.4 Call the method to render your push notifications
class MindboxFirebaseMessagingService: FirebaseMessagingService() {
...
override fun onMessageReceived(remoteMessage: RemoteMessage) {
Mindbox.handleRemoteMessage(
context = applicationContext,
message = remoteMessage,
activities = activities,
channelId = channelId,
channelName = channelName,
pushSmallIcon = pushSmallIcon,
defaultActivity = defaultActivity,
channelDescription = channelDescription
)
}
}
If the push notification is successfully processed and displayed, the method will return TRUE
. Otherwise, it will return FALSE
.
An example of a complete file
import cloud.mindbox.mobile_sdk.Mindbox
import com.google.firebase.messaging.*
class MindboxFirebaseMessagingService: FirebaseMessagingService() {
override fun onNewToken(token: String) {
// Passing the token to Mindbox SDK
Mindbox.updatePushToken(applicationContext, token)
}
override fun onMessageReceived(remoteMessage: RemoteMessage) {
// Default activity to be called when receiving a URL that is missing from the list
val defaultActivity = MainActivity::class.java
val channelId = "< CHANNEL ID >" // "my_android_app_channel"
val channelName = "< CHANNEL NAME >" // "Marketing messages"
val channelDescription = "< CHANNEL DESCRIPTION >" // "Messages containing ads"
val pushSmallIcon = R.mipmap.ic_launcher
// List of URLs and activities that different URLs invoke
val activities = mapOf(
"https://mindbox.cloud/" to defaultActivity,
)
// The method returns a Boolean value to enable a fallback to process push notifications
val messageWasHandled = Mindbox.handleRemoteMessage(
context = applicationContext,
message = remoteMessage,
activities = activities,
channelId = channelId, // Channel ID for notifications sent from Mindbox
channelName = channelName,
pushSmallIcon = pushSmallIcon, // Small icon for notifications
defaultActivity = defaultActivity,
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
}
}
}
3.5 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
To control updates, specify a release version in the Module: app dependencies
block in the app/build.gradle
file. For the latest release, refer to the Maven Central Repository Search.
dependencies {
...
implementation 'cloud.mindbox:mobile-sdk:{version}'
implementation 'cloud.mindbox:mindbox-firebase:{version}'
...
}
4.2 Specify the library in the init method
Specify that the build should use MindboxFirebase: go to the Mindbox.init
API method that is part of the onCreate
callback of your app.
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))
// ...
}
}
You should now see that:
Mindbox can send a mobile push notification and the target device displays it.
Updated about 1 year ago