Single chat

Prerequisites#

In order to send messages using the chat sdk , at first you need to establish the connection to the server. sdk provides methods for initializing the connection configuration as well as methods for making connection.

Note: Even if you don't have internet connection, you are still allowed to send messages which will be kept in offline database.Once the user connected to the internet, the messages will be sent automatically when user opens the app.

Preparing single chat jid#

Almost of the sdk methods expect jid as a input parameter, so sdk provides below utility method to prepare the jid. The method prepares the single chat jid from the given string by using chat config provided via [com.contusflysdk.api.ChatConnectionManager.initialize] method.

Note: The below characters is not allowed in uniqueId: U+0022 (") U+0026 (&) U+0027 (') U+002F (/) U+003A (:) U+003C (<) U+003E (>) U+0040 (@).

ArgumentTypeDescription
UNIQUE_USER_IDStringunique userId for preparing JID
FlyUtils.getJid(UNIQUE_USER_ID)

Text message#

Text is a basic form of communication between users. sdk provides methods to send the text message to the end users. once the user has sent message via sdk, it will give callback with status. if you want send the text message for a user, you can utilise the below method.

To send your text message, you need to pass the TextMessage object as an argument to the parameter in the sendTextMessage() method.

var textMessage = TextMessage()
textMessage.toId = TO_JID
textMessage.messageText = MESSAGE
textMessage.replyMessageId = REPLY_MESSAGE_ID // Optional
textMessage.mentionedUsersIds = MENTION_IDS // Optional
textMessage.metaData = META_DATA // Optional
textMessage.topicID = TOPIC_ID // Optional
FlyMessenger.sendTextMessage(messageParams: messageParams) { isSuccess, error, chatMessage in
if (isSuccess) {
//you can add this message object to your arraylist,
//then you can notify the tableview
}
}
ArgumentTypeDescription
MESSAGE_PARAMSTextMessageObject to hold the parameters of the text message
CALLBACKFlySendMessageListenercallback to observe the action status

Location message#

Location sharing is a famous communication between users. sdk provides methods to send the location message to the end users. once the user has sent message via sdk, it will give callback with status. if you want send the location message for a user, you can utilise the below method.

To send your location message, you need to pass the FileMessage object with LocationMessageParams set as an argument to the parameter in the sendFileMessage() method.

var locationMessageParams = LocationMessageParams()
locationMessageParams.latitude = LATITUDE
locationMessageParams.longitude = LONGITUDE
var fileMessage = FileMessage()
fileMessage.toId = TO_JID
fileMessage.messageType = MessageType.location
fileMessage.locationMessage = locationMessageParams
fileMessage.replyMessageId = REPLY_MESSAGE_ID // Optional
fileMessage.mentionedUsersIds = MENTION_IDS // Optional
fileMessage.metaData = META_DATA // Optional
fileMessage.topicID = TOPIC_ID // Optional
FlyMessenger.sendFileMessage(messageParams: fileMessage){ isSuccess,error,message in
if (isSuccess) {
//you can add this message object to your arraylist,
//then you can notify the tableview
}
}
ArgumentTypeDescription
MESSAGE_PARAMSFileMessageObject to hold the parameters of the location message
CALLBACKFlySendMessageListenercallback to observe the action status
caution

If Location attachment feature unavailable for your plan then it will throw 403 exception.

Contact message#

Contact sharing is very useful communication between users. sdk provides methods to send the contact message to the end users. once the user has sent message via sdk, it will give callback with status. if you want send the contact message for a user, you can utilise the below method.

To send your contact message, you need to pass the FileMessage object with ContactMessageParams set as an argument to the parameter in the sendFileMessage() method.

var contactMessageParam = ContactMessageParams()
contactMessageParam.name = CONTACT_NAME
contactMessageParam.numbers = CONTACT_NUMBERS
var fileMessage = FileMessage()
fileMessage.toId = TO_JID
fileMessage.messageType = MessageType.contact
fileMessage.contactMessage = contactMessageParam
fileMessage.replyMessageId = REPLY_MESSAGE_ID // Optional
fileMessage.mentionedUsersIds = MENTION_IDS // Optional
fileMessage.metaData = META_DATA // Optional
fileMessage.topicID = TOPIC_ID // Optional
FlyMessenger.sendFileMessage(messageParams: fileMessage){ isSuccess,error,message in
if (isSuccess) {
//you can add this message object to your arraylist,
//then you can notify the tableview
}
}
ArgumentTypeDescription
MESSAGE_PARAMSFileMessageObject to hold the parameters of the contact message
CALLBACKFlySendMessageListenercallback to observe the action status
caution

If Contact attachment feature unavailable for your plan then it will throw 403 exception.

Document message#

Document sharing is very useful communication between users. sdk provides methods to send the document message to the end users. once the user has sent message via sdk, it will give callback with status. if you want send the document message for a user, you can utilise the below method.

To send your document message, you need to pass the FileMessage object with FileMessageParams set as an argument to the parameter in the sendFileMessage() method.

var fileMessageParams = FileMessageParams()
fileMessageParams.fileUrl = FILE_URL
fileMessageParams.fileName = FILE_NAME
var fileMessage = FileMessage()
fileMessage.toId = TO_JID
fileMessage.messageType = MessageType.document
fileMessage.fileMessage = fileMessageParams
fileMessage.replyMessageId = REPLY_MESSAGE_ID // Optional
fileMessage.mentionedUsersIds = MENTION_IDS // Optional
fileMessage.metaData = META_DATA // Optional
fileMessage.topicID = TOPIC_ID // Optional
FlyMessenger.sendFileMessage(messageParams: fileMessage){ isSuccess,error,message in
if (isSuccess) {
//you can add this message object to your arraylist,
//then you can notify the tableview
}
}
ArgumentTypeDescription
MESSAGE_PARAMSFileMessageObject to hold the parameters of the document message
CALLBACKFlySendMessageListenercallback to observe the action status
caution

If Document attachment feature unavailable for your plan then it will throw 403 exception.

Video message#

Video sharing is very useful communication between users. sdk provides methods to send the video message to the end users. once the user has sent message via sdk, it will give callback with status. if you want send the video message for a user, you can utilise the below method.

To send your video message, you need to pass the FileMessage object with FileMessageParams set as an argument to the parameter in the sendFileMessage() method.

var fileMessageParams = FileMessageParams()
fileMessageParams.fileUrl = FILE_URL
fileMessageParams.fileName = FILE_NAME
fileMessageParams.fileSize = FILE_SIZE
fileMessageParams.duration = DURATION
fileMessageParams.thumbImage = THUMB_IMAGE
fileMessageParams.caption = CAPTION
fileMessageParams.fileKey = FILE_KEY
var fileMessage = FileMessage()
fileMessage.toId = TO_JID
fileMessage.messageType = MessageType.video
fileMessage.fileMessage = fileMessageParams
fileMessage.replyMessageId = REPLY_MESSAGE_ID // Optional
fileMessage.mentionedUsersIds = MENTION_IDS // Optional
fileMessage.metaData = META_DATA // Optional
fileMessage.topicID = TOPIC_ID // Optional
FlyMessenger.sendFileMessage(messageParams: fileMessage){ isSuccess,error,message in
if (isSuccess) {
//you can add this message object to your arraylist,
//then you can notify the tableview
}
}
ArgumentTypeDescription
MESSAGE_PARAMSFileMessageObject to hold the parameters of the video message
LISTENERFlySendMessageListenercallback to observe the action status

To get the metadata of a video file for sending videos make use of the utility methods MediaUtils.processVideo() and MediaUtils.compressVideo()

caution

If Video attachment feature unavailable for your plan then it will throw 403 exception.

Image message#

To send image as a message call the below method. Calling the below method will upload the image file to the server provided during sdk initialization then send the message

To send your image message, you need to pass the FileMessage object with FileMessageParams set as an argument to the parameter in the sendFileMessage() method.

var fileMessageParams = FileMessageParams()
fileMessageParams.fileUrl = FILE_URL
fileMessageParams.fileName = FILE_NAME
fileMessageParams.fileSize = FILE_SIZE
fileMessageParams.thumbImage = THUMB_IMAGE
fileMessageParams.caption = CAPTION
fileMessageParams.fileKey = FILE_KEY
var fileMessage = FileMessage()
fileMessage.toId = TO_JID
fileMessage.messageType = MessageType.image
fileMessage.fileMessage = fileMessageParams
fileMessage.replyMessageId = REPLY_MESSAGE_ID // Optional
fileMessage.mentionedUsersIds = MENTION_IDS // Optional
fileMessage.metaData = META_DATA // Optional
fileMessage.topicID = TOPIC_ID // Optional
FlyMessenger.sendFileMessage(messageParams: fileMessage){ isSuccess,error,message in
if (isSuccess) {
//you can add this message object to your arraylist,
//then you can notify the tableview
}
}
ArgumentTypeDescription
MESSAGE_PARAMSFileMessageObject to hold the parameters of the image message
CALLBACKFlySendMessageListenercallback to observe the action status

To get the metadata of an image file for sending images make use of the utility methods MediaUtils.getAssetsImageInfo() and MediaUtils.compressImage()

caution

If Image attachment feature unavailable for your plan then it will throw 403 exception.

Audio message#

To send audio as a message call the below method. Calling the below method will upload the audio file to the server provided during sdk initialization then send the message

To send your audio message, you need to pass the FileMessage object with FileMessageParams set as an argument to the parameter in the sendFileMessage() method.

var fileMessageParams = FileMessageParams()
fileMessageParams.fileUrl = FILE_URL
fileMessageParams.fileName = FILE_NAME
fileMessageParams.fileSize = FILE_SIZE
fileMessageParams.duration = DURATION
fileMessageParams.caption = CAPTION
fileMessageParams.fileKey = FILE_KEY
var fileMessage = FileMessage()
fileMessage.toId = TO_JID
fileMessage.messageType = MessageType.audio
fileMessage.fileMessage = fileMessageParams
fileMessage.replyMessageId = REPLY_MESSAGE_ID // Optional
fileMessage.mentionedUsersIds = MENTION_IDS // Optional
fileMessage.metaData = META_DATA // Optional
fileMessage.topicID = TOPIC_ID // Optional
FlyMessenger.sendFileMessage(messageParams: fileMessage){ isSuccess,error,message in
if (isSuccess) {
//you can add this message object to your arraylist,
//then you can notify the tableview
}
}
ArgumentTypeDescription
MESSAGE_PARAMSFileMessageObject to hold the parameters of the audio message
listenerFlySendMessageListenercallback to observe the action status

To get the metadata of an audio file for sending audio make use of the utility method MediaUtils.processAudio()

caution

If Audio attachment feature unavailable for your plan then it will throw 403 exception.

Get messages#

To fetch all the conversation between you and a single chat user or group, call the below method.

var messages : [ChatMessage] = FlyMessenger.getMessagesOf(jid: JID)
ArgumentTypeDescription
JIDStringJid of the chat user/group

Get media messages#

To get the media messages of a conversation, call the below method.

var mediaMessages : [ChatMessage] = FlyMessenger.getMediaMessagesOf(jid: JID)
ArgumentTypeDescription
JIDStringJid of the chat user/group

To get Video, Image, and Audio messages a conversation, call the below method. It is used in view all media to get array of messages group by month

ChatManager.getVideoImageAudioMessageGroupByMonth(jid: jid) { (isSuccess, flyError, resultDict) in
}
ArgumentTypeDescription
jidStringJid of the chat user/group

Get Single message#

To get a single message from database using a message id, call the below method.

var message : ChatMessage? = FlyMessenger.getMessageOfId(MESSAGE_MID)
ArgumentTypeDescription
MESSAGE_MIDStringId of a message

Get messages using ids#

To get array of messages from database using their message ids, call the below method.

var messages : [ChatMessage] = FlyMessenger.getMessagesUsingIds(MESSAGE_MIDS)
ArgumentTypeDescription
MESSAGE_MIDS[String]Ids of messages

Remove unread message separator#

To remove the unread message separator in a chat conversation list call the below method.

FlyMessenger.deleteUnreadMessageSeparatorOfAConversation(JID)
ArgumentTypeDescription
JIDStringJid of the user/group

Favourite Messages#

Favourite messags is feature which allows users to mark some messages as favourites. so that they can see those important messages in future without searching for them in chat. Once you marked messages as favourite, you can get those from sdk to show it in your app's screen.

ArgumentTypeDescription
MESSAGE_IDStringmessageId of the message
CHAT_USER_JIDStringjid of the chat user
IS_FAVOURITEbooleantrue, if you are adding to favourites.false to remove from favourites
CALLBACKChatActionListenercallback to observe the action status

Make favourite#

You can mark the messages as favourite by using the below method.

ChatManager.updateFavouriteStatus(MESSAGE_ID, CHAT_USER_JID, true) {isSuccess,error,message in
}

Remove favourite#

You can remove the messages from favourite by using the below method.

ChatManager.updateFavouriteStatus(MESSAGE_ID, CHAT_USER_JID, false) {isSuccess,error,message in
}

Get all favourite messages#

The below method will return all the favourite messages from the local db.

let favouriteMessages = ChatManager.getFavouriteMessages()

Upload/Download media message#

Whenever you receive a media message from a user, or when retrying to upload a already sent media message you can use built-in sdk functions to upload/download the media content / The below method is applicable only if you are using sdk media server.

FlyMessenger.uploadMedia(MEDIA_MESSAGE_ID);
FlyMessenger.downloadMedia(MEDIA_MESSAGE_ID);
ArgumentTypeDescription
MEDIA_MESSAGE_IDStringMessageID of the ChatMeassage

Note: Use same methods for Upload/Download Media Retry.

Delete messages#

Delete messsage is a feature which is found in modern messaging apps which allows the user to delete the message once it is sent.Once you sent the message, you can able to delete the messages by using built in sdk methods.

Delete for me#

If you want to delete the messages for yourself only then you can use the below method.

Note: The messages will not be deleted to the receipient.

ArgumentTypeDescription
TO_JIDStringjid of the end user
MESSAGE_ID_LIST[String]List of messageId to be deleted
DELETE_CHAT_TYPEDeleteChatTypeDeleteChatType.chat for single chat, DeleteChatType.groupchat for group chat
ISREVOKE_MEDIA_ACCESSBooleanTrue - selected media file can be deleted,False - selected media file cannot be deleted
CALLBACKDeleteMessageListenercallback to observe the action status
ChatManager.deleteMessagesForMe(TO_JID, MESSAGE_ID_LIST,DELETE_CHAT_TYPE,ISREVOKE_MEDIA_ACCESS)(isSuccess, message) -> {
if isSuccess {
}
};
caution

If delete message feature unavailable for your plan then below methods will throw 403 exception.

Delete for everyone#

If you want to delete the messages for yourself and receiver then you can use the below method. The messages will also be deleted to the receipient.

ArgumentTypeDescription
TO_JIDStringjid of the end user
MESSAGE_ID_LIST[String]List of messageId to be deleted
DELETE_CHAT_TYPEDeleteChatTypeDeleteChatType.chat for single chat, DeleteChatType.groupchat for group chat
ISREVOKE_MEDIA_ACCESSBooleanTrue - selected media file can be deleted,False - selected media file cannot be deleted
CALLBACKDeleteMessageListenercallback to observe the action status
ChatManager.deleteMessagesForEveryone(TO_JID, MESSAGE_ID_LIST,DELETE_CHAT_TYPE,ISREVOKE_MEDIA_ACCESS)(isSuccess, message) -> {
if isSuccess {
}
};
caution

If delete message feature unavailable for your plan then below methods will throw 403 exception.

Cancel upload/download#

Whenever you would like to cancel upload/download of a media message , you can use built-in sdk functions to cancel the media content upload/download.The below method is applicable only if you are using sdk media server.

FlyMessenger.cancelMediaUploadOrDownload(MESSAGE_ID)
ArgumentTypeDescription
MESSAGE_IDStringmessage id of the media message

When the user cancelled the media download, then user need to restart the download using FlyMessenger.downloadMedia(messageId: String) method. In case of media upload, the upload initiated once the user sent the message In case of retry call FlyMessenger.uploadMedia(messageId: String).