Configuring Firebase Cloud Messaging
For showing Push notifications there are 2 important things
- Registration for Push that is generating push token.
- Receiving the Push payload from Firebase Cloud Messaging(FCM) service and showing the notification on the device.
Push registration can be handled by the application or MoEngage SDK. There is some configuration required based on whether the above-mentioned things are handled by the application or SDK.
Push Registration and Receiving are handled by App
By default, MoEngage SDK attempts to register for push token, since your application is handling push you need to opt out of SDK's token registration.
How to opt-out of MoEngage Registration?
To opt out of MoEngage's token registration mechanism, disable token registration using configureFCM() API while configuring FCM in the MoEngage.Builder as described
val moEngage = MoEngage.Builder(this, "XXXXXXXX")
.configureNotificationMetaData(NotificationConfig(R.drawable.small_icon, R.drawable.large_icon, R.color.notiColor, null, true, isBuildingBackStackEnabled = false, isLargeIconDisplayEnabled = true))
.configureFcm(FcmConfig(false))
.build()
MoEngage.initialiseDefaultInstance(moEngage)
MoEngage moEngage = new MoEngage.Builder(this, "XXXXXXXXXX")
.configureNotificationMetaData(new NotificationConfig(R.drawable.small_icon, R.drawable.large_icon, R.color.notiColor, "sound", true, true, true))
.configureFcm(new FcmConfig(false))
.build();
MoEngage.initialiseDefaultInstance(moEngage);
Pass the Push Token To MoEngage SDK
Your application would need to pass the Push Token received from FCM to the MoEngage SDK for the MoEngage platform to send out push notifications to the device.
Use the passPushToken() API to pass the push token to the MoEngage SDK.
MoEFireBaseHelper.getInstance().passPushToken(applicationContext,token)
MoEFireBaseHelper.getInstance().passPushToken(getApplicationContext(), token);
info |
Note Ensure token is passed to MoEngage SDK whenever push token is refreshed and on application update. Passing the token on the application update is important for migration to the MoEngage Platform. |
Passing the Push payload to the MoEngage SDK
To pass the push payload to the MoEngage SDK call the passPushPayload() API from the onMessageReceived() in the Firebase receiver.
Before passing the payload to the MoEngage SDK you should check if the payload is from the MoEngage platform using the isFromMoengagePlatfrom() helper API provided by the SDK.
if (MoEPushHelper.getInstance().isFromMoEngagePlatform(remoteMessage.data)){
MoEFireBaseHelper.getInstance().passPushPayload(applicationContext, remoteMessage.data)
}else{
// your app's business logic to show notification
}
if (MoEPushHelper.getInstance().isFromMoEngagePlatform(remoteMessage.getData())) {
MoEFireBaseHelper.Companion.getInstance().passPushPayload(getApplicationContext(), remoteMessage.getData());
}else{
// your app's business logic to show notification
}
Push Registration and Receiving are handled by SDK
warning |
Critical
|
Add the following code to the manifest file:
<service android:name="com.moengage.firebase.MoEFireBaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
Callbacks
We recommend you to add the callbacks in the onCreate() of the Application class since these callbacks can be triggered even when the application is in the background.
Token Callback
When MoEngage SDK handles push registration it optionally provides a callback to the application whenever a new token is registered or the token is refreshed.
To get the token callback implement the TokenAvailableListener and register for the callback using MoEFireBaseHelper.getInstance().addTokenListener().
Non-MoEngage Payload
If you are using the receiver provided by the SDK in your application's manifest file, SDK provides a callback in case a push payload is received for any other server apart from MoEngage Platform.
To get a callback implement the NonMoEngagePushListener and register for the callback using MoEFireBaseHelper.getInstance().addNonMoEngagePushListener.
Rich Landing
A rich landing page can be used to open a web URL inside the app via a push campaign.
The below configuration is only required if you want to add a parent activity to the Rich landing page, if not you can move to the next section.
To use a rich landing page you need to add the below code in the AndroidManifest.xml
<activity
android:name="com.moe.pushlibrary.activities.MoEActivity"
android:label="[ACTIVITY_NAME]"
android:parentActivityName="[PARENT_ACTIVITY_NAME]" >
</activity>
Parameter | Description |
---|---|
[ACTIVITY_NAME] |
Replace with the name that you want to appear on your rich landing page. |
[PARENT_ACTIVITY_NAME] |
Replace with the parent activity name that you want. |