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 our 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 our 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 automatically synchronises local app data with the server when the app becomes foregrounded or the socket reconnected. However, we recommend that you call below method when the screen with a list of conversations becomes visible, or when a user triggers a refresh, for example, by using swipe to refresh the 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 a particular conversation:

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

or

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

Add participants to a conversation

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

Remove participants from a conversation

Provide a list of profile IDs to be removed from a 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 additional parts, FCM and APNS push notifications, or 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 */});

Send 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

The chat SDK implements a logic to obtain the next page of messages starting from the latest messages and working backwards.

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

Send user is typing event

To send notification to conversation participants that the user started or finished typing a new message:

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