Listening to Content Card Events
This tutorial explains how to listen to content card events in your application.
Overview
The Messaging extension provides a way to listen to events from content cards displayed in your application. The following functions can be implemented in conformance with the ContentCardUIEventListener
interface:
onDisplay
onDismiss
onInteract
Implement ContentCardUIEventListener
Complete the following steps to hear content card events:
- Implement the ContentCardUIEventListener interface in your class.
Kotlin
Copied to your clipboardclass ContentCardCallback: ContentCardUIEventListener {override fun onDisplay(aepUI: AepUI<*, *>) {Log.d("ContentCardCallback", "onDisplay")}override fun onDismiss(aepUI: AepUI<*, *>) {Log.d("ContentCardCallback", "onDismiss")}override fun onInteract(aepUI: AepUI<*, *>,interactionId: String?,actionUrl: String?): Boolean {Log.d("ContentCardCallback", "onInteract $interactionId $actionUrl")// If the url is handled here, return truereturn false}}
- Pass the listener to the ContentCardEventObservers class when retrieving the card composable.
Kotlin
Copied to your clipboard@Composableprivate fun AepContentCardList(viewModel: AepContentCardViewModel) {// Create the ContentCardUIEventListenerval contentCardCallback = ContentCardCallback()// Collect the state from ViewModelval aepUiList by viewModel.aepUIList.collectAsStateWithLifecycle()// Create row with composables from AepUI instancesLazyRow {items(aepUiList) { aepUI ->when (aepUI) {is SmallImageUI -> {val state = aepUI.getState()if (!state.dismissed) {SmallImageCard(ui = aepUI,style = smallImageCardStyleRow,// provide the ContentCardUIEventListener as a parameter to the // ContentCardEventObserverobserver = ContentCardEventObserver(contentCardCallback))}}}}}}
Handling actionable URLs
The onInteract
method provides an optional actionURL
parameter associated with the interaction event. The return value of this method determines how the URL is handled.
Returns
true
if your application has successfully handled the URL. This indicates to the SDK that no further action is needed.Returns
false
to allow the SDK to process the URL.
Kotlin
Copied to your clipboardoverride fun onInteract(aepUI: AepUI<*, *>,interactionId: String?,actionUrl: String?): Boolean {actionUrl?.let {// handle action url herereturn true}return false}