Webex Messaging
Features and Methods
This article provides detailed information on the messaging features and methods provided by the Webex Android Messaging SDK.
The SDK lets you easily implement real-time messaging capabilities including posting new messages, and deleting, listing, and editing existing messages.
anchorBefore Continuing: Sync Webex Spaces
anchorBefore working with spaces or messages within them, ensure that all spaces are synced using the following method:
// Indicates if the latest conversations have been synced to the local data warehouse.
// Returns true if the sync is completed.
// Returns false if the sync is in progress.
webex.spaces.isSpacesSyncCompleted
// The callback handler for sync status changes.
webex.spaces.setOnSpaceSyncingStatusChangedListener( CompletionHandler { result->
if(result.isSuccessful){
// result.data provides the sync status.
if(result.data){
// result.data is true if a spaces sync is in progress.
}
else{
// result.data is false if a spaces sync is complete.
}
}
}
anchorGet a Message by ID
anchorRetrieve a specific message using its ID:
webex.messages.get(messageId, CompletionHandler { result ->
if (result.isSuccessful) {
// result.data gives the message object
} else {
// result.error?.errorMessage gives error message
}
})
anchorSend a Text Message
anchorSend a text messages to a person via ID or email, or to a space:
webex.messages.postToPerson(personId, message, null, CompletionHandler { result ->
if (result.isSuccessful) {
// Success. result.data contains the Message object
} else {
// Error occurred. result.error?.errorMessage provides the error message
}
})
val emailAddress = EmailAddress.fromString(email) // email is the Email address in string format
webex.messages.postToPerson(emailAddress, message, files, CompletionHandler { result ->
if (result.isSuccessful) {
// Success. result.data contains the Message object
} else {
// Error occurred. result.error?.errorMessage provides the error message
}
})
val text: Message.Text = Message.Text.plain("some_message")
webex.messages.postToSpace(spaceId, text, null, null, CompletionHandler { result ->
if (result.isSuccessful) {
// Success. result.data contains the Message object
} else {
// Error occurred. result.error?.errorMessage provides the error message
}
})
anchorPost a Message to a Space or a Person using a Draft object
anchorThe target
is the destination to which the message is being sent and can be a person ID, space ID, or email address. The draft
is of type [Message.Draft](https://webex.github.io/webex-android-sdk/-s-d-k/com.ciscowebex.androidsdk.message/-message/-draft/index.html?query=interface Draft).
To post a message to a space or person using a draft object:
webex.messages.post(target, draft, CompletionHandler { result ->
if (result.isSuccessful) {
// result.data contains the message object
} else {
// result.error?.errorMessage gives the error message
}
})
anchorPost Mentions
anchorTo post a message mentioning a particular person using a person ID:
// Mention individuals
val mentionsList = ArrayList<Mention>()
mentionsList.add(Mention.Person("personId1", startIndex, endIndex))
mentionsList.add(Mention.Person("personId2", startIndex, endIndex))
webex.messages.postToSpace(spaceId, text, mentionsList, files, completionHandler)
To post a mention to a group:
// Mention groups
val mentionsList = ArrayList<Mention>()
mentionsList.add(Mention.All())
webex.messages.postToSpace(spaceId, text, mentionsList, files, completionHandler)
The startIndex
and endIndex
are the index positions in the text message which will indicate the mention object. For example, to mention XYZ in the text Hello XYZ
, the index position would be 6 and 8, where 6 is the startIndex
and 8 is the endIndex
.
anchorPost Files and Track Upload Progress
anchorTo attach files to messages:
// Attach files to a message
val text = Message.Text.plain("hello world")
// Create a File object
val tempFile = File.createTempFile("tempFile", ".txt")
// Create a local file object
val localFile = LocalFile(tempFile, "text/html", null, null)
val listOfFiles = ArrayList<LocalFile>()
listOfFiles.add(localFile)
webex.messages.postToSpace(spaceId, text, mentionsList, listOfFiles, completionHandler)
To track the file upload progress:
// Track upload progress
val text = Message.Text.plain("hello world")
// Create a File object
val tempFile = File.createTempFile("tempFile", ".txt")
// Create a local file object
val localFile = LocalFile(tempFile, "text/html", null, object : MessageClient.ProgressHandler {
override fun onProgress(bytes: Double) {
// Use the bytes value to denote quantity uploaded
}
})
val listOfFiles = ArrayList<LocalFile>()
listOfFiles.add(localFile)
webex.messages.postToSpace(spaceId, text, mentionsList, listOfFiles, completionHandler)
anchorAttach Files with Thumbnails
anchorTo attach a file with a thumbnail:
// Attach files with a thumbnail
// Create a File object
val file = File.createTempFile("tempFile", ".txt")
// Create a thumbnail
var thumbnail = LocalFile.Thumbnail(file, null, 320, 400)
// Create a local file object
val localFile = LocalFile(file, "text/html", thumbnail, object : MessageClient.ProgressHandler {
override fun onProgress(bytes: Double) {
// Use the bytes value to denote quantity uploaded
}
})
val listOfFiles = ArrayList<LocalFile>()
listOfFiles.add(localFile)
webex.messages.postToSpace(spaceId, text, mentionsList, listOfFiles, completionHandler)
anchorEdit a Message
anchorTo modify the content of an existing message:
webex.messages.edit(messageObject, text, mentions, CompletionHandler { result ->
if (result.isSuccessful) {
// Edit success
val newMessageObject = result.data
} else {
// Error occurred. result.error?.errorMessage provides the error message
}
})
anchorDelete a Message
anchorTo delete a message using it message ID:
webex.messages.delete(messageId, CompletionHandler { result ->
if (result.isSuccessful) {
// Message deleted successfully
} else {
// Error occurred. result.error?.errorMessage provides the error message
}
})
anchorList Messages Before a Specific Date
anchorTo retrieve messages in a space before a given date:
val before = Before.Date(date)
webex.messages.list(spaceId, before, max, mentions, { result ->
if (result.isSuccessful) {
// result.data contains the messages
} else {
// Error occurred. result.error?.errorMessage provides the error message
}
})
Specify the maximum number of messages to return using max
.
anchorList Messages Before a Specific Message
anchorTo retrieve messages in a space preceding a certain message ID:
val before = Before.Message("messageId")
webex.messages.list(spaceId, before, max, mentions, { result ->
if (result.isSuccessful) {
// result.data contains the messages
} else {
// Error occurred. result.error?.errorMessage provides the error message
}
})
Specify the maximum number of messages to return using max
.
anchorList Messages Mentioning a Specific Person
anchorTo list messages mentioning a particular person, identified by personId
, in a space:
val mentionsList : ArrayList<Mention> = ArrayList()
mentionsList.add(Mention.Person(personIdOfSomePersonInSpace))
mentionsList.add(Mention.Person(personIdOfAnotherPersonInSpace))
webex.messages.list(spaceId, null, max, mentionsList, { result ->
if (result.isSuccessful) {
// result.data contains the messages
} else {
// Error occurred. result.error?.errorMessage provides the error message
}
})
Specify the maximum number of messages to return using max
.
anchorSet Up a Message Observer
anchorTo set up a message observer to receive messaging events:
webex.messages.setMessageObserver(object : MessageObserver {
override fun onEvent(event: MessageObserver.MessageEvent) {
when (event) {
is MessageObserver.MessageReceived -> {
// event.getMessage() gives the Message object
}
is MessageObserver.MessageDeleted -> {
...
}
is MessageObserver.MessageFileThumbnailsUpdated -> {
...
}
is MessageObserver.MessageEdited -> {
...
}
is MessageObserver.MessagesUpdated -> {
...
}
}
}
})
anchorDownload Files from a Message
anchorDownload files included in a message to a specified local directory where file
includes the directory and file name:
webex.messages.downloadFile(remoteFile, file,
object : MessageClient.ProgressHandler {
override fun onProgress(bytes: Double) {
}
},
CompletionHandler { fileUrlResult ->
if (fileUrlResult.isSuccessful) {
// Download successful. fileUrlResult.data gives the file URI
} else {
// Error occurred. fileUrlResult.error?.errorMessage gives the error message
}
}
)
The remoteFile
is the target file and is of type [Remote File](https://webex.github.io/webex-android-sdk/-s-d-k/com.ciscowebex.androidsdk.message/-remote-file/index.html?query=interface RemoteFile).
anchorDownload Thumbnail Images
anchorTo download thumbnail images associated with a RemoteFile
object:
file
is the local file directory in which the thumbnail is to be saved.
webex.messages.downloadThumbnail(remoteFile, file, CompletionHandler { result ->
if (result.isSuccessful) {
if (result.data != null) {
// result.data gives the thumbnail URI
} else {
// Error occurred
}
} else {
emitter.onError(Throwable(result.error?.errorMessage))
}
})
The remoteFile
is the target file and is of type [Remote File](https://webex.github.io/webex-android-sdk/-s-d-k/com.ciscowebex.androidsdk.message/-remote-file/index.html?query=interface RemoteFile).
anchorMark a Specific Message as Read
anchorTo mark all messages sent before and including the specified messageId
in a space as read:
webex.messages.markAsRead(spaceId, messageId, CompletionHandler { result ->
if (result.isSuccessful) {
// Message is marked as read
} else {
// result.error?.errorMessage gives the error message
}
})
anchorMark All Messages as Read
anchorTo mark ALL the messages in a space as read:
webex.messages.markAsRead(spaceId)
anchorRetrieve the Last Read Message Status
anchorTo obtain the last activity and read status in a space:
webex.spaces.getWithReadStatus(spaceId, CompletionHandler { result ->
if (result.isSuccessful) {
//show the data
} else {
//handle error
}
})
Check for 'All' Mentions in a Message
To determine if a message mentions all participants in a space:
message.isAllMentioned()
Retrieve a List of Mentioned People in a Message
To retrieve a list of people mentioned in a message:
var mentions = message.getMentions()