Push Notification
Configuring your MoEngage Account
Copy the Sever Key from the FCM console and add it to the MoEngage Dashboard(Not sure where to find the Server Key refer to Getting FCM Server Key. To upload it, go to Settings Page and add the Server Key and package name.
Please make sure you add the keys both in Test and Live environment.

Before configuring MoEngage SDK for receiving push notifications please make you have configured Firebase in your application, if not done already refer to the documentation and complete setup.
Also, make sure you have added the Firebase Messaging dependency in your application's build.gradle
file.
Adding meta for push notification
To show push notifications you need to add the notification small icon and large icon along with the AppId to the MoEngage.Builder
val moEngage = MoEngage.Builder(this, "XXXXXXXX")
.setNotificationSmallIcon(R.drawable.icon)
.setNotificationLargeIcon(R.drawable.ic_launcher)
.build()
MoEngage.initialise(moEngage)
MoEngage moEngage =
new MoEngage.Builder(this, "XXXXXXXXXX")
.setNotificationSmallIcon(R.drawable.icon)
.setNotificationLargeIcon(R.drawable.ic_launcher)
.build();
MoEngage.initialise(moEngage);
Notification Small Icon Guidelines
Notification small icon should be flat, pictured face on, and must be white on a transparent background. Refer to the developer docs for more details.
For showing Push notifications there are 2 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 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 call in the optOutTokenRegistration()
on the MoEngage.Builder
as shown below
val moEngage = MoEngage.Builder(this, "XXXXXXXX")
.setNotificationSmallIcon(R.drawable.icon)
.setNotificationLargeIcon(R.drawable.ic_launcher)
.optOutTokenRegistration()
.build()
MoEngage.initialise(moEngage)
MoEngage moEngage = new MoEngage.Builder(this, "XXXXXXXXXX")
.setNotificationSmallIcon(R.drawable.icon)
.setNotificationLargeIcon(R.drawable.ic_launcher)
.optOutTokenRegistration()
.build();
MoEngage.initialise(moEngage);
Pass the Push Token To MoEngage SDK
The 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 below API to pass the push token to the MoEngage SDK.
PushManager.getInstance().refreshToken(applicationContext, token)
PushManager.getInstance().refreshToken(getApplicationContext(), token);
Note: Please make sure token is passed to MoEngage SDK whenever push token is refreshed and on application update. Passing token on 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 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)){
MoEPushHelper.getInstance().handlePushPayload(applicationContext,pushPayload)
}else{
// your app's business logic to show notification
}
if (MoEPushHelper.getInstance().isFromMoEngagePlatform(remoteMessage.getData())) {
MoEPushHelper.getInstance().handlePushPayload(getApplicationContext() ,pushPayload)
}else{
// your app's business logic to show notification
}
Push Registration and Receiving handled by SDK
Add the below code in your 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 token is refreshed.
An application can get this callback by implementing PushManager.OnTokenReceivedListener
and registering for a callback in the Application class' onCreate()
using PushManager.getInstance().setTokenObserver()
MoEngage SDK(version 9.2.00 above) is compiled with 17.3.2 hence expecting the client app to use Firebase version 17.3.2. However, we do support versions below 17.1.0 but your application needs to handle the token registration mechanism.
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
Rich landing page can be used to open a web url inside the app via a push notification.
To use 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>
Replace [ACTIVITY_NAME]
with the name that you want to appear on your rich landing page. Replace [PARENT_ACTIVITY_NAME]
with the parent activity name that you want.
In case you don't want any in-app to be shown when loading the rich landing please add the above mentioned com.moe.pushlibrary.activities.MoEActivity
to the in-app suppression list as mentioned here.
Geofence Push
By default, SDK does not track location neither geo-fence campaigns work by default.
To track location and run geo-fence campaigns you need to opt-in for location service by calling enableLocationServices()
on the MoEngage.Builder
instance.
val moEngage = MoEngage.Builder(this, "XXXXXXXX")
.setNotificationSmallIcon(R.drawable.icon)
.setNotificationLargeIcon(R.drawable.ic_launcher)
.optOutTokenRegistration()
.enableLocationServices()
.build()
MoEngage.initialise(moEngage)
MoEngage moEngage =
new MoEngage.Builder(this, "XXXXXXXXXX")
.setSenderId("xxxxxxx")
.setNotificationSmallIcon(R.drawable.icon)
.setNotificationLargeIcon(R.drawable.ic_launcher)
.enableLocationServices()
.build();
MoEngage.initialise(moEngage);
For Geo-fence pushes to work your Application should have location permission and Play Services' Location Library should be included.
Geofence hit callbacks
MoEngage SDK optionally notifies the application whenever a Geo-Fence is triggered. If required the application can consume the trigger and ask the SDK not to process it, alternatively, the application can just use the callback for logging/analytical purposes and let MoEngage process the trigger.
To get the callback application needs to implement OnGeofenceHitListener
API Reference and register this listener in the Application
class's onCreate()
using MoEGeofenceHelper.getInstance().registerGeofenceHitListener()
.
Updated over 5 years ago