Extend abstract BaseMessage class to make custom classes

Hi I’m finding it hard to figure out how to extend BaseMessage class to implement our own classes, Just like we have out of the box 3 variants UserMessage, FileMessage and AdminMessage, i want to create my own implementations for the same, the available constructors doesn’t seem to be helpful


Welcome to the community! Could you provide some more information for us? What language/SDK and what version of that SDK are you using?


1 Like

I’m on android, with the latest SDK version

@arjun Is this with UIKit or Core?

it’s with the Core SDK

Thank you for that information. Could you help me understand your use case as to why you’d want to create your own custom message class? That may help me better understand what options are available to you.

I don’t want to use SB’s FileMessage for media like pdf, image, video etc instead i’d like to pass the extra data via the data field in the BaseMessage class and then serialize it into my custom class which extends the BaseMessage for eg.

class ImageMessage() : BaseMessage() {
    val url: String // extracted from mData field from BaseMessage
    val name: String // // extracted from mData field from BaseMessage

// this would be the custom message I'd receive from SB
    "message_survival_seconds": -1,
"custom_type": "audio",
"mentioned_users": [],
"translations": {},
"updated_at": 0,
"is_op_msg": false,
"is_removed": false,
"user": {
    "require_auth_for_profile_image": false,
    "is_active": true,
    "role": "",
    "user_id": "406172",
    "nickname": "Arjun",
    "profile_url": "",
    "metadata": {}
"file": {},
"message": "This is a test message of type IMAGEsend via postman, biatch",
"data": "{\"name\":\"file_example_MP3_5MG.mp3\",\"url\":\"https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_5MG.mp3\",\"size\":\"5MB\",\"thumbnail\":\"https://images.unsplash.com/photo-1615141506772-90b39c01a7c9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=934&q=80\",\"duration\":\"2:12\"}",
"silent": false,
"type": "MESG",
"created_at": 1615369753564,
"mention_type": "users",
"channel_url": "sendbird_group_channel_8751938_f4a3f58aab375c188db6cd73e01604c6700c3ee6",
"message_id": 81124199


@arjun There are fields in Sendbird’s message object which has some restrictions such as not allowing duplicate or empty fields.
If the customer overrides this class, there’s a possibility that they fail to meet the restrictions and this is the main reason we don’t suggest the overrides.
However, if it’s needed in case of UI updates (i.e. to use in message diff), they can use this constructor to create the object only to be used on checking the diff with other messages. (This object will only contain the minimum data needed to check the equality with other message objects)

public BaseMessage(String channelUrl, long msgId, long createdAt) {}

If the purpose of the inheritance is to add other data to the message object, we suggest to set those data on the custom data field of the message, which can be set on sending a message.

public UserMessage sendUserMessage(String message, String data, SendUserMessageHandler handler)
public UserMessage sendUserMessage(UserMessageParams params, final SendUserMessageHandler handler)

I see…thanks for the insight

1 Like