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.Install the Module
After you configure the BOM, add the Recommendations artifact in yourapp/build.gradle or app/build.gradle.kts. The BOM manages version numbers, so you do not specify a version.
Fetch Recommended Items
CallMoERecommendationsHelper.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.
Handle the Result
TheonSuccess 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.NetworkErrorRecommendationsFailureReason.RATE_LIMIT_EXCEEDED, after a delayRecommendationsFailureReason.INTERNAL_SERVER_ERRORRecommendationsFailureReason.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 aRecommendationConfig 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
Does the SDK render the recommended items?
Does the SDK render the recommended items?
No, the SDK returns the item data only. Your application parses the response and builds the user interface.
Does the SDK download the images referenced in the items?
Does the SDK download the images referenced in the items?
No, the SDK does not download or cache media. Use a standard image loading library for the image URLs in the response.
What happens if I call fetchRecommendations() before I initialize the SDK?
What happens if I call fetchRecommendations() before I initialize the SDK?
The call throws
SdkNotInitializedException. Initialize the SDK in Application.onCreate() before you fetch recommendations. For more information, refer to SDK Initialization.Can I force a fresh fetch before the cache expires?
Can I force a fresh fetch before the cache expires?
No, the SDK serves the cached items until the cache duration elapses. Configure a duration that matches how frequently your catalog changes.
On which thread does the SDK deliver the result?
On which thread does the SDK deliver the result?
The SDK invokes both
onSuccess and onFailure on the main thread, so you update your views directly from either callback.