Message Customization
Chat SDK uses ChatKit for the message view so I would recommend that you look at their documentation before trying to add custom message types to Chat SDK. Chat SDK really just exposes the ChatKit custom message API.

Override Text Message Display

In this example, we will override the outcoming text message so that we can append "Hello World!" to every text message.
To do this, the first step is to make a new class that extends the OutcomingTextMessageViewHolder.
1
public class CustomOutcomingTextMessageViewHolder extends OutcomingTextMessageViewHolder {
2
public CustomOutcomingTextMessageViewHolder(View itemView, Object payload) {
3
super(itemView, payload);
4
}
5
​
6
@Override
7
public void onBind(MessageHolder message) {
8
super.onBind(message);
9
text.setText(text.getText() + " Hello World");
10
}
11
​
12
}
Copied!
The onBind method is what binds the MessageHolder data model to the MesageViewHolder.
We then need to make a new subclass of the TextMessageHandler class so that we can register the new view holder with Chat SDK.
1
public class CustomTextMessageHandler extends TextMessageHandler {
2
​
3
@Override
4
public void onBindMessageHolders(Context context, MessageHolders holders) {
5
holders.setIncomingTextConfig(IncomingTextMessageViewHolder.class, R.layout.view_holder_incoming_text_message, getAvatarClickPayload(context))
6
.setOutcomingTextConfig(CustomOutcomingTextMessageViewHolder.class, R.layout.view_holder_outcoming_text_message, getAvatarClickPayload(context));
7
}
8
​
9
}
Copied!
This is where we are registering the view holder with ChatKit. We can also specify custom layout files.
Finally, we need to tell Chat SDK to use this message handler:
1
ChatSDKUI.shared().getMessageCustomizer().addMessageHandler(new CustomTextMessageHandler());
Copied!

Creating a Custom Message Type

Chat SDK supports completely custom message types. To do this, we need to setup a number of classes.
1
public class ExampleSnapMessageHandler extends CustomMessageHandler {
2
​
3
public static int SnapMessageType = 919;
4
​
5
@Override
6
public List<Byte> getTypes() {
7
return types(SnapMessageType);
8
}
9
​
10
@Override
11
public boolean hasContentFor(MessageHolder holder) {
12
return holder.getClass().equals(SnapMessageHolder.class);
13
}
14
​
15
@Override
16
public void onBindMessageHolders(Context context, MessageHolders holders) {
17
holders.registerContentType(
18
(byte) SnapMessageType,
19
IncomingSnapMessageViewHolder.class,
20
sdk.chat.ui.R.layout.view_holder_incoming_image_message,
21
OutcomingSnapMessageViewHolder.class,
22
sdk.chat.ui.R.layout.view_holder_outcoming_image_message,
23
ChatSDKUI.shared().getMessageCustomizer());
24
}
25
}
Copied!
This is an example of a custom message type.
    getTypes() - Which message types we support
    hasContentFor - Is this view holder of the correct type?
    onBindMessageHolders Here we register our custom types with ChatKit. We can define outcoming and incoming view holders and also define custom XML files.
1
public class SnapMessageHolder extends MessageHolder implements MessageContentType {
2
​
3
public String customValue;
4
​
5
public SnapMessageHolder(Message message) {
6
super(message);
7
customValue = (String) message.valueForKey("Key");
8
}
9
}
Copied!
The message holder exposes properties from the message to the view holders.
1
public class IncomingSnapMessageViewHolder extends BaseIncomingTextMessageViewHolder<SnapMessageHolder> {
2
​
3
TextView username;
4
​
5
public IncomingSnapMessageViewHolder(View itemView, Object payload) {
6
super(itemView, payload);
7
username = itemView.findViewById(R.id.userName);
8
}
9
​
10
@Override
11
public void onBind(SnapMessageHolder message) {
12
super.onBind(message);
13
​
14
String value = message.customValue;
15
username.setText(value);
16
​
17
}
18
}
Copied!
The view holder binds the message holder to the view. It has access to the view and can get properties from the XML layout file using itemView.findViewById().
The final step would be to send the custom message.
1
Disposable d = MessageSendRig.create(new MessageType(ExampleSnapMessageHandler.SnapMessageType), thread, message -> {
2
// Set custom Data here
3
message.setValueForKey("Value", "Key");
4
}).run().subscribe(() -> {
5
// Success
6
}, throwable -> {
7
// Failure
8
});
Copied!
For another real world example you can look ad the ImageMessageHandler.

Image Message Handler

Chat SDK supports completely custom message types. To do this, we need to extend the CustomMessageHandler class. We can look at the image message handler as an example:
1
public class ImageMessageHandler extends CustomMessageHandler {
2
​
3
@Override
4
public List<Byte> getTypes() {
5
return types(MessageType.Image, MessageType.Location);
6
}
7
​
8
@Override
9
public boolean hasContentFor(MessageHolder holder) {
10
return holder.getClass().equals(ImageMessageHolder.class);
11
}
12
​
13
@Override
14
public void onBindMessageHolders(Context context, MessageHolders holders) {
15
holders.registerContentType(
16
(byte) MessageType.Image,
17
IncomingImageMessageViewHolder.class,
18
getAvatarClickPayload(context),
19
R.layout.view_holder_incoming_image_message,
20
OutcomingImageMessageViewHolder.class,
21
getAvatarClickPayload(context),
22
R.layout.view_holder_outcoming_image_message,
23
ChatSDKUI.shared().getMessageCustomizer());
24
}
25
​
26
@Override
27
public void onClick(ChatActivity activity, View rootView, Message message) {
28
if (message.getSender().isMe() && message.getMessageStatus() == MessageSendStatus.Failed) {
29
super.onClick(activity, rootView, message);
30
} else if (message.typeIs(MessageType.Image)) {
31
ImageMessageOnClickHandler.onClick(activity, rootView, message.stringForKey(Keys.ImageUrl));
32
}
33
else if (message.typeIs(MessageType.Location)) {
34
double longitude = message.doubleForKey(Keys.MessageLongitude);
35
double latitude = message.doubleForKey(Keys.MessageLatitude);
36
​
37
Location location = new Location(ChatSDK.getString(R.string.app_name));
38
location.setLatitude(latitude);
39
location.setLongitude(longitude);
40
​
41
LocationMessageOnClickHandler.onClick(activity, location);
42
}
43
}
44
​
45
@Override
46
public MessageHolder onNewMessageHolder(Message message) {
47
if (message.getMessageType().is(MessageType.Image, MessageType.Location)) {
48
return new ImageMessageHolder(message);
49
}
50
return null;
51
}
52
​
53
}
Copied!
    The onBindMessageHolder allows us to register our content type with ChatKit
    The onClick method is called if the user clicks the message
    The onNewMessageHolder method is called to provide a new instance of the message holder
    The hasContentFor method lets the system know when this custom message type should be used
To register this with the framework, you can use the following:
1
ChatSDKUI.shared().getMessageCustomizer().addMessageHandler(new ImageMessageHandler());
Copied!

Summary

This may seem complicated, if that's the case, I would recommend thoroughly reviewing the ChatKit documentation. This guide just explains how to access the ChatKit API from within Chat SDK. It doesn't go through the ChatSDK API in detail.
Last modified 8mo ago