Prerequisites
Complete the following setup before you begin:- Create a Firebase project for your application and add the
google-services.jsonconfiguration file. Refer to the Firebase Android setup guide. - Generate FCM credentials and add them to the MoEngage dashboard. Refer to FCM Authentication and Push Configuration.
Overview
Push notifications require two mandatory steps:- Push token registration. The device registers for push, and Firebase generates a push token.
- Push display. The application receives the push payload from Firebase Cloud Messaging (FCM) and displays the notification on the device.
- Push Token Registration and Display by the MoEngage SDK describes the steps required for the MoEngage SDK to register the token and display the notification.
- Push Token Registration and Payload Handling by the Application describes the steps required for your application to register the token and receive the notification payload.
Push Token Registration and Display by the MoEngage SDK
Add the following service to the manifest file so that the MoEngage SDK receives the notification.Token Callback
Implement a token callback to access the push token that MoEngage registers on behalf of your application. When the MoEngage SDK handles registration, it provides a callback each time it registers or refreshes a token. Implement the TokenAvailableListener interface and register it using MoEFireBaseHelper.getInstance().addTokenListener(). Register the callback in theonCreate() method of your Application class, because the SDK can invoke it while the application is in the background.
Push Display for Non-MoEngage Payloads
Implement this callback if your application handles notifications that originate outside MoEngage. When you declare the SDK service in your manifest file, the SDK provides a callback for each push payload it receives from a server other than the MoEngage platform. Implement the NonMoEngagePushListener interface and register it using MoEFireBaseHelper.getInstance().addNonMoEngagePushListener(). Register the callback in theonCreate() method of your Application class, because the SDK can invoke it while the application is in the background.
Push Token Registration and Payload Handling by the Application
Skip this section if the MoEngage SDK handles push token registration and display. The MoEngage SDK registers for a push token by default. If your application registers the token, opt out of MoEngage token registration first.Opt Out of MoEngage Token Registration
Disable token registration through the configureFcm() API on the MoEngage.Builder object.Pass the Push Token to the MoEngage SDK
Your application must pass the push token it receives from FCM to the MoEngage SDK, because the MoEngage platform requires the token to deliver notifications to the device. Use the passPushToken() API.Pass the token to the MoEngage SDK every time FCM refreshes it. Passing the token on application update is required for migration to the MoEngage platform.
Pass the Push Payload to the MoEngage SDK
Pass payloads from the MoEngage platform to the MoEngage SDK even when your application declares its own service, because the SDK renders the notification, including rich push notifications. Call the passPushPayload() API from theonMessageReceived() method of your Firebase service. Check whether the payload originates from the MoEngage platform using the isFromMoEngagePlatform() API before you pass it.
Rich Landing
A rich landing page opens a web URL inside the application from a push campaign. This step is required to display rich landing pages, regardless of which component handles push token registration and display.The following declaration is included in current SDK versions. Add it only if you need to change the parent activity of
MoEActivity.