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. - Use
com.google.firebase:firebase-messaging25.1.2 or higher. This is the minimum version required for the Firebase Installation ID. - Generate FCM credentials and add them to the MoEngage dashboard. Refer to FCM Authentication and Push Configuration.
Overview
MoEngage Android SDK 15.03.00 registers devices for push using the Firebase Installation ID. Two flows are available, depending on which component generates the Installation ID:- Flow 1: MoEngage Handles Installation ID Generation describes the steps required for the MoEngage SDK to generate the Installation ID and display the notification.
- Flow 2: The Application Handles Installation ID Generation describes the steps required for your application to generate the Installation ID and pass it to the MoEngage SDK.
Flow 1: MoEngage Handles Installation ID Generation
Step 1: Declare the MoEngage Messaging Service
AddMoEFireBaseMessagingService to the manifest file.
Step 2: Enable the Firebase Installation ID
Add the following metadata to the manifest file.Register for the Installation ID (Optional)
Call the registerForInstallationId() API after MoEngage SDK initialization completes, so that the SDK registers for an Installation ID. This call is required only in the following cases:- Firebase auto-initialization is disabled in your application. Refer to the Firebase documentation on preventing auto initialization.
- MoEngage SDK initialization is delayed, for example when your application initializes the SDK on a worker thread instead of in the
onCreate()method of the Application class.
Get the Installation ID (Optional)
Apply one of the following options if your application needs to read the Installation ID that MoEngage generates.Option 1: Get the Installation ID Asynchronously
Register an InstallationIdAvailableListener in your Application class using the addInstallationIdListener() API. The SDK triggers the callback only for an Installation ID that it generates or updates after you register the listener. Call removeInstallationIdListener() to stop receiving the callback.Option 2: Get the Installation ID Synchronously
Call the getInstallationId() API from a worker thread. The API returnsnull if the Installation ID is not available when you call it.
Push Display for Non-MoEngage Payloads (Optional)
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 in the onCreate method 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.
Flow 2: The Application Handles Installation ID Generation
Skip this section if the MoEngage SDK generates the Installation ID.Step 1: Opt Out of MoEngage Push Token Registration
The MoEngage SDK registers for push by default. If your application generates the Installation ID, opt out of MoEngage registration first. Disable token registration through the configureFcm() API on the MoEngage.Builder object.Step 2: Pass the Installation ID to the MoEngage SDK
Your application must pass the Installation ID to the MoEngage SDK using the passInstallationId() API, because the MoEngage platform requires it to deliver notifications to the device.Step 3: 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 generates the Installation ID.The following declaration is included in current SDK versions. Add it only if you need to change the parent activity of
MoEActivity.