Skip to content

recommend-pro/recommend-android-sdk

Repository files navigation

Recommend Android SDK

GitHub release License

The Recommend Android SDK allows you to quickly and easily integrate Recommend personal customer experiences in your Android app.

Table of contents

Installation

Requirements

  • Android 5.0 (API level 21) and above

Configuration

Add the dependency in your build.gradle:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Add recommend-android-sdk to your build.gradle dependencies:

dependencies {
    implementation 'com.github.recommend-pro:recommend-android-sdk:$latest_version'
}

Getting Started

Before using Recommend SDK, you need to initialize it:

Recommend.init(
    context,
    "recommend_account_id",
    "recommend_application_id",
    "custom_api_host"
)

Usage

Device activity tracking

You can track device activities using device service:

Recommend.getDeviceService().trackActivity(
  DeviceAddToCartActivity(
    cartHash = "test_cart_hash",
    sku = "item_sku",
    variationSku = "item_variant_sku"
  ),
  Metrics(
    nonInteractive = false,
    data = listOf(
      Metrics.Metric(
        code = "metric_code",
        value = "metric_value"
      )
    )
  )
)

List of supported activities can be found here.

SDK automatically tracks some activities based on application state. You can disable automatic tracked events:

Recommend.getDeviceService().disableAutoEventTracking()

Push notification

Before using recommend push notification you need to follow Google's guide for setting up and configuring your Firebase app instance. See Set up a Firebase Cloud Messaging client app on Android on the Firebase website. You also need to receive push notifications by reviewing the Handling messages section as well.

Once Firebase was added to the project, you need to add handling of push notifications and new push tokens.

class FirebaseMessagingService: FirebaseMessagingService() {
  override fun onMessageReceived(message: RemoteMessage) {
    super.onMessageReceived(message)
    Recommend.getMessagingService().processMessage(message.data)
  }

  override fun onNewToken(token: String) {
    super.onNewToken(token)
    Recommend.getMessagingService().setPushToken(token)
  }
}

You can change subscription status using Message Service.

Recommend.getMessagingService().setSubscriptionStatus(MessagingPushSubscriptionStatus.SUBSCRIBED)

Keep in mind that before changing subscription status you mush set push token to SDK.

Recommend.getMessagingService().setPushToken(token)

For the correct working of smart campaigns that send push notifications, you need to add processing of push clicks. To do this, you need to add Intents handling.

Recommend.getMessagingService().onReceiveIntent(intent)

Also you can set push notification icon.

Recommend.getMessagingService().setSmallIconDrawable(iconDrawableId)

If you want to completely override the process of creating a notification, or just do something when a Recommend push notification is received, use onReceivePushNotificationListener. If you want to prevent Recommend SDK from creating notifications return true in the listener, or return false if you want to allow it to SDK.

Recommend.getMessagingService().setOnReceivePushNotificationListener { recommendPush ->
  //Do something with push data
  true
}

You can display an Android push notification by following Google's official guidelines.

Recommendation panels

To get personalised Recommend panels you can use Recommendation Service.

val context = RecommendationPanelRequest.RecommendationPanelRequestContext(
  searchTerm = "search_term",
  currentContext = RecommendationPanelRequest.RecommendationPanelRequestContext.CurrentContext(
    sku = "current_product_sku",
    listId = "current_list_id"
  ),
  skus = listOf("first_product_sku", "second_product_sku"),
  listIds = listOf("first_list_id", "second_list_id")
)

val metrics = Metrics(
  nonInteractive = true,
  data = listOf(
    Metrics.Metric(
      code = "metric_code",
      value = "metric_value"
    )
  )
)

val filters = listOf(
  RecommendationPanelRequest.RecommendationPanelRequestFilter(
    type = RecommendationPanelRequest.RecommendationPanelRequestFilter.Type.PRODUCT,
    code = "filter_code",
    operator = "filter_operator",
    value = "filter_value",
  )
)

val recommendationPanelRequests = RecommendationPanelRequest(
  id = relatedProductCollection.code,
  context = context,
  filters = filters,
  attrsToInclude = listOf(
    "CUSTOM_ATTR_KEY_1",
    "CUSTOM_ATTR_KEY_2",
    "CUSTOM_ATTR_KEY_3"
  ),
  variations = Variations(
    include = true,
    attrsToInclude = listOf("CUSTOM_ATTR_KEY_1", "CUSTOM_ATTR_KEY_2", "CUSTOM_ATTR_KEY_3")
  )
)

Recommend.getRecommendationService().getRecommendPanel(
  onLoad = { panels ->
    //Use panels data to show UI
  },
  onError = { error ->
    //Handle Recommend error
  },
  metrics = metrics,
  pageType = "page_type",
  panelRequests = recommendationPanelRequests
)

License

The contents of this repository are licensed under the MIT License.