Skip to main content

Overview

The MoEngage Android SDK fetches the recommended items for a recommendation you created on the MoEngage dashboard and returns them to your app as key-value data, handling user identity, networking, and caching internally.
Prerequisites
  • Integrate BOM 4.6.0 or above, which includes the Recommendations module. For more information, refer to Install using BOM.
  • Initialize the SDK in Application.onCreate(). For more information, refer to SDK Initialization.
  • Enable Recommendations for your workspace and create a recommendation on the MoEngage dashboard. For more information, refer to Recommendations Overview.

Install the Module

After you configure the BOM, add the Recommendations artifact in your app/build.gradle or app/build.gradle.kts. The BOM manages version numbers, so you do not specify a version.
Call MoERecommendationsHelper.fetchRecommendations() and attach onSuccess and onFailure callbacks to the returned task. Pass the Recommendation ID of the recommendation you want to fetch items from, as defined on the MoEngage dashboard. For the full parameter list, refer to the API reference.
fetchRecommendations() throws SdkNotInitializedException if it is called before the SDK is initialized.

Handle the Result

The onSuccess callback receives a RecommendedItems object on the main thread. Its items property is a List<Map<String, Any>>, where each map is one recommended item keyed by its attribute names. An empty list is a successful result and means no recommended items are available for the user. Attribute values are converted from the JSON response to Kotlin and Java types, and values nested inside objects and arrays follow the same rules. For the conversion rules, refer to the API reference.

Handle Failures

On failure, onFailure receives a MoERequestFailure object on the main thread. Use its reason property to identify the failure. Its message property carries details for logging only and can be empty. The SDK writes nothing to the cache on failure and leaves any cached items untouched. The reason property is either a RecommendationsFailureReason, which covers the failures specific to Recommendations, or a MoERequestFailureReason, which covers the failures common to all MoEngage SDK requests. For the full list of reasons, refer to the API reference. The SDK does not retry a failed request. To try again, call fetchRecommendations() again. Because the SDK caches nothing on failure, the next call makes a fresh network request. Retry only for these reasons:
  • MoERequestFailureReason.NetworkError
  • RecommendationsFailureReason.RATE_LIMIT_EXCEEDED, after a delay
  • RecommendationsFailureReason.INTERNAL_SERVER_ERROR
  • RecommendationsFailureReason.UNKNOWN_ERROR

Caching

The SDK caches the items it fetches, so repeated calls for the same recommendation do not trigger a network request.
  • The SDK scopes each cache entry to the combination of Recommendation ID, item ID, and included fields. Changing any of the three fetches a fresh list.
  • Within the cache duration, fetchRecommendations() returns the cached items without a network call.
  • After the cache duration elapses, the next call fetches the items from the server and refreshes the entry.
  • The SDK caches an empty result in the same way, so an empty recommendation does not trigger a network call on every screen render.
  • The SDK clears the cache when the user logs out, when the user identity changes, and when the SDK is disabled.

Configure the Cache Duration

The SDK caches recommended items for 24 hours (86400 seconds). To cache items for longer, pass a RecommendationConfig to MoEngage.Builder before you initialize the SDK. For more information, refer to SDK Initialization.
The duration is in seconds. The minimum cache duration is 86400 seconds (24 hours). The SDK ignores a smaller value and uses 86400 seconds instead.

FAQs

No, the SDK does not download or cache media. Use a standard image loading library for the image URLs in the response.
The call throws SdkNotInitializedException. Initialize the SDK in Application.onCreate() before you fetch recommendations. For more information, refer to SDK Initialization.
No, the SDK serves the cached items until the cache duration elapses. Configure a duration that matches how frequently your catalog changes.
The SDK invokes both onSuccess and onFailure on the main thread, so you update your views directly from either callback.