---
source_url: https://www.pubnub.com/docs/chat/kotlin-chat-sdk/build/features/messages/send-receive
title: Send and receive messages
updated_at: 2026-06-12T11:22:54.773Z
---

> Documentation Index
> For a curated overview of PubNub documentation, see: https://www.pubnub.com/docs/llms.txt
> For the full list of all documentation pages, see: https://www.pubnub.com/docs/llms-full.txt


# Send and receive messages

:::note Requires Message Persistence
Enable [Message Persistence](https://youtu.be/qLMtbINWGig) on your keyset in the [Admin Portal](https://admin.pubnub.com/) to store messages.
:::

## Send

Use `sendText()` to send plain text messages to a channel with lightweight delivery options. If you need to send rich content (mentions, channel references, links, files, or quoted messages), use [MessageDraft](https://www.pubnub.com/docs/chat/kotlin-chat-sdk/learn/chat-entities/message-draft) instead.

### Method signature

This method takes the following parameters:

```kotlin
channel.sendText(
    text: String,
    params: SendTextParams = SendTextParams(),
): PNFuture<PNPublishResult>
```

The `SendTextParams` data class consolidates optional message parameters:

```kotlin
data class SendTextParams(
    val meta: Map<String, Any>? = null,
    val shouldStore: Boolean = true,
    val usePost: Boolean = false,
    val ttl: Int? = null,
    val customPushData: Map<String, String>? = null,
)
```

#### Input

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| text | String | Yes |  | Text that you want to send to the selected channel. |
| params | SendTextParams | Optional | `SendTextParams()` | Object containing additional message parameters. |
| > meta | Map<String, | Optional | `null` | Publish additional details with the request. |
| > shouldStore | Boolean | Optional | `true` | If `true`, the messages are stored in Message Persistence. |
| > usePost | Boolean | Optional | `false` | When `true`, the SDK uses HTTP POST to publish the messages. |
| > ttl | Int? | Optional | `null` | Defines if / how long (in hours) the message should be stored in Message Persistence. If shouldStore = true, and ttl = 0, the message is stored with no expiry time., If shouldStore = true and ttl = X, the message is stored with an expiry time of X hours unless you have message retention set to Unlimited on your keyset configuration in the Admin Portal., If shouldStore = false, the ttl parameter is ignored., If ttl is not specified, then the expiration of the message defaults back to the expiry value for the keyset. |
| > customPushData | Map<String, | Optional | `null` | Additional key-value pairs added to the FCM and/or APNS push messages. |

#### Output

| Type | Description |
| --- | --- |
| `PNFuture<PNPublishResult>` | Returned object with a value of any type. |

### Sample code

Send the `Hi Everyone!` message to the `support` channel. Mark its `high` priority and state that it should be stored in Message Persistence for `15` hours.

```kotlin
chat.getChannel("support").async { result ->
    result.onSuccess { channel ->
        // handle success
        channel.sendText(
            text = "Hi Everyone!",
            params = SendTextParams(
                meta = mapOf("messageImportance" to "high"),
                shouldStore = true,
                ttl = 15 // 15 hours
            )
        ).async { sendResult ->
            sendResult.onSuccess { publishResult ->
                // handle success
                println("Message sent successfully: ${publishResult.timetoken}")
            }.onFailure { exception ->
                // handle failure
                println("Error sending message: ${exception.message}")
            }
        }
    }.onFailure { exception ->
        // handle failure
        println("Error getting channel: ${exception.message}")
    }
}
```

## Receive

To receive messages on a given channel, you must call [onMessageReceived()](https://www.pubnub.com/docs/chat/kotlin-chat-sdk/build/features/channels/watch) on the channel and start listening to message events.

## Send (deprecated)

Send a [message](https://www.pubnub.com/docs/chat/kotlin-chat-sdk/learn/chat-entities/message) to a previously defined channel using the `sendText()` method.

`sendText()` prepares the final message content for publishing, including text, any attached files, metadata, mentioned users, or referenced channels.

#### Method signature

This method takes the following parameters:

```kotlin
channel.sendText(
    text: String,
    meta: Map<String, Any>?,
    shouldStore: Boolean?,
    usePost: Boolean?,
    ttl: Int?,
    mentionedUsers: MessageMentionedUsers?,
    referencedChannels: Map<Int, MessageReferencedChannel>?,
    textLinks: List<TextLink>?,
    quotedMessage: Message?,
    files: List<InputFile>?
): PNFuture<PNPublishResult>
```

##### Input

| Parameter | Description |
| --- | --- |
| `text` *Type: `String`Default: n/a | Text that you want to send to the selected channel. |
| `meta`Type: `Map<String, Any>`Default: n/a | Publish additional details with the request. |
| `shouldStore`Type: `Boolean`Default: `true` | If `true`, the messages are stored in Message Persistence. |
| `usePost`Type: `Boolean`Default: `false` | When `true`, the SDK uses HTTP POST to publish the messages. The message is sent in the BODY of the request instead of the query string when HTTP GET is used. The messages are also compressed to reduce their size. |
| `ttl`Type: `Int`Default: n/a | Defines if / how long (in hours) the message should be stored in Message Persistence. If shouldStore = true, and ttl = 0, the message is stored with no expiry time., If shouldStore = true and ttl = X, the message is stored with an expiry time of X hours unless you have message retention set to Unlimited on your keyset configuration in the Admin Portal., If shouldStore = false, the ttl parameter is ignored., If ttl is not specified, then the expiration of the message defaults back to the expiry value for the keyset. |
| `mentionedUsers`Type: `MessageMentionedUsers`Default: n/a | Object mapping a mentioned user (with name and ID) with the number of [mention](https://www.pubnub.com/docs/chat/kotlin-chat-sdk/build/features/users/mentions) (like `@Mar`) in the message (relative to other user mentions). For example, `{ 0: { id: 123, name: "Mark" }, 2: { id: 345, name: "Rob" } }` means that `Mark` will be shown on the first mention (`@`) in the message and `Rob` on the third. Refer to [User mentions](https://www.pubnub.com/docs/chat/kotlin-chat-sdk/build/features/users/mentions) for more details and example. |
| `referencedChannels`Type: `Map<Int, MessageReferencedChannel>`Default: n/a | Object mapping the referenced channel (with name and ID) with the place (`Int`) where this reference (like `#Sup`) was mentioned in the message (relative to other channel references). For example, `{ 0: { id: 123, name: "Support" }, 2: { id: 345, name: "Off-topic" } }` means that `Support` will be shown on the first reference in the message and `Off-topic` on the third. Refer to [Reference channels](https://www.pubnub.com/docs/chat/kotlin-chat-sdk/build/features/channels/references) for more details and example. |
| `textLinks`Type: `List<TextLink>`Default: n/a | Returned list of [text links](https://www.pubnub.com/docs/chat/kotlin-chat-sdk/build/features/messages/links) that are shown as text in the message Each `TextLink` contains these fields: `class TextLink(val startIndex: Int, val endIndex: Int, val link: String)` where `startIndex` indicates the position in the whole message where the link should start and `endIndex` where it ends. Note that indexing starts with `0`. Refer to [Links](https://www.pubnub.com/docs/chat/kotlin-chat-sdk/build/features/messages/links) for more details and example. |
| `quotedMessage`Type: `Message`Default: n/a | Object added to a message when you [quote another message](https://www.pubnub.com/docs/chat/kotlin-chat-sdk/build/features/messages/quotes). This object stores the following info about the quoted message: `timetoken` for the time when the quoted message was published, `text` with the original message content, and [userId](https://www.pubnub.com/docs/general/setup/users-and-devices) as the identifier of the user who published the quoted message. Refer to [Quotes](https://www.pubnub.com/docs/chat/kotlin-chat-sdk/build/features/messages/quotes) for more details and example. |
| `files`Type: `List<InputFile>`Default: n/a | One or multiple [files](https://www.pubnub.com/docs/chat/kotlin-chat-sdk/build/features/messages/files#send-files) attached to the text message. Each `InputFile` contains a name, type, and source: `class InputFile(val name: String, val type: String, val source: Uploadable)`. Refer to [Files](https://www.pubnub.com/docs/chat/kotlin-chat-sdk/build/features/messages/files) for more details and example. |

##### Output

| Type | Description |
| --- | --- |
| `PNFuture<PNPublishResult>` | Returned object with a value of any type. |

#### Sample code

Send the `Hi Everyone!` message to the `support` channel. Mark its `high` priority and state that it should be stored in Message Persistence for `15` hours.

```kotlin
chat.getChannel("support").async { result ->
    result.onSuccess { channel ->
        // handle success
        channel.sendText(
            text = "Hi Everyone!",
            meta = mapOf("messageImportance" to "high"),
            shouldStore = true,
            ttl = 15 // 15 hours
        ).async { sendResult ->
            sendResult.onSuccess { publishResult ->
                // handle success
                println("Message sent successfully: ${publishResult.timetoken}")
            }.onFailure { exception ->
                // handle failure
                println("Error sending message: ${exception.message}")
            }
        }
    }.onFailure { exception ->
        // handle failure
        println("Error getting channel: ${exception.message}")
    }
}
```