Messaging service

You can choose to access two method versions - with callbacks and reactive.

// service which methods will accept callbacks to deliver result, the request will be performed in the background delivering result in UI thread.
service = client.service().messaging();
// service which methods will return observables you need to subscribe to
service = client.rxService().messaging();

messaging service API

create conversation

ConversationCreate request = ConversationCreate.builder()
   // Unique conversation identifier.
   .setId("1234")
   // Description
   .setDescription("This is my first conversation")
   // Name 
   .setName("Awesome chat")
   // Is this conversation visible to users not being participants.
   .setPublic(false)
   /* Sets what permissions 'owner' and 'participant' will have in this conversation
      You can set if they will can: add new participants, remove participants, send messages.
      By default 'owner' and 'participant' can send messages, can add participants, cannot remove participants.
      Here we added CanRemoveParticipants permission for the 'owner'. */
   .setRoles(new Roles(Role.builder().setCanRemoveParticipants().build(), new Role()))
   .build();

Then pass this object to Comapi services

service.createConversation(request,
   new Callback<ComapiResult<ChatResult>>(){/* implement */});

or

rxService.createConversation(conversation)
   .subscribe(new Observer<ComapiResult<ChatResult>>(){/* implement */});

update conversation

ConversationUpdate update = ConversationUpdate.builder()
                .setDescription("New description")
                .setName("Different name")
                .setRoles(roles)
                .setPublic(false)
                .build();

Then pass this object to Comapi services

service.updateConversation(conversationId, update, eTag,
   new Callback<ComapiResult<ChatResult>>(){/* implement */});

or

rxService.updateConversation(conversationId, update, eTag)
   .subscribe(new Observer<ComapiResult<ChatResult>>(){/* implement */});

delete conversation

service.deleteConversation(conversationId, eTag,
   new Callback<ComapiResult<Void>>(){/* implement */});

or

rxService.deleteConversation(conversationId, eTag)
   .subscribe(new Observer<ComapiResult<Void>>(){/* implement */});

synchronise store

The SDK will automatically synchronise local app data with the server when the app becomes foregrounded or socket reconnected. However we recommend to call below method when screen with list of conversations becomes visible or when user triggers refresh (e.g. by using swipe to refresh widget).

service.synchroniseStore(new Callback<Boolean>(){/* implement */});

or

rxService.synchroniseStore()
   .subscribe(new Observer<Boolean>(){/* implement */});

You can also synchronise the local state of a single conversation

service.synchroniseConversation(conversationId, new Callback<Boolean>(){/* implement */});

or

rxService.synchroniseConversation(conversationId)
   .subscribe(new Observer<Boolean>(){/* implement */});

conversation participants

Get all participants for particular conversation

service.getParticipants(conversationId, 
   new Callback<List<ChatParticipant>>(){/* implement */});

or

rxService.getParticipants(conversationId)
   .subscribe(new Observer<List<ChatParticipant>>(){/* implement */});

add participants to conversation

Add new participants to conversation

service.addParticipants(conversationId, participants,
   new Callback<ChatResult>(){/* implement */});
rxService.addParticipants(conversationId, participants)
   .subscribe(new Observer<ChatResult>(){/* implement */});

remove participants from conversation

Provide list of profile ids to be removed from conversation.

service.removeParticipants(conversationId, ids,
   new Callback<ChatResult>(){/* implement */});

or

rxService.removeParticipants(conversationId, ids)
   .subscribe(new Observer<ChatResult>(){/* implement */});

send message in conversation

service.sendMessage(conversationId, body,
   new Callback<ChatResult>(){/* implement */});
rxService.sendMessage(conversationId, body)
   .subscribe(new Observer<ChatResult>(){/* implement */});

you can also send messages with more parts, FCM/APNS push notifications, custom metadata:

Map<String, Object> data = new HashMap<>();
data.put("key","value");

Map<String, Object> fcm = new HashMap<>();
fcm.put("data", data);
fcm.put("notification", "{ \"title\":\"Message\", \"body\":\"Hi!\" } ");

Map<String, Object> apns = new HashMap<>();
apns.put("alert", "Hi!");

Part part = Part.builder()
               .setData("Hi")
               .setName("body")
               .setSize("Hi".length())
               .setType("text/plain")
               .build();

MessageToSend message = MessageToSend.builder()
   .setAlert(fcm, apns)
   .setMetadata(data)
   .addPart(part)
   .build();

Then call

service.sendMessage(conversationId, message, 
   new Callback<ComapiResult<MessageSentResponse>>(){/* implement */});

or

rxService.sendMessage(conversationId, message)
   .subscribe(new Observer<ComapiResult<MessageSentResponse>>(){/* implement */});

Sending message with attachments

service.sendMessage(conversationId, message, attachents,
   new Callback<ComapiResult<MessageSentResponse>>(){/* implement */});
rxService.sendMessage(conversationId, message, attachemnts)
   .subscribe(new Observer<ComapiResult<MessageSentResponse>>(){/* implement */});

where attachments can be created by one of the static constructors

Attachment.create(file, type, folder, name);
Attachment.create(byteArray, type, folder, name);
Attachment.create(base64string, type, folder, name);

update message status

You can mark the message as being read by the user, this information will be send to all participants of the coversation

service.markMessagesAsRead(conversationId, messageIds, 
   new Callback<ChatResult>(){/* implement */});

or

rxService.markMessagesAsRead(conversationId, messageIds)
   .subscribe(new Observer<ChatResult>(){/* implement */});

get message page

Chat SDK implents a logic to obtain the next page of messages stoarting from the latest messages and going backwards

service.getPreviousMessages(conversationId,
   new Callback<ChatResult>(){/* implement */});
rxService..getPreviousMessages(conversationId)
   .subscribe(new Observer<ChatResult>(){
      /* implement */});

send user is typing event

To send an information to conversation participants that the user started or finished typing a new message call

service.isTyping(conversationId, isTyping, 
   new Callback<ComapiResult<Void>>(){/* implement */});
rxService.isTyping(conversationId, isTyping)
   .subscribe(new Observer<ComapiResult<Void>>(){/* implement */});