Receiving Push Notifications from Multiple Providers
What this step achievesYour app correctly handles push notifications from Maestra and any other provider side by side, without conflicts.
Make the following changes in the FirebaseMessagingService subclass you created previously.
Step 1: Send the token to your backend
In the onNewToken method, add a call to send the token to your backend:
override fun onNewToken(token: String) {
Mindbox.updatePushToken(
context = applicationContext,
token = token,
pushService = MindboxFirebase
)
// Your method to send the Firebase token to your backend
sendTokenToYourBackend(token)
}Step 2: Detect the push source
Use isMindboxPush in onMessageReceived to determine whether the incoming message came from Maestra. Pass the message received in onMessageReceived to the method.
For Firebase, call it on the MindboxFirebase object.
override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)
if (MindboxFirebase.isMindboxPush(remoteMessage = message)) {
val messageWasHandled = Mindbox.handleRemoteMessage(
context = applicationContext,
message = message,
activities = activities,
channelId = channelId,
channelName = channelName,
pushSmallIcon = pushSmallIcon,
defaultActivity = defaultActivity,
channelDescription = channelDescription
)
}
}Step 3: Render non-Maestra notifications
Add an else branch to handle notifications from your other providers:
override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)
if (MindboxFirebase.isMindboxPush(remoteMessage = message)) {
val messageWasHandled = Mindbox.handleRemoteMessage(
context = applicationContext,
message = message,
activities = activities,
channelId = channelId,
channelName = channelName,
pushSmallIcon = pushSmallIcon,
defaultActivity = defaultActivity,
channelDescription = channelDescription
)
} else {
// Your code to display notifications from other providers
showNotification(title = message.data["title"], body = message.data["body"])
}
}Full example
class MindboxFirebaseMessagingService : FirebaseMessagingService() {
override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)
if (MindboxFirebase.isMindboxPush(remoteMessage = message)) {
val messageWasHandled = Mindbox.handleRemoteMessage(
context = applicationContext,
message = message,
activities = activities,
channelId = channelId,
channelName = channelName,
pushSmallIcon = pushSmallIcon,
defaultActivity = defaultActivity,
channelDescription = channelDescription
)
} else {
// Your code to display notifications from other providers
showNotification(title = message.data["title"], body = message.data["body"])
}
}
override fun onNewToken(token: String) {
Mindbox.updatePushToken(
context = applicationContext,
token = token,
pushService = MindboxFirebase
)
// Your method to send firebase token to your backend
sendTokenToYourBackend(token)
}
}Important: data pushes vs. notification pushes
How Android handles notification types
onMessageReceived()is only triggered for data pushes. If your app is in the background or killed and the incoming message is a notification push, Android renders it directly —onMessageReceived()is never called.
Updated 6 months ago

