Messages
This section contains what all messages related actions can be performed using Channelize.io iOS API SDK functions.
Get Messages
Retrieves the list of messages from a conversation.
Function
func getConversationMessages(conversationId: String, queryBuilder: CHGetMessageQueryBuilder, completion: @escaping ([CHMessage]?,String?) -> ())
Function Params
Name | Type | Required | Description |
---|---|---|---|
conversationId | String | Yes | The unique ID of the conversation. |
queryBuilder | CHGetMessageQueryBuilder | No | Query Builder to filter the result. |
CHGetMessageQueryBuilder
Variables
Name | Type | Required | Description |
---|---|---|---|
ids | [String] | Yes | Search the messages with message IDs. |
types | [MessageType] | No | Search the messages with types separated by commas. Allowed values are .normal , .forward , .quotedMessage , .admin . By default all the messages are retrived. |
customTypes | [String] | No | Search the messages with custom types separated by commas. |
attachmentTypes | [AttachmentType] | No | Search the messages with type field of attachments separated by commas. like .text , .image , .audio , .video , .location , .gif , .sticker , .document etc. |
ownerIds | [String] | No | Search the messages with message's owner IDs separated by commas. |
parentId | String | No | Search the reply messages of given parent message. |
sort | CHMessageSorting | No | Specifies the sorting criteria for the returned results. Allowed values are .ASC and .DESC . |
limit | Int | No | Specifies the number of results to return. Default is 50 |
skip | Int | No | Specifies the number of results you want to skip from the beginning. |
Request Example
let queryBuilder = CHGetMessageQueryBuilder()
queryBuilder.limit = 40
queryBuilder.skip = 0
queryBuilder.attachmentTypes = [.audio,.image,.video]
queryBuilder.types = [.normal,.quotedMessage]
ChannelizeAPIService.getConversationMessages(conversationId: "42ab4d40-666a-11e9-bdcd-0bf68210bf49", queryBuilder: queryBuilder, completion: {(messages,errorString) in
guard errorString == nil else {
print("Failed to get Messages. Error: \(errorString ?? "")")
return
}
if let recievedMessages = messages {
// Implement Your logic.
}
})
Completion Handler
messages
: Optional array ofCHMessage
.errorString
: Optional error string if api call was unsuccessful.
Get Messages Count
Retrieves the total number of messages in a conversation.
Function
func getConversationMessagesCount(conversationId: String, queryBuilder: CHGetMessageCountQueryBuilder, completion: @escaping (Int,String?) -> ())
Function Params
Name | Type | Required | Description |
---|---|---|---|
conversationId | String | Yes | The unique ID of the conversation. |
queryBuilder | CHGetMessageCountQueryBuilder | No | Query Builder to filter the result. |
CHGetMessageCountQueryBuilder
Variables
Name | Type | Required | Description |
---|---|---|---|
ids | [String] | Yes | Search the messages with message IDs. |
types | [MessageType] | No | Search the messages with types separated by commas. Allowed values are .normal , .forward , .quotedMessage , .admin . By default all the messages are retrived. |
customTypes | [String] | No | Search the messages with custom types separated by commas. |
attachmentTypes | [AttachmentType] | No | Search the messages with type field of attachments separated by commas like .text , .image , .audio , .video , .location , .gif , .sticker , .document etc. |
ownerIds | [String] | No | Search the messages with message's owner IDs separated by commas. |
parentId | String | No | Search the reply messages of given parent message. |
Request Example
let queryBuilder = CHGetMessageCountQueryBuilder()
queryBuilder.attachmentTypes = [.audio,.image,.video]
queryBuilder.types = [.normal,.quotedMessage]
ChannelizeAPIService.getConversationMessagesCount(conversationId: "42ab4d40-666a-11e9-bdcd-0bf68210bf49", queryBuilder: queryBuilder, completion: {(count,errorString) in
guard errorString == nil else {
print("Failed to get Messages count. Error: \(errorString ?? "")")
return
}
print("Total Message Count is \(count)")
})
Completion Handler
count
: Count of Messages.errorString
: Optional error string if api call was unsuccessful.
Send Messages
Sends a message to the conversation.
Channelize supports different types of messages like normal, reply, forward, admin.
- Normal: A normal message created in the conversation.
- Forward: A forwarded message from the previous message. Moreover, Forward messages API call automatically send messages of this type.
- Reply: A message in a reply thread. Messages created with parent_id are automatic of this type.
- Admin: A message generated by a system event, like updating the group, add members, remove members, etc.
Function
func sendMessage(queryBuilder: CHMessageQueryBuilder, uploadProgress: @escaping (UUID?,Double?) -> (), completion: @escaping (CHMessage?,String?) -> ())
CHMessageQueryBuilder
Name | Type | Required | Description |
---|---|---|---|
id | String | No | The unique ID of the message. If not provided, will be automatically generated. |
conversationId | String | Yes (if no userId) | Specify the conversation ID of the conversation where the message is sent to. |
userId | String | Yes(if no conversationId) | Specify the user ID of the message receiver. |
messageType | MessageType | No | Specify the type of message. Allowed values are .normal , .admin , .quotedMessage , .forward . |
createdAt | Date | No | The creation time of the message. If not provided, it will be auto-generated. |
body | String | No | The content of the message. |
parentId | String | No | The ID of the parent message, if the message is a reply. |
recipients | [String] | No | The ID of users who you want to send and display messages in a conversation but not for other users. If the recipients field is not passed, by default the message is sent to all members of a conversation. |
attachments | [CHBuilderMessageType] | No | A list of attachments (text, image, audio, video, document, location, gif and sticker). |
mentionedUsers | [CHMentionedUserQueryBuilder] | No | Specify users mentioned in the message. |
Attachments
You can send one or multiple attachments in the message. All attachments confirm to the CHBuilderMessageType
protocol. Seven types of attachments query builders are supported. They are CHImageAttachmentQueryBuilder
, CHVideoAttachmentQueryBuilder
, CHAudioAttachmentQueryBuilder
, CHLocationAttachmentQueryBuilder
, CHGifAttachmentQueryBuilder
, CHStickerAttachmentQueryBuilder
, CHDocAttachmentQueryBuilder
.
Image Attachment
To create an image attachment, create an object of CHImageAttachmentQueryBuilder
class and assign properties to it.
Name | Type | Required | Description |
---|---|---|---|
attachMentIdentifier | UUID | No | A unique identifier if you want to get upload the progress of the file. |
imageData | Data | Yes | Image Data for the image. |
thumbNailData | Data | Yes | Thumbnail Data for the thumbnail of the image. |
fileName | String | No | The name of the file. |
size | Int | No | The size of the file in bytes. |
For e.g
let imageAttachMentQuery = CHImageAttachmentQueryBuilder()
imageAttachMentQuery.imageData = Data()
imageAttachMentQuery.thumbNailData = Data()
imageAttachMentQuery.fileName = "Demo Image"
Video Attachment
To create a video attachment, create an object of CHVideoAttachmentQueryBuilder
and assign properties to it.
Name | Type | Required | Description |
---|---|---|---|
attachMentIdentifier | UUID | No | A unique identifier if you want to get uploading progress of the file. |
videoData | Data | Yes | Data for the Video. |
thumbNailData | Data | Yes | Thumbnail Data for the thumbnail of the video. |
fileName | String | No | The name of the file. |
size | Int | No | The size of the file in bytes. |
For e.g.
let videoAttachmentQuery = CHVideoAttachmentQueryBuilder()
videoAttachmentQuery.attachMentIdentifier = UUID()
videoAttachmentQuery.videoData = videoData
videoAttachmentQuery.thumbNailData = thumbnailData
videoAttachmentQuery.fileName = "My New Video"
Audio Attachment
To create an audio attachment, create an object of CHAudioAttachmentQueryBuilder
and assign properties to it.
Name | Type | Required | Description |
---|---|---|---|
attachMentIdentifier | UUID | No | A unique identifier if you want to get upload the progress of the file. |
audioData | Data | Yes | Data for the audio. |
duration | Double | Yes | Duration of audio file in milliseconds. |
fileName | String | No | The name of the file. |
fileExtension | String | No | The extension of file. |
mimeType | String | No | The mime type of the file. |
size | Int | No | The size of the file in bytes. |
For e.g.
let audioAttachmentQuery = CHAudioAttachmentQueryBuilder()
audioAttachmentQuery.attachMentIdentifier = UUID()
audioAttachmentQuery.audioData = audioData
audioAttachmentQuery.duration = 100034
audioAttachmentQuery.fileExtension = "m4a"
audioAttachmentQuery.mimeType = "audio/m4a"
audioAttachmentQuery.fileName = "My New Song"
Location Attachment Query
To create a location attachment, create an object of CHLocationAttachmentQueryBuilder
and assign properties to it.
Name | Type | Required | Description |
---|---|---|---|
locationTitle | String | No | Name of the Location. |
locationAddress | String | No | Address of the location. |
locationLatitude | Double | Yes | Latitude of the location. |
locationLongitude | Double | Yes | Longitude of the location. |
For e.g.
let locationAttachmentQueryBuilder = CHLocationAttachmentQueryBuilder()
locationAttachmentQueryBuilder.locationTitle = "Union Square"
locationAttachmentQueryBuilder.locationAddress = "320 Geary St, San Francisco 94102, United States"
locationAttachmentQueryBuilder.locationLatitude = 37.7873589
locationAttachmentQueryBuilder.locationLongitude = -122.408227
GIF Attachment
To create a GIF Attachment, create CHGifAttachmentQueryBuilder
object and assign properties to it.
Name | Type | Required | Description |
---|---|---|---|
gifStillUrl | String | No | Still url of the GIF. |
gifDownSampledUrl | String | No | Down sampled URL of the GIF. |
gifOriginalUrl | String | Yes | Original Url of the GIF. |
For e.g.
let gifQueryBuilder = CHGifAttachmentQueryBuilder()
gifQueryBuilder.gifStillUrl = "https://media3.giphy.com/media/KenDhChWfWEgliudjI/200_s.gif"
gifQueryBuilder.gifDownSampledUrl = "https://media3.giphy.com/media/KenDhChWfWEgliudjI/200_d.gif"
gifQueryBuilder.gifOriginalUrl = "https://media3.giphy.com/media/KenDhChWfWEgliudjI/200.gif"
Sticker Attachment
To create a sticker attachment, create CHStickerAttachmentQueryBuilder
object and assign properties to it.
Name | Type | Required | Description |
---|---|---|---|
stickerStillUrl | String | No | Still url of the Sticker. |
stickerDownSampledUrl | String | No | Down sampled URL of the Sticker. |
stickerOriginalUrl | String | Yes | Original Url of the Sticker. |
For e.g.
let stickerQueryBuilder = CHStickerAttachmentQueryBuilder()
stickerQueryBuilder.stickerDownSampledUrl = "https://media3.giphy.com/media/KenDhChWfWEgliudjI/200_d.gif"
stickerQueryBuilder.stickerStillUrl = "https://media3.giphy.com/media/KenDhChWfWEgliudjI/200_s.gif"
stickerQueryBuilder.stickerOriginalUrl = "https://media3.giphy.com/media/KenDhChWfWEgliudjI/200.gif"
Document Attachment
To create a document attachment, create CHDocAttachmentQueryBuilder
object and assign properties to it.
Name | Type | Required | Description |
---|---|---|---|
attachMentIdentifier | UUID | No | A unique identifier if you want to get upload the progress of the file. |
fileData | Data | Yes | Data for the Document. |
fileName | String | No | The name of the file. |
fileExtension | String | No | The extension of file. |
mimeType | String | No | The mime type of the file. |
size | Int | No | The size of the file in bytes. |
For e.g.
let docAttachment = CHDocAttachmentQueryBuilder()
docAttachment.fileName = fileName
docAttachment.mimeType = mimeType
docAttachment.size = fileSize
docAttachment.fileExtension = fileExtension
docAttachment.attachMentIdentifier = uniqueId
docAttachment.fileData = fileData
Mentioned Users
You can specify one or more mentioned users in the message. To create a mentioned user params, create an object of CHMentionedUserQueryBuilder
and assign properties to it.
Name | Type | Required | Description |
---|---|---|---|
order | Int | Yes | Mention user's order in the message in reference to another mentioned user. |
userId | String | No | User ID of mentioned user. |
wordCount | Int | Yes | Mentioned user's name word count. |
For e.g.
let firstMentioned = CHMentionedUserQueryBuilder()
firstMentioned.order = 1
firstMentioned.userId = "21590"
firstMentioned.wordCount = 2
Full Example to send a message
// Mentioned Users
let firstMentioned = CHMentionedUserQueryBuilder()
firstMentioned.order = 1
firstMentioned.userId = "21590"
firstMentioned.wordCount = 2
let secondMentioned = CHMentionedUserQueryBuilder()
secondMentioned.order = 2
secondMentioned.wordCount = 3
secondMentioned.userId = "21908"
/// Attachments
// Image Attachment
let imageAttachMentQuery = CHImageAttachmentQueryBuilder()
imageAttachMentQuery.imageData = demoImageData
imageAttachMentQuery.thumbNailData = demoThumbnailData
imageAttachMentQuery.fileName = "Demo Image"
// Location Attachment
let locationAttachmentQueryBuilder = CHLocationAttachmentQueryBuilder()
locationAttachmentQueryBuilder.locationTitle = "Union Square"
locationAttachmentQueryBuilder.locationAddress = "320 Geary St, San Francisco 94102, United States"
locationAttachmentQueryBuilder.locationLatitude = 37.7873589
locationAttachmentQueryBuilder.locationLongitude = -122.408227
// GIF Attachment
let gifQueryBuilder = CHGifAttachmentQueryBuilder()
gifQueryBuilder.gifStillUrl = "https://media3.giphy.com/media/KenDhChWfWEgliudjI/200_s.gif"
gifQueryBuilder.gifDownSampledUrl = "https://media3.giphy.com/media/KenDhChWfWEgliudjI/200_d.gif"
gifQueryBuilder.gifOriginalUrl = "https://media3.giphy.com/media/KenDhChWfWEgliudjI/200.gif"
// Message Query
let sendMessageBuilder = CHMessageQueryBuilder()
sendMessageBuilder.id = "5a928b10-4258-11ea-9c07-831a487bf6af"
sendMessageBuilder.userId = "21044"
sendMessageBuilder.messageType = .quotedMessage
sendMessageBuilder.body = "Demo text String"
sendMessageBuilder.createdAt = Date()
sendMessageBuilder.ownerId = "21038"
sendMessageBuilder.parentId = "7d928b90-4258-11ea-9c07-920a487bf6af"
sendMessageBuilder.mentionedUsers = [firstMentioned,secondMentioned]
sendMessageBuilder.attachments = [gifQueryBuilder,locationAttachmentQueryBuilder,imageAttachMentQuery]
CHMessageRequest.instance.sendMessage(queryBuilder: sendMessageBuilder, uploadProgress: {(identifier,uploadProgress) in
if let attachmentIdentifier = identifier {
print("Upload progress is \(uploadProgress ?? 0.0) for identifier \(attachmentIdentifier)")
}
}, completion: {(message,errorString) in
guard errorString == nil else {
print("Failed to Send message. Error: \(errorString ?? "")")
return
}
// Use Message object
})
Forward a message
Forward a message into one or multiple conversations. Consider the forwarded messages as a new instance of the message.
Function
func forwardMessages(messageIds: [String], userIds: [String]?, conversationIds: [String]?, completion: @escaping (Bool,String?) -> ())
Function Params
Name | Type | Required | Description |
---|---|---|---|
messageIds | [String] | Yes | Specifies an array of one or more unique IDs of the messages to forward. |
conversationIds | [String] | No | Specifies an array of one or more unique IDs of the conversations where the message is sent to. |
userIds | [String] | No | Specifies an array of one or more unique IDs of the users who'll get the message. |
Please specify at least one of userIds
or 'conversationIds'
Request Example
let messageIds = ["20bf7003-7d64-11ea-818d-3feba8c6b791","30bf7003-7d64-11ea-818d-3feba8c6b791"]
let userIds = ["21544","21568","21694"]
let conversationIds = ["40bf7003-7d64-11ea-818d-3feba8c6b791","50bf7003-7d64-11ea-818d-3feba8c6b791"]
ChannelizeAPIService.forwardMessages(messageIds: messageIds, userIds: userIds, conversationIds: conversationIds, completion: {(status,errorString) in
if status {
print("Messages forward Successfully")
} else {
print("Error in forwarding messages. Error: \(errorSting ?? "")")
}
})
Completion Handler
status
: Bool value indicating the status of the result.errorString
: Optional error string if api call was unsuccessful.
Delete Messages
Deletes a message from a conversation.
Function
func deleteMessages(messageIds: [String], completion: @escaping (Bool,String?) -> ())
Request Example
let messageIds = ["20bf7003-7d64-11ea-818d-3feba8c6b791","30bf7003-7d64-11ea-818d-3feba8c6b791"]
ChannelizeAPIService.deleteMessages(messageIds: messageIds, completion: {(status,errorString) in
if status {
print("Messages deleted Successfully")
} else {
print("Error in Deleting messages. Error: \(errorString ?? "")")
}
})
Completion Handler
status
: Bool value indicating the status of the result.errorString
: Optional error string if api call was unsuccessful.
Delete Messages for Everyone
Recalls messages that you sent from the inbox of the recipients. You can also substitute a replacement message.
Function
func deleteMessagesForEveryOne(messageIds: [String], completion: @escaping (Bool,String?) -> ())
Request Example
let messageIds = ["20bf7003-7d64-11ea-818d-3feba8c6b791","30bf7003-7d64-11ea-818d-3feba8c6b791"]
ChannelizeAPIService.deleteMessagesForEveryOne(messageIds: messageIds, completion: {(status,errorString) in
if status {
print("Messages deleted Successfully")
} else {
print("Error in Deleting messages. Error: \(errorString ?? "")")
}
})
Completion Handler
status
: Bool value indicating the status of the result.errorString
: Optional error string if api call was unsuccessful.
Mark Messages Read
Mark all messages read in a targeted conversation for a user.
Function
func markConversationRead(conversationId: String, timestamp: Date? = nil, completion: @escaping (Bool,String?) -> ())
Function Params
Name | Type | Required | Description |
---|---|---|---|
conversationId | String | Yes | The unique ID of the conversation in which messages are to be marked Read. |
timestamp | Date | No | Specifies the timestamp to be the reference point of marking as read. If specified, the messages received before the specified time are marked as read |
Request Example
ChannelizeAPIService.markConversationRead(conversationId: self.conversation?.id ?? "", completion: {(status,errorString) in
if status {
print("Messages marked Successfully")
} else {
print("Error in marking messages. Error: \(errorString ?? "")")
}
})
Completion Handler
status
: Bool value indicating the status of the result.errorString
: Optional error string if api call was unsuccessful.
Get Unread Message Count
Retrieves the total number of unread messages for a user.
Function
func getUnreadMessageCount(completion: @escaping (Int,String?) -> ())
Request Example
ChannelizeAPIService.getUnreadMessageCount(completion: {(count,errorString) in
guard errorString == nil else {
return
}
print("Total unread Message count is \(count)")
})
Completion Handler
status
: Bool value indicating the status of the result.errorString
: Optional error string if api call was unsuccessful.
Add Reaction
Adds a Reaction to the message. The conversation members can add a reaction to received messages.
Function
func addMessageReaction(messageId: String, reactionType: String, completion: @escaping (Bool,String?) ->())
Function Params
Name | Type | Required | Description |
---|---|---|---|
messageId | String | Yes | The unique ID of the message. |
reactionType | String | Yes | Specifies a reaction type. Example 'like', 'dislike' and more. |
Request Example
let messageId = "66338183-7C4B-423A-BADB-C3470D7BE85E"
ChannelizeAPIService.addMessageReaction(messageId: messageId, reactionType: "like", completion: {(status,errorString) in
if status {
print("Message Reaction Added Successfully")
} else {
print("Failed To Add Message Reaction")
print("Error: \(errorString ?? "")")
}
})
Completion Handler
status
: Bool value indicating the status of the result.errorString
: Optional error string if api call was unsuccessful.
Remove Reaction
Removes a reaction from a message. Only the conversation members can remove their reactions.
Function
func addMessageReaction(messageId: String, reactionType: String, completion: @escaping (Bool,String?) ->())
Function Params
Name | Type | Required | Description |
---|---|---|---|
messageId | String | Yes | The unique ID of the message. |
reactionType | String | Yes | Specifies a reaction type. Example 'like', 'dislike' and more. |
Request Example
let messageId = "66338183-7C4B-423A-BADB-C3470D7BE85E"
ChannelizeAPIService.removeMessageReaction(messageId: messageId, reactionType: "like", completion: {(status,errorString) in
if status {
print("Message Reaction Removed Successfully")
} else {
print("Failed To Remove Message Reaction")
print("Error: \(errorString ?? "")")
}
})
Completion Handler
status
: Bool value indicating the status of the result.errorString
: Optional error string if api call was unsuccessful.
List Reactions
Retrieves the list of users who reacted on a message.
Function
func listMessageReactors(messageId: String, reactionType: String, limit: Int, skip: Int, completion: @escaping ([CHReactorModel]?,String?) ->())
Function Params
Name | Type | Required | Description |
---|---|---|---|
messageId | String | Yes | The unique ID of the message. |
reactionType | String | Yes | Specifies a reaction type. Example 'like', 'dislike' and more. |
limit | Int | Yes | Specifies the number of results to return. |
skip | Int | Yes | Specifies the number of results you want to skip from the beginning. (Useful in Pagination) |
Request Example
let messageId = "66338183-7C4B-423A-BADB-C3470D7BE85E"
ChannelizeAPIService.listMessageReactors(messageId: messageId, reactionType: "like", limit: 30, skip: 0, completion: {(reactors,errorString) in
guard errorString == nil else {
print("Failed To Get Message Reactors")
print("Error: \(errorString ?? "")")
}
// Do your Stuff with Reactors List
})
Completion Handler
reactors
:[CHReactorModel]
array containg reactors infoerrorString
: Optional error string if api call was unsuccessful.