Configuring your MoEngage Account
- Ensure you have configured the Firebase application.
- Ensure you have the FCM server key.
- Ensure you add the FCM Server Key in the Dashboard --> Settings --> Channel --> Push --> Mobile Push --> Android.
Ensure you add the keys in both the Test and Live environment. - Ensure that you have added the Firebase Messaging dependency in your application's build.gradle file.
Adding metadata for push notification
Metadata regarding the notification is required to show push notifications where the small icon and large icon drawable are mandatory.
For more information about API reference for all the possible options, refer to NotificationConfig.
Use the configureNotificationMetaData() to transfer the configuration to the SDK.
val moEngage = MoEngage.Builder(this, "XXXXXXXX")
.configureNotificationMetaData(NotificationConfig(R.drawable.small_icon, R.drawable.large_icon, R.color.notiColor, null, true, isBuildingBackStackEnabled = true, isLargeIconDisplayEnabled = true))
.build()
MoEngage.initialise(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))
.build();
MoEngage.initialise(moEngage);
Ensure that the SDK is initialized with the metadata in the onCreate() of the Application class for push notifications to work.
info |
Notification Small Icon Guidelines The notification small icon should be flat, pictured face on, and must be white on a transparent background. |
Configuring Firebase Cloud Messaging
For showing Push notifications, there are two important things:
- Registration for Push, i.e. generating push token.
- Receiving the Push payload from Firebase Cloud Messaging(FCM) service and showing the notification on the device.
The above can either 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 handled by App
By default, MoEngage SDK attempts to register for a 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 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.initialise(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.initialise(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 following API to pass the push token to the MoEngage SDK.
MoEFireBaseHelper.getInstance().passPushToken(applicationContext,token)
MoEFireBaseHelper.getInstance().passPushToken(getApplicationContext(), token);
info |
Note Ensure the token is passed to MoEngage SDK whenever the push token is refreshed and on application update. Passing tokens on application updates 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 MoEngage API from the onMessageReceived() from the Firebase receiver.
Before passing the payload to the MoEngage SDK, you should check if the payload is from the MoEngage platform using the 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 handled by SDK
warning |
Critical When using the SDK version |
Add the following code to the manifest file:
<service android:name="com.moengage.firebase.MoEFireBaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
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.
An application can get this callback by implementing FirebaseEventListener
and registering for a callback in the Application class onCreate()
using MoEFireBaseHelper.getInstance().addEventListener()
For more about the listener, refer to the API reference.
warning |
Critical
|
Registration using Sender-id
In case you want to register for push via Sender id instead of the default config in the google-services.json
you need to pass the sender-id to the SDK in the FcmConfig
object while initializing the SDK and add the firebase-iid
dependency in your app's build.gradle
file.
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(true, "<sender_id>"))
.build()
MoEngage.initialise(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(true, "<sender_id>"))
.build();
MoEngage.initialise(moEngage);
Registration using Instance-Id has been deprecated by Google, and support is removed from MoEngage in the future version.
info |
Note At any point, your application's manifest should have only one service with intent filter com.google.firebase.MESSAGING_EVENT. If there is more than one service, only the first service will receive the callback, and MoEngage SDK might never receive the Push Payload resulting in poor delivery rates. |
Rich Landing
A rich landing page can be used to open a web URL inside the app via a push notification/in-app/card campaign.
To use a rich landing page, you need to add the below code to 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. |