Android: Coroutines

Description on using the SDK asynchronous functionality from Kotlin Coroutines

From version 16.0.0 the Android SDK has extension methods added to the SailthruMobile and MessageStream classes to allow asynchronous functionality to be accessed in a suspendible way from coroutines. Each piece of functionality has two variations, one will return a Result containing the outcome of the call, the other will return desired value or throw an exception if there is an error. Use whichever one best fits your use-case!

Coroutine Extensions

SailthruMobile Extensions:

// Get Device ID
val deviceId = SailthruMobile().getDeviceId()
// or
val deviceId = SailthruMobile().getDeviceIdResult().getOrElse { error ->
    // Handle error
    null
}

// Clear Device
SailthruMobile().clearDevice(SailthruMobile.EVENTS)
// or
SailthruMobile().clearDeviceResult(SailthruMobile.EVENTS).onFailure { error ->
    // Handle error
}

// Set User ID
SailthruMobile().setUserId("USER_ID")
// or
SailthruMobile().setUserIdResult("USER_ID").onFailure { error ->
    // Handle error
}

// Set User Email
SailthruMobile().setUserEmail("USER_EMAIL")
// or
SailthruMobile().setUserEmailResult("USER_EMAIL").onFailure { error ->
    // Handle error
}

// Enable/Disable Geo IP
SailthruMobile().setGeoIPTrackingEnabled(true)
// or
SailthruMobile().setGeoIPTrackingEnabledResult(true).onFailure { error ->
    // Handle error
}

// Get Recommendations
val recommendations = SailthruMobile().getRecommendations("SECTION_ID")
// or
val recommendations = SailthruMobile().getRecommendationsResult("SECTION_ID").getOrElse { error ->
    // Handle error
    null
}

// Track Pageview
SailthruMobile().trackPageview(URI("www.example.com"), listOf("tag1, tag2"))
// or
SailthruMobile().trackPageviewResult(URI("www.example.com"), listOf("tag1, tag2")).onFailure { error ->
    // Handle error
}

// Track Impression
SailthruMobile().trackImpression("SECTION_ID", listOf(URI("www.example.com/page1"), URI("www.example.com/page2")))
// or
SailthruMobile().trackImpressionResult("SECTION_ID", listOf(URI("www.example.com/page1"), URI("www.example.com/page2"))).onFailure { error ->
    // Handle error
}

// Track Click
SailthruMobile().trackClick("SECTION_ID", URI("www.example.com"))
// or
SailthruMobile().trackClickResult("SECTION_ID", URI("www.example.com")).onFailure { error ->
    // Handle error
}

// Set Attributes
val attributeMap = AttributeMap().apply {
  putString("hello", "there")
}
SailthruMobile().setAttributes(attributeMap)
// or
SailthruMobile().setAttributesResult(attributeMap).onFailure { error ->
    // Handle error
}

// Remove Attribute
SailthruMobile().removeDeviceAttribute("ATTRIBUTE_KEY")
// or
SailthruMobile().removeDeviceAttributeResult("ATTRIBUTE_KEY").onFailure { error ->
    // Handle error
}

// Set Profile Vars
val profileVars = JSONObject().apply { 
  put("hello", "there")
}
SailthruMobile().setProfileVars(profileVars)
// or
SailthruMobile().setProfileVarsResult(profileVars).onFailure { error ->
    // Handle error
}

// Get Profile Vars
val profileVars = SailthruMobile().getProfileVars()
// or
val profileVars = SailthruMobile().getProfileVarsResult().getOrElse { error ->
    // Handle error
    null
}

// Log Purchase
val purchaseItem = PurchaseItem(1, "item", 1234, "item12345", URI("www.example.com/item12345"))
val purchase = Purchase(arrayListOf(purchaseItem))
SailthruMobile().logPurchase(purchase)
// or
SailthruMobile().logPurchaseResult(purchase).onFailure { error ->
    // Handle error
}

// Log Abandoned Cart
val purchaseItem = PurchaseItem(1, "item", 1234, "item12345", URI("www.example.com/item12345"))
val purchase = Purchase(arrayListOf(purchaseItem))
SailthruMobile().logAbandonedCart(purchase)
// or
SailthruMobile().logAbandonedCartResult(purchase).onFailure { error ->
    // Handle error
}

// Handle Sailthru Link
val parsedLink = SailthruMobile().handleSailthruLink(Uri.parse("SAILTHRU_LINK"))
// or
val parsedLink = SailthruMobile().handleSailthruLinkResult(Uri.parse("SAILTHRU_LINK")).getOrElse { error ->
    // Handle error
    null
}

MessageStream extensions:

// Get Messages
val messages = MessageStream().getMessages()
// or
val messages = MessageStream().getMessagesResult().getOrElse { error ->
    // Handle error
    arrayListOf()
}

// Get Message
val message = MessageStream().getMessage("MESSAGE_ID")
// or
val message = MessageStream().getMessageResult("MESSAGE_ID").getOrElse { error ->
    // Handle error
    null
}

// Delete Message
MessageStream().deleteMessage(message)
// or
MessageStream().deleteMessageResult(message).onFailure { error ->
    // Handle error
}

// Get Unread Count
val unreadCount = MessageStream().getUnreadMessageCount()
// or
val unreadCount = MessageStream().getUnreadMessageCountResult().getOrElse { error ->
    // Handle error
    null
}

// Set Message Read
MessageStream().setMessageRead(message)
// or
MessageStream().setMessageReadResult(message).onFailure { error ->
    // Handle error
}

// Set Messages Read
MessageStream().setMessagesRead(messages)
// or
MessageStream().setMessagesReadResult(messages).onFailure { error ->
    // Handle error
}

Flows

We have also added flows as an alternative to providing listener implementations to the SDK to handle events. For the NotificationReceived, NotificationTapped and NotificationActionTapped events you can now instead subscribe to these SharedFlow instances to process the events (note that subscribing to the flows will block the current coroutine, so you should launch a separate coroutine for each subscriber).

launch {
  SailthruMobile().notificationReceivedFlow.collect { (context, bundle) ->
      // Handle received event
  }
}

launch {
  SailthruMobile().notificationTappedFlow.collect { (context, bundle) ->
      // Handle tapped event
  }
}

launch {
  SailthruMobile().notificationActionTappedFlow.collect { (context, bundle, actionTapped) ->
      // Handle action tapped event
  }
}

Did this page help you?