Chat / Flutter
Current version: v3
Chat Flutter v3
Chat Flutter
Chat
Flutter
Home
/
Chat
/
Flutter
/
Message

Send a message

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

Message typeDescription

UserMessage

A text message sent by a user.

FileMessage

A binary file message sent by a user.

Additionally, you can also 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.


User message

The following code shows several types of parameters that you can configure to customize text messages using UserMessageParams. Under the UserMessageParams object, you can assign specific 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 UserMessageParams object as an argument to the parameter in the sendUserMessage() method.

Note: Starting from Flutter Chat SDK 3.1.0, UserMessageParams requires the message parameter.

Through the callback handler of the sendUserMessage() 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.

Open channel

final params = UserMessageParams(message: MESSAGE)
    ..customType = CUSTOM_TYPE
    ..data = DATA
    ..mentionType = MentionType.users   // This could be either MentionType.users or MentionType.channel.
    ..mentionedUserIds = ['Jeff', 'Julia']
    ..metaArrays = [
        MessageMetaArray(key: 'itemType', value: ['tablet']),
        MessageMetaArray(key: 'quality', value: ['best', 'good'])
    ]
    ..targetLanguages = ['fr', 'de']    // French and German, respectively.
    ..pushOption = PushNotificationDeliveryOption.normal;

try {
    final preMessage = openChannel.sendUserMessage(params: params, onCompleted: (message, error) {
        If (error != null) {
            // Handle error.
        } else {
            // A text message with detailed configuration is successfully sent to the channel.
            // By using userMessage.messageId, userMessage.message or userMessage.customType,
            // you can access the result object from the Sendbird server to check your UserMessageParams configuration.
            // The current user can receive messages from other users through the onMessageReceived() method of an event handler.
        }
    });
} catch (e) {
    // Handle error.
}

Group channel

try {
    final params = UserMessageParams(message: 'message')
        ..customType = 'custom'
        ..mentionType = MentionType.users
        ..mentionUserIds = ['Jeff', 'Julia']
        ..metaArrays = [
            MessageMetaArray(key: 'itemType', values: ['tablet']),
            MessageMetaArray(key: 'quality', values: ['best', 'good'])
        ]
        ..targetLanguages = ['fr', 'de']
        ..pushNotificationDeliveryOption = PushNotificationDeliveryOption.normal;

    final preMessage = groupChannel.sendUserMessage(params, onCompleted:(message, err) {
    });
    // Use preMessage to display the message before it is sent to the server.
} catch (e) {
    // Handle error.
}

File message

A user can also send a binary file through the Chat SDK as the file itself or through sending 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 isn't 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(), which is another method that allows you to send up to 20 file messages per one method call.

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

Through the callback handler of the sendFileMessage() 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.

Open channel

try {
    // Send a file message with a raw file.
    final params = FileMessageParams.withFile(
            FILE,
            name: FILE_NAME,
            size: FILE_SIZE,
            mimeType: MIME_TYPE,
        )
        ..thumbnailSizes =  [Size(100, 100), Size(40, 40)];
        ..customType = CUSTOM_TYPE
        ..mentionType = MentionType.users       // This could be either MentionType.users or MentionType.channel.
        ..mentionedUserIds = ['Jeff', 'Julia']
        ..pushOption = PushNotificationDeliveryOption.normal;
        // Acceptable values are normal, which is the default, and suppress.

    final preMessage = await openChannel.sendFileMessage(params: params, onCompleted: (message, error) {
        If (error != null) {
            // Handle error.
        }
        // A file message with detailed configuration is successfully sent to the channel.
        // By using fileMessage.messageId, fileMessage.fileName or fileMessage.customType,
        // you can access the result object from the Sendbird server to check your FileMessageParams configuration.
        // The current user can receive messages from other users through the onMessageReceived() method of a channel event handler.
    });
} catch (e) {
    // Handle error.
}

Group channel

try {
    final params = FileMessageParams.withFile(file, name: ‘file_name’)
        ..thumbnailSizes =  [Size(100, 100), Size(200, 200)]
        ..customType = 'custom'
        ..mentionType = MentionType.users
        ..mentonUserIds = ['Jeff', 'Julia']
        ..pushNotificationDeliveryOption = PushNotificationDeliveryOption.normal;

    final preMessage = groupChannel.sendFileMessage(params, onCompleted: (message, err) {
        // A file message with detailed configuration is successfully sent to the channel.
    });
    // Use preMessage to display the message before it is sent to the server.
} catch (e) {
    // Handle error.
}