> ## Documentation Index
> Fetch the complete documentation index at: https://social-b97141fb-feat-user-history-moderation-detail.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Message Preview

> Read the latest message preview from chat channel and subchannel SDK objects.

Use message preview when your app needs a lightweight latest-message summary for a chat list, inbox row, or notification handoff. The SDK exposes preview data on channel and subchannel models after message preview is enabled for the network.

> [!NOTE]
>
> Message preview is configured outside these client SDK calls, typically through network settings. Client SDKs read the preview that is returned with channel or subchannel data; they do not enable the setting themselves.

## Platform Surface

| Surface | TypeScript | iOS | Android | Flutter |
| - | - | - | - | - |
| Channel preview | `channel.messagePreview` | `channel.messagePreview` | `channel.getMessagePreview()` | `channel.messagePreview` |
| Subchannel preview | `subChannel.messagePreview` | `subChannel.messagePreview` | `subChannel.getMessagePreview()` | Not exposed as a public preview object |
| Preview ID | `messagePreviewId` | `messagePreviewId` | `getMessagePreviewId()` | `messagePreviewId` |
| Preview data | `data`, `dataType` | `data`, `dataType` | `getData()`, `getDataType()` | `data`, `dataType` |

## Parameters

| Parameter | Required | Description |
| - | - | - |
| `channelId` | Yes | Channel ID used to retrieve or observe the channel that contains the preview. |
| `subChannelId` | Yes for subchannel preview | Subchannel ID used to retrieve or observe a subchannel preview where the platform exposes it. |
| Preview setting | Yes | The network must have message preview enabled before new messages produce preview data. |
| `messagePreview` null handling | Yes | A channel or subchannel can return no preview when no eligible message exists, preview is disabled, or the preview has not been cached yet. |

## Read Channel Preview

Read the preview from the channel object you already use for channel lists. Treat the preview as optional and render a fallback for empty channels.

```typescript TypeScript theme={null}
import { ChannelRepository } from '@amityco/ts-sdk';

const unsubscribe = ChannelRepository.getChannel(
  channelId,
  ({ data: channelSnapshot, loading, error }) => {
    if (error) {
      handleError(error);
      return;
    }

    if (!loading && channelSnapshot?.messagePreview) {
      renderResults({
        id: channelSnapshot.messagePreview.messagePreviewId,
        dataType: channelSnapshot.messagePreview.dataType,
        data: channelSnapshot.messagePreview.data,
        userId: channelSnapshot.messagePreview.user?.userId,
      });
    }
  },
);

unsubscribe();
```

```swift iOS theme={null}
token = channelRepository.getChannel(channelId).observe { liveObject, error in
    if let error {
        handleError(error)
        return
    }

    guard let preview = liveObject.snapshot?.messagePreview else { return }

    showSuccessMessage([
        "id": preview.messagePreviewId,
        "type": preview.dataType,
        "subChannelId": preview.subChannelId
    ])
}
```

```kotlin Android theme={null}
val currentChannel = channel ?: return
val preview = currentChannel.getMessagePreview() ?: return

showSuccessMessage(
    mapOf(
        "id" to preview.getMessagePreviewId(),
        "type" to preview.getDataType(),
        "subChannelId" to preview.getSubChannelId(),
    ),
)
```

```dart Flutter theme={null}
final fetchedChannel = await AmityChatClient.newChannelRepository()
    .getChannel(channelId);

final preview = fetchedChannel.messagePreview;

if (preview != null) {
  final previewId = preview.messagePreviewId;
  final previewType = preview.dataType;
  final previewData = preview.data;
}
```

## Read Subchannel Preview

Use subchannel preview when your UI lists subchannels directly. Flutter currently exposes `messagePreviewId` on `AmitySubChannel`, but not the composed `AmityMessagePreview` object.

```typescript TypeScript theme={null}
import { SubChannelRepository } from '@amityco/ts-sdk';

const unsubscribe = SubChannelRepository.getSubChannel(
  subChannelId,
  ({ data: subChannel, loading, error }) => {
    if (error) {
      handleError(error);
      return;
    }

    if (!loading && subChannel?.messagePreview) {
      renderResults(subChannel.messagePreview.messagePreviewId);
    }
  },
);

unsubscribe();
```

```swift iOS theme={null}
let subChannelRepository = AmitySubChannelRepository()

token = subChannelRepository.getSubChannel(withId: "sub-channel-id").observe { liveObject, error in
    if let error {
        handleError(error)
        return
    }

    guard let preview = liveObject.snapshot?.messagePreview else { return }
    showSuccessMessage(preview.messagePreviewId)
}
```

```kotlin Android theme={null}
val subChannelRepository = AmityChatClient.newSubChannelRepository()

val disposable = subChannelRepository
    .getSubChannel(subChannelId)
    .subscribe(
        { subChannel ->
            val preview = subChannel.getMessagePreview()
            showSuccessMessage(preview?.getMessagePreviewId() ?: "")
        },
        { error -> handleGeneralError(error) },
    )
```

## Preview Fields

| Field | Description |
| - | - |
| `messagePreviewId` | ID of the message represented by the preview. |
| `dataType` | Message data type, such as text, image, file, video, audio, or custom. |
| `data` | Preview payload for the message data type. For text messages, this contains the text payload. |
| `channelId` | Channel that owns the previewed message. |
| `subChannelId` | Subchannel that owns the previewed message. |
| `subChannelName` | Subchannel display name where the platform exposes it. |
| `isDeleted` | Whether the previewed message is deleted. Availability depends on the network preview setting. |
| `user` | User object for the previewed message creator when the SDK has the user data. |

## Related Topics

> **[Query Channels](/social-plus-sdk/chat/conversation-management/channels/query-channels)**
>
> Load channel lists that include preview data.

> **[Query Messages](/social-plus-sdk/chat/messaging-features/messages/query-and-filter-messages)**
>
> Load full message timelines when preview data is not enough.

> **[Channel Unread Count](/social-plus-sdk/chat/engagement-features/unread-status/channel-unread-count)**
>
> Combine previews with unread count and mention indicators.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.