Messages and the Android SDK

Sending a Message

There are several ways to send a message. The following snippet is one way to send a message.

public void sendMessage() {
Message messageForSend = Message.messageForSend("Hello World!", 0,
rosterEntry, organizationTTL, null, null, deleteOnRead);
TT.getInstance().getConversationManager().sendMessage(messageForSend);
}

RosterEntry is the person/group you are trying to send the message to, TTL is the time-to-live for the message, usually this time is specified by an organization setting.

Sending an Attachment

Sending an attachment is very similar to sending a normal message, the difference is that a File object is required along with a mimeType of the attachment that you want to send.

public void sendAttachment() {
Message attachmentForSend = Message.messageForSend("Hello World!", 0,
rosterEntry, organizationTTL, photoFile, AttachmentMimeTypes.TYPE_JPG, deleteOnRead);
TT.getInstance().getConversationManager().sendAttachment(attachmentForSend, shouldDeleteAttachment);
}

Resending a Message

private void resendMessage(Message message) {
// Notify our Conversation Manager that this message needs to be resent
TT.getInstance().getConversationManager().resendMessage(message.getMessageId());
conversationAdapter.addMessage(message);
scrollToBottom();
}

Recalling a Message

private void recallMessage(Message message) {
// Notify our Conversation Manager that this message needs to be recalled
TT.getInstance().getConversationManager().recallMessage(message.getMessageId());
}

Forwarding a Message

private void forwardMessage(Message message) {
// Notify our Conversation Manager that this message is to be forwarded
TT.getInstance().getConversationManager().forwardMessage(message.getMessageId(), message);
}

Autoforwarding

private void autoForwardToThisUser(RosterEntry rosterEntry) {
Map<SettingType, Object> modifiedSettingsMap = new ArrayMap<>(1);
modifiedSettingsMap.put(SettingType.DND_AUTO_FORWARD_RECEIVER, rosterEntry.getId());
// Call this method to notify our Organization Manager to modify the organization settings
// and set this user as the recipient of our auto forwarded messages
TT.getInstance().getOrganizationManager().modifyOrganizationSettings(mRosterEntry.getOrgId(), modifiedSettingsMap, null);
}

Receiving Updates for Messages

In your Conversation View, you would want to subscribe to Message updates like MESSAGE_ADDED, MESSAGE_UPDATED, MESSAGES_REMOVED, etc.

@Override
public void onEventReceived(@NonNull String event, @Nullable Object o) {
if (getActivity() == null) {
return;
}
switch (event) {
case TTEvent.MESSAGE_ADDED: {
/**
* It may also be useful to check if the fragment is alive as well!
*/
// Confirm that this new message is for this conversation and organization,
// if not, return early
if (!isEventForRosterEntryOrg(o)) return;
Timber.d("Message Added");
Message message = (Message) o;
/**
onEventReceived is done on a background thread, so make sure to publish your logic
on the UI thread
*/
getActivity().runOnUiThread(() -> {
if (getUserVisibleHint()) {
// This is how you mark a conversation as read
TT.getInstance().getConversationManager().markConversationAsRead(mRosterEntry);
}
conversationAdapter.addMessage(message);
scrollToBottom();
});
}
break;
case TTEvent.MESSAGE_UPDATED: {
if (!isEventForRosterEntryOrg(o)) return;
Timber.d("Message Updated");
}
break;
case TTEvent.MESSAGES_REMOVED: {
Timber.d("Messages Removed");
List<Message> messages = (List<Message>) o;
if (getActivity() == null) return;
getActivity().runOnUiThread(() -> {
conversationAdapter.removeMessages(messages);
});
}
break;
case TTEvent.MESSAGE_STATUS_UPDATED: {
// This is where to handle message status updates like Read, Delivered
Timber.d("Message Status Received!");
Message message = (Message) o;
if (!isEventForRosterEntryOrg(message)) return;
getActivity().runOnUiThread(() -> {
conversationAdapter.markMessageAsRead(m);
});
}
break;
}
}