Skip to main content

Typing Status

Applications can send the current user's typing status in one-to-one chat sessions. Upon receiving the notification, the peer can display "xxx is typing" in the UI.

Sending Typing Status Messages

Call sendTypingStatus when the current user is entering text to send the typing status.

The IMLib SDK internally enforces that the same user can only send one typing status message to the same conversation within 6 seconds. The processing interval for receiving typing status messages in the same conversation is 6 seconds by default. This configuration cannot be modified currently.

Interface

RongCoreClient.getInstance().sendTypingStatus(conversationType, targetId, typingContentType);

Parameters

ParameterTypeDescription
conversationTypeConversationTypeConversation type. This interface only supports one-to-one chat.
targetIdStringConversation ID.
typingContentTypeStringThe type name of the message being entered. For text messages, pass the type name "RC:TxtMsg".

Sample Code

 ConversationType conversationType = ConversationType.PRIVATE;
String targetId = "Conversation ID";
String typingContentType = "RC:TxtMsg";
RongCoreClient.getInstance()
.sendTypingStatus(conversationType, targetId, typingContentType);

Monitoring Typing Status

Applications can use setTypingStatusListener to set up a listener for the peer's typing status, which monitors typing status notifications received in one-to-one chat sessions. When a typing status notification is received from the peer, the SDK returns the list of currently typing users and message types through the onTypingStatusChanged callback.

Sample Code

 RongCoreClient.getInstance().setTypingStatusListener(new IRongCoreListener.TypingStatusListener() {
@Override
public void onTypingStatusChanged(Conversation.ConversationType type, String targetId, Collection<TypingStatus> typingStatusSet) {
//Only display when the typing status conversation type and targetID match the current conversation
if (type.equals(mConversationType) && targetId.equals(mTargetId)) {
// count represents the number of users currently typing in the conversation (currently only supports one-to-one chat)
int count = typingStatusSet.size();
if (count > 0) {
Iterator iterator = typingStatusSet.iterator();
TypingStatus status = (TypingStatus) iterator.next();
String objectName = status.getTypingContentType();

MessageTag textTag = TextMessage.class.getAnnotation(MessageTag.class);
MessageTag voiceTag = VoiceMessage.class.getAnnotation(MessageTag.class);
//Determine whether the peer is typing text or voice messages
if (objectName.equals(textTag.value())) {
//Display "Peer is typing"
mHandler.sendEmptyMessage(SET_TEXT_TYPING_TITLE);
} else if (objectName.equals(voiceTag.value())) {
//Display "Peer is speaking"
mHandler.sendEmptyMessage(SET_VOICE_TYPING_TITLE);
}
} else {
//No users are currently typing in the conversation, keep the original title
mHandler.sendEmptyMessage(SET_TARGETID_TITLE);
}
}
}
});