/ SDKs / iOS
SDKs
Chat SDKs iOS v4
Chat SDKs iOS
Chat SDKs
iOS
Version 4

Send a message

Copy link

For both open and group channels, users can send messages of the following types to the channel they are in.

Message typeClassDescription

Text

UserMessage

A text message sent by a user

File

FileMessage

A binary file message sent by a user.

File

MultipleFilesMessage

A a message with multiple files sent by a user.

In addition to these message types, you can further subclassify a message by specifying its custom type. This custom type takes on the form of String and can be used to search or filter messages. It allows you to append information to your message and customize message categorization.

The following code shows several types of parameters that you can configure to customize text messages by using UserMessageCreateParams. Under the UserMessageCreateParams object, you can assign values to message, data, and other properties. By assigning arbitrary string to the data property, you can set custom font size, font type or JSON object. To send your messages, you need to pass the UserMessageCreateParams object as an argument to the parameter in the sendUserMessage(params:completionHandler:) method.

Through the completionHandler parameter of the sendUserMessage(params:completionHandler:) method, the Sendbird server always notifies whether your message has been successfully sent to the channel. When there is a delivery failure due to network issues, an exception is returned through the callback method.

let params = UserMessageCreateParams(message: MESSAGE)
params.customType = CUSTOM_TYPE
params.data = DATA
params.mentionType = .users // Acceptable values are .users and .channel.
params.mentionedUserIds = ["Jeff", "Julia"] // Or .mentionedUsers = LIST_OF_USERS_TO_MENTION
params.metaArrays = [
    MessageMetaArray(key: "itemType", value: ["tablet"]),
    MessageMetaArray(key: "quality", value: ["best", "good"])
]
params.translationTargetLanguages = ["fr", "de"] // French and German
params.pushNotificationDeliveryOption = .default

channel.sendUserMessage(params: params) { userMessage, error in
    guard error == nil else {
        // Handle error.
        return
    }

    // A text message with detailed configuration is successfully sent to the channel.
    // By using userMessage.messageId, userMessage.message, userMessage.customType, and so on,
    // you can access the result object from the Sendbird server
    // to check your UserMessageCreateParams configuration.
    // The current user can receive messages from other users
    // through the channel(_:didReceive:) method of an event delegate.
}

A user can also send a binary file through the Chat SDK as the file itself or using a URL.

Sending a raw file means you're uploading it to the Sendbird server where it can be downloaded on client apps. When you upload a file directly to the server, there is a size limit imposed on the file depending on your plan. You can see the limit on your dashboard and contact our sales team to change the limit.

The other option is to send a file hosted on your server. You can pass the file's URL, which represents its location, as an argument to a parameter. In this case, your file is not hosted on the Sendbird server and it can only be downloaded from your own server. When you send a file message with a URL, there is no limit on the file size since it isn't directly uploaded to the Sendbird server.

Note: You can use sendFileMessages(params:progressHandler:sentMessageHandler:completionHandler:), which is another method that allows you to send up to 20 file messages per one method call. Refer to our API Reference to learn more.

The following code shows several types of parameters that you can configure to customize your file messages by using FileMessageCreateParams. Under the FileMessageCreateParams object, you can assign specific values to customType, and other properties. To send your messages, you need to pass the FileMessageCreateParams object as an argument to the parameter in the sendFileMessage(params:completionHandler:) method.

Through the completionHandler parameter of the sendFileMessage(params:completionHandler:) method, the Sendbird server always notifies whether your message has been successfully sent to the channel. When there is a delivery failure due to network issues, an exception is returned through the callback method.

// Send a file message with a raw file.
var thumbnailSizes = [ThumbnailSize]()
thumbnailSizes.append(ThumbnailSize.make(maxSize: CGSize(width: 100.0, height: 100.0))) // 3 thumbnail images are allowed.
thumbnailSizes.append(ThumbnailSize.make(maxWidth: 200.0, maxHeight: 200.0))

let params = FileMessageCreateParams(file: FILE) // Or FileMessageCreateParams(fileURL: FILE_URL)
params.fileName = FILE_NAME
params.fileSize = FILE_SIZE
params.mimeType = MIME_TYPE
params.thumbnailSizes = thumbnailSizes
params.customType = CUSTOM_TYPE
params.mentionType = .users     // Acceptable values are .users and .channel.
params.mentionedUserIds = ["Jeff", "Julia"]     // Or .mentionedUsers = LIST_OF_USERS_TO_MENTION
params.pushNotificationDeliveryOption = .default

channel.sendFileMessage(params: params) { fileMessage, error in
    guard error == nil else {
        // Handle error.
        return
    }

    // A file message with detailed configuration is successfully sent to the channel.
    // By using fileMessage.messageId, fileMessage.fileName, fileMessage.customType, and so on,
    // you can access the result object from the Sendbird server
    // to check your FileMessageCreateParams configuration.
    // The current user can receive messages from other users
    // through the channel(_:didReceive:) method of an event delegate.
}

If your app goes to the background while uploading a file such as a profile image or picture, the app can complete the upload process using the application(_:handleEventsForBackgroundURLSession:completionHandler:) method in your AppDelegate. To complete a file upload in progress in the background, a background event delegate should be added and implemented in AppDelegate. If you don't want to upload a file while the app is in the background, remove the following delegation in AppDelegate.

// AppDelegate.swift
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) {
        print("method for handling events for background URL session is waiting to be process. background session id: \(identifier)")
        completionHandler()
    }
}

You can also send a message with multiple files through sendMultipleFilesMessage() by passing an object MultipleFilesMessageCreateParams. The params contains uploadableFileInfoList, which holds an array of uploadableFileInfo objects. A single uploadableFileInfo object contains the data of each file such as fileUrl and fileSize.

let firstFile = multipleFilesMessage.files[0]
let secondFile = multipleFilesMessage.files[1]

let nameOfFirstFile = firstFile.fileName
let sizeOfFirstFile = firstFile.fileSize
let thumbnailsOfFirstFile = firstFile.thumbnails

Once all of the files were uploaded to the Sendbird server and its message was successfully sent, file-related data is contained in the message object's files as an array of UploadedFileInfo.

If the upload of some of the files fail while others succeed, the message is marked as failed and the server returns the URL of successfully uploaded files in a failed message through onResult(). You could use the URLs when retrying the message request.