-
Notifications
You must be signed in to change notification settings - Fork 16
How to integrate the Infobip Mobile Messaging SDK with another push notifications provider using a custom FirebaseMessagingService?
If you plan to integrate your mobile application with multiple push notification providers or
maybe you use FCM to process push from a native backend and don't want to migrate the entire codebase to use with Infobip
(for example there's still some use cases where you want to send notifications directly to Firebase), then you will
need to implement a custom service that extends the FirebaseMessagingService
.
The FirebaseMessagingService is a service which acts as an entry point for handling messages from FCM.
Extending this service is required for you to have one place in your app to handle all Firebase messages
and to act as a proxy object between multiple push notification providers. In this guide, we are interested in 2 methods:
onMessageReceived
and onNewToken
, which we will later override. The onMessageReceived
method is called when a message is received from the FCM and the onNewToken
method
is called when a new token is generated by the FCM. In order to handle new messages and tokens and to ensure a custom implementation
of these methods will be called upon, you have to add the following to your app AndroidManifest.xml
under the <application>
tag:
<application>
...
<service
android:name=".YourFirebaseMessagingServiceName"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
...
</application>
An Android application is supposed to have only one service registered for a com.google.firebase.MESSAGING_EVENT
action.
If there are multiple services registered for a com.google.firebase.MESSAGING_EVENT
action, only the first one declared in your app's
AndroidManifest.xml
will be used. To make sure that your custom service is the one that has this action and to not rely on the order specfified in the manifest,
add the following to your app's AndroidManifest.xml
under the <application>
tag for all services that extend the FirebaseMessagingService
:
<application>
...
<service
android:name=".FirstPushProviderFirebaseMessagingServiceName"
android:exported="false">
<intent-filter tools:node="remove">
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service
android:name=".SecondPushProviderFirebaseMessagingServiceName"
android:exported="false">
<intent-filter tools:node="remove">
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
...
</application>
Before implementing a custom FirebaseMessagingService
, ensure you are familiar with the methods provided by other push notification providers for handling incoming messages and token updates.
Here is an example of how to implement a custom FirebaseMessagingService
:
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import org.infobip.mobile.messaging.cloud.firebase.MobileMessagingFirebaseService
import com.infobip.webrtc.ui.service.InfobipRtcUiFirebaseService
class YourFirebaseService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
if (MobileMessagingFirebaseService.onMessageReceived(this, remoteMessage)) {
return
}
//NOTE: needed only if you use the Infobip RTC
if (InfobipRtcUiFirebaseService.onMessageReceived(this, remoteMessage)) {
return
}
// process non-Infobip notifications here or pass them to another push notification provider
// TODO your code
}
override fun onNewToken(token: String) {
MobileMessagingFirebaseService.onNewToken(this, token)
//NOTE: needed only if you use the Infobip RTC
InfobipRtcUiFirebaseService.onNewToken(this, token)
// process Firebase token here or pass it to another push notification provider
// TODO your code
}
}
expand to see Java code
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import org.infobip.mobile.messaging.cloud.firebase.MobileMessagingFirebaseService;
import com.infobip.webrtc.ui.service.InfobipRtcUiFirebaseService;
public class YourFirebaseService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (MobileMessagingFirebaseService.onMessageReceived(this, remoteMessage)) {
return;
}
//NOTE: needed only if you use the Infobip RTC
if (InfobipRtcUiFirebaseService.onMessageReceived(this, remoteMessage)) {
return;
}
// process non-Infobip notifications here or pass them to another push notification provider
// TODO your code
}
@Override
public void onNewToken(String token) {
MobileMessagingFirebaseService.onNewToken(this, token);
//NOTE: needed only if you use the Infobip RTC
InfobipRtcUiFirebaseService.onNewToken(this, token);
// process Firebase token here or pass it to another push notification provider
// TODO your code
}
}
More information about the Infobip RTC and the Firebase Messaging Service delegation can be found here.
Other push notification providers might use legacy methods to handle new FCM messages by implementing a BroadcastReceiver instead of a FirebaseMessagingService. However, they may still rely on FirebaseMessagingService for handling new tokens, so it remains important to implement a custom FirebaseMessagingService.
Without a properly implemented custom FirebaseMessagingService, Infobip's SDK will not be able to receive notifications and token updates. Since having a fresh FCM token is necessary not only for Infobip's SDK but also for other push notification providers, it is important to handle new tokens correctly in a custom FirebaseMessagingService. There are cases — such as depersonalization and repersonalization — where Infobip's SDK triggers a token update from FCM. If the custom FirebaseMessagingService does not propagate this new token properly to the other push notification providers, they might not be able to send notifications or display them correctly on the device.
If you encounter version conflicts of firebase-messaging
library, you can resolve it by adding the
following to your app's build.gradle
file:
dependencies {
...
configurations.configureEach {
resolutionStrategy {
force "com.google.firebase:firebase-messaging:versionNumber"
}
}
...
}
If you encounter version conflicts of kotlin-stdlib-jdk8
library, you can resolve it by adding the
following to your app's build.gradle
file:
dependencies {
...
configurations.configureEach {
resolutionStrategy {
force "org.jetbrains.kotlin:kotlin-stdlib-jdk8:versionNumber"
}
}
...
}
Infobip Mobile Messaging SDK automatically prompts users for push notification permission on Android 13 and newer. This default behavior can be disabled if you wish to request push notification permission via another push notification provider's SDK or if you prefer to handle it manually. For more information on how to disable automatic push notification permission handling and how to request push notification permission using Infobip's SDK, refer to this Android 13 Notification Permission Handling guide.
If you have any questions or suggestions, feel free to send an email to support@infobip.com or create an issue.
- Library events
- Server errors
- Users and installations
- Messages and notifications management
- Inbox
Geofencing API- DEPRECATED- Android Manifest components
- Privacy settings
- In-app chat
- Infobip RTC calls and UI
- Backup rules