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
try 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.

FlyMessenger.sendTextMessage(TO_JID, TEXT, REPLY_MESSAGE_ID, MENTION_IDS){isSuccess,error,message in
if isSuccess {
// append message in your array and the update ui
// reset the message TextField UI
}
}
ArgumentTypeDescription
TO_JIDStringjid of the end user
MESSAGEStringText message to be sent
REPLY_MESSAGE_IDStringif it is a reply message for message A, then message A's messageId otherwise empty string
MENTION_IDS[String]list of userId to mention specific users while sending a group message, otherwise it will be empty
CALLBACKFlySendMessageListenercallback to observe the action status

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
if let chatMessages = result.getData() as? [[ChatMessage]] {
}
}
ArgumentTypeDescription
jidStringJid of the chat user/group
CALLBACKFlyCompletionHandlerFlyCompletionHandler used as completion Handler

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

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.

ArgumentTypeDescription
TO_JIDStringjid of the end user
LATITUDEDoublelocation latitude which needs to be sent
LONGITUDEDoublelocation longitude which needs to be sent
REPLY_MESSAGE_IDStringif it is a reply message for message A, then message A's messageId otherwise empty string
CALLBACKSendMessageListenercallback to observe the action status
FlyMessenger.sendLocationMessage(TO_JID, LATITUDE, LONGITUDE, REPLY_MESSAGE_ID){isSuccess,error,message in
if isSuccess {
// append message in your array and the update ui
// reset the message TextField UI
}
}
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.

ArgumentTypeDescription
TO_JIDStringjid of the end user
CONTACT_NAMEStringcontact name
CONTACT_NUMBERSList<String>list of numbers in that contact
REPLY_MESSAGE_IDStringif it is a reply message for message A, then message A's messageId otherwise empty string
CALLBACKSendMessageListenercallback to observe the action status
FlyMessenger.sendContactMessage(TO_JID, CONTACT_NAME, CONTACT_NUMBERS, REPLY_MESSAGE_ID){isSuccess,error,message in
if isSuccess {
// append message in your array and the update ui
// reset the message TextField UI
}
}
caution

If Contact 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.

ArgumentTypeDescription
TO_JIDStringjid of the end user
MEDIA_DATAMediaDatastruct to hold the metadata of the video file
REPLY_MESSAGE_IDStringif it is a reply message for message A, then message A's messageId otherwise empty string
MENTION_IDS[String]list of userId to mention specific users while sending a group message, otherwise it will be empty
LISTENERSendMessageListenercallback to observe the action status
FlyMessenger.sendVideoMessage(TO_JID, MEDIA_DATA, REPLY_MESSAGE_ID, MENTION_IDS){isSuccess,error,message in
if isSuccess {
// append message in your array and the update ui
// reset the message TextField UI
}
}

To compose MediaData object 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

ArgumentTypeDescription
TO_JIDStringjid of the end user
MEDIA_DATAMediaDatastruct to hold the metadata of the image file
REPLY_MESSAGE_IDStringif it is a reply message for message A, then message A's messageId otherwise empty string
MENTION_IDS[String]list of userId to mention specific users while sending a group message, otherwise it will be empty
CALLBACKSendMessageListenercallback to observe the action status
FlyMessenger.sendImageMessage(TO_JID, MEDIA_DATA, REPLY_MESSAGE_ID, MENTION_IDS){isSuccess,error,message in
if isSuccess {
// append message in your array and the update ui
// reset the message TextField UI
}
}

To compose MediaData object 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.

Document message#

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

ArgumentTypeDescription
TO_JIDStringjid of the end user
MEDIA_DATAMediaDatastruct to hold the metadata of the document file
REPLY_MESSAGE_IDStringif it is a reply message for message A, then message A's messageId otherwise empty string
CALLBACKSendMessageListenercallback to observe the action status
FlyMessenger.sendDocumentMessage(TO_JID, MEDIA_DATA, REPLY_MESSAGE_ID){isSuccess,error,message in
if isSuccess {
// append message in your array and the update ui
// reset the message TextField UI
}
}
caution

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

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
CHAT_TYPEChatTypeEnumChatType.singleChat for single chat, ChatType.groupchat for group chat, ChatType.broadcast for broadcast
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, CHAT_TYPE, 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, CHAT_TYPE, false) {isSuccess,error,message in
}

Get all favourite messages#

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

let favouriteMessages: [ChatMessage] = ChatManager.getFavouriteMessages()

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

ArgumentTypeDescription
TO_JIDStringjid of the end user
MEDIA_DATAMediaDatastruct to hold the metadata of the audio file
IS_RECORDEDBooleantrue if the audio file is a recoreded or voice message else false
replyMessageIdStringif it is a reply message for message A, then message A's messageId otherwise empty string
listenerSendMessageListenercallback to observe the action status
FlyMessenger.sendAudioMessage(TO_JID, MEDIA_DATA,IS_RECORDED,REPLY_MESSAGE_ID)(isSuccess, message) -> {
if (message != null) {
//you can add this message object to your array,
//then you can call notifyItemInserted in adapter of recycler view
}
});

To compose MediaData object 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.

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).