val attachment = Attachment(...)
val message = Message(
cid = "messaging:general",
text = "Look at this attachment!",
attachments = mutableListOf(attachment),
)
ChatClient.instance().sendMessage(channelType = "messaging", channelId = "general", message = message).enqueue { result ->
if (result.isSuccess) {
// Use result.data()
} else {
// Handle result.error()
}
}
Sending Custom Attachments
The Stream Chat SDK has support for some built-in attachment types like images, videos, and files. However, you can also create your custom attachments that have their own type and contain arbitrary data. You can use this to add location info, custom media like audio, product details, or whatever other content your app deals with to a message.
In this guide, we’ll look at how to create custom attachments and send them to a channel.
To render custom attachments with the Compose SDK, see the Custom Attachments page.
Sending an attachment requires you to create the Attachment
object, add it to a message, and then send that message:
Attachment attachment = new Attachment();
Message message = new Message();
message.setCid("messaging:general");
message.setText("Look at this attachment!");
message.setAttachments(Collections.singletonList(attachment));
ChatClient.instance().sendMessage("messaging", "general", message).enqueue(result -> {
if (result.isSuccess()) {
// Use result.data()
} else {
// Handle result.error()
}
}
);
Let’s see how you can create an Attachment
.
Create an Attachment Without Files
If your attachment is just plain data (for example, location coordinates), and requires no file to be transferred, you can create and send it in the following way:
val attachment = Attachment(
type = "location", // 1
extraData = mutableMapOf( // 2
"lat" to 40.017985,
"lon" to -105.280184,
),
)
Attachment attachment = new Attachment();
// 1
attachment.setType("location");
// 2
Map<String, Object> extraData = new HashMap<>();
extraData.put("lat", 40.017985);
extraData.put("lon", -105.280184);
attachment.setExtraData(extraData);
- Setting a custom value for the
type
of the attachment makes it easy to identify the custom attachment on the receiving side to render it in the Message List. - The
extraData
Map allows you to add arbitrary pieces of data to the attachment. In this example, we use this to add a pair of location coordinates.
Create an Attachment With Files
If you want to upload a file for your attachment, you need to create an Attachment
object that has its upload
property set:
val attachment = Attachment(
type = "audio", // 1
upload = File("audio-file.mp3"), // 2
)
Attachment attachment = new Attachment();
// 1
attachment.setType("audio");
// 2
attachment.setUpload(new File("audio-file.mp3"));
- Again, a custom
type
allows you to identify your custom attachments to render them in the Message List. - The file in the
upload
property will be uploaded automatically before the message is sent. The URL where it’s available from will be placed in theurl
property of theAttachment
.
By default, files are uploaded to Stream’s CDN, which has a 100 MB size limit. However, you can also use your own CDN for files.