PrivMX DOCS
Kotlin

Entries

Working with Inbox Entries.

All the data sent by someone to an Inbox is called an Entry. In Threads and Stores, a user must be assigned to the container to send data. In Inboxes, however, anyone who has Inbox ID can send a reply (assuming they have the Bridge URL and Solution ID).

The sample code on this page is based on the initial assumptions.

Entry's publicMeta and privateMeta fields support any kind of data formats encoded to byte arrays. Examples in this section use kotlinx-serialization-json dependency to serialize object to JSON format.

Sending Entries using Public API

Public submissions require using connectPublic function, provided by the Connection class, to establish a public connection to the platform. After connecting, create an InboxApi instance, which allows to operate on Inboxes.

val connection = Connection.connectPublic(
    solutionId,
    bridgeUrl,
)
val publicInboxApi = InboxApi(connection)

Assuming you need some kind of structure in entries, define a data struct InboxPublicEntryData, and its instance: Define structure of data sending to Inbox entry:

@Serializable
data class InboxPublicEntryData(
    val name: String,
    val surname: String,
    val email: String,
    val comment : String
)

Now, having established public connection and inboxApi, you can send data to the Inbox:

val inboxID = "INBOX_ID"
val inboxPublicEntryData = InboxPublicEntryData(
    "NAME",
    "SURNAME",
    "EMAIL",
    "COMMENT"
)

publicInboxApi.prepareEntry(
    inboxID,
    Json.encodeToString(inboxPublicEntryData).encodeToByteArray(),
)?.let {
    publicInboxApi.sendEntry(it)
}

Getting Entries

Created entries can be listed by non-public connections created using PrivmxEndpoint class.

Define Entry item class with decoded data:

data class EntryItem(
    val entry: InboxEntry,
    val decodedData: InboxPublicEntryData
)

Fetching the most recent Entries in given Inbox:

val inboxID = "INBOX_ID"
val startIndex = 0L
val pageSize = 100L

val entriesPagingList = inboxApi.listEntries(
    inboxID,
    startIndex,
    pageSize,
    SortOrder.DESC
)

val entries = entriesPagingList.readItems.map {
    EntryItem(
        it,
        Json.decodeFromString(it.data.decodeToString())
    )
}

Reading Entries Files

Files from Entries can be read by non-public connections created using PrivmxEndpoint class.

val entryID = "ENTRY_ID"
val inboxEntry = inboxApi.readEntry(entryID)

val files: List<File> = inboxEntry.files
val filesContent: List<ByteArray> = files.map { file ->
    inboxApi.openFile(file.info.fileId)!!
        .let { fileHandle ->
            var fileContent = ByteArray(0)
            do {
                val chunk = inboxApi.readFromFile(fileHandle, StoreFileStream.OPTIMAL_SEND_SIZE)
                fileContent += chunk
            } while (chunk.size.toLong() == StoreFileStream.OPTIMAL_SEND_SIZE)
            inboxApi.closeFile(fileHandle)
            fileContent
        }
}

We use cookies on our website. We use them to ensure the proper functioning of the site and, if you agree, for purposes we set, such as analytics or marketing.

PrivMX Endpoint Kotlin v2.2

This package is not up to date with the core documentation. Some of the features you've seen described in other parts of the documentation might not be mentioned here. Those changes do not influence compatibility, however

On this page