Communicate with the Experience

The SDK allows you to pass parameters, as well as send and receive custom messages to the embedded Experience so as to assist in developing more complex integrations.

This enables us to share information, such as authentication or user preferences (such as language), between the app and Experience, or to react to user interactions in either, such as notifying your app that the user requested to log in.


Sending parameters to the Experience initialisation

The first and simplest way to provide data to the Experience is via embed parameters. This allows the parent application to pass a dictionary of strings to the embedded Experience at the time the Experience is being initialised.

This approach is specially useful for data you want to seed the Experience with from its very inception, such as the language the user prefers, or a parameter that will customise the Experience from start to finish.

The following snippet outlines how this is achieved on each platform:

...
val config = ExperienceConfiguration(
    parameters = hashMapOf("custom_key" to "custom_value")
)

val experienceView = Launcher.default.getExperience(context, config)
...

Sending and receiving messages

The other way to exchange data to the Experience is via sending and receiving messages. This allows the parent application and the Experience to send and receive messags between each other.

This is useful for updating the state of the Experience during its runtime, such as notifying about changes in the user log in status, as well as to notify about events, such as a change in the preferred user language.

Awaiting for the Experience to be ready

Given that embedding the Experience on the page is an asynchronous process, we need to await for it to become ready before we can start sending messages, otherwise they wouldn't be received correctly.

To identify that the Experience is ready, we can register to receive a notification. The following snippet showcases how to do so:

...
val experienceViewListener = object: ExperienceViewListener {
    override fun onReady(experienceView: ExperienceView) {
        // experience is ready to accept incoming messages
    }
}
val experience = Launcher.default.getExperience(this, config).apply {
    listener = experienceViewListener
}
...

Sending a message

A message is an object composed of the following:

  • An action identifying its purpose.

  • A data payload to be exchanged.

We can send a message by using the following code:

...
val experience = Launcher.default.getExperience(this, config)
experience.sendMessage("my_action", mapOf("my" to "payload"))
...

Receiving messages from the Experience

Just as your application can send messages to the Experience, so can the Experience send you messages. Using a snippet similar to the next you'll be notified about messages sent by the Experience, and allow you to treat them as expected:

...
val experienceViewListener = object: ExperienceViewListener {
    override fun onMessage(experienceView: ExperienceView, message: Message) {
        when (message.action) {
            "my_action" -> {
                handlePayload(message.payload)
            }
            else -> {}
        }
    }
}

val experience = Launcher.default.getExperience(this, config).apply {
    listener = experienceViewListener
}
...

Responding to a message

In some scenarios, you'll want to have a request/response pattern when communicating with the Experience. To do so, you will need to first receive a message to respond to, and then respond to it using a snippet similar to the following:

...
override fun onMessage(experienceView: ExperienceView, message: Message) {
    when (message.action){
        "my_action" -> {
            respondToRequest(message, mapOf("my" to "payload"))
        }
        else -> {}
    }
}
...

Awaiting a response

We also offer the possibility of sending a message, and await for a response from the Experience. The following snippet demonstrates how to do so in each of the supported platforms:

...
val experience = Launcher.default.getExperience(this, config)

experience.sendRequest("my_action", mapOf("my" to "payload")){    
    it.onSuccess { payload ->        
        Log.d("Launcher", "Response payload: $payload")
    }    
    it.onFailure { error ->        
        Log.e("Launcher", "Response payload: ${error.message}")
    }
}
...

Last updated