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

Receive messages in a group channel

Copy link

Messages sent by other group channel members can be received through the messageCollection(_:context:channel:addedMessages:) method in message collection delegates.


Message type

Copy link

Each BaseMessage object received in a group channel falls into one of the following three message types.

Message typeClassDescription

Text

UserMessage

A text message sent by a user.

File

FileMessage

A binary file message sent by a user.

File

MultipleFileMessage

A message with multiple files sent by a user.

Admin

AdminMessage

A text message sent by an admin through the Platform API.

File message and multiple files message

Copy link

When receiving a message payload with file data, the SDK can distinguish FileMessage and MultipleFilesMessages based on the number of files included in files. If the message contains more than two files, the SDK acknowledges it as a multipleFilesMessage. If the number of files contained in a messages exceeds the limit, which is 30, the message fails and returns an error of 800110. If any of the files exceeds the file size limit, it returns 800260.


Event delegate

Copy link

When a new message arrives in a group channel, messageCollection(_:context:channel:addedMessages:) and messageCollection(_:context:updatedChannel:) in the message collection are called.

Whenever an event related to message or group channel occurs, the collection is informed of the event through MessageContext and ChannelContext, respectively. The MessageContext and ChannelContext instances have CollectionEventSource, which contains the information of the event that took place. For example, when a new message is received, CollectionEventSource.eventMessageReceived is passed as a MessageEventContext.source value to MessageCollectionDelegate.messageCollection(_:context:channel:addedMessages:). Meanwhile, when a message is delivered to group channel members who are online, the message's delivery status is automatically marked as delivered. The value for GroupChannelEventContext.source is set to CollectionEventSource.eventDeliveryStatusUpdated and channel members are notified of the event through MessageCollectionDelegate.messageCollection(_:context:updatedChannel:). To learn more about this topic, see the Message events section in the Message collection page.

You can also register other message-related event delegate by setting the delegate property in the MessageCollection class.

class MessageChannelChattingViewController: UIViewController, MessageCollectionDelegate {

    func addDelegate() {
        messageCollection.delegate = self
    }

    func messageCollection(_ collection: MessageCollection, context: MessageContext, channel: GroupChannel, addedMessages: [BaseMessage]) {
       
    }
     
    func messageCollection(_ collection: MessageCollection, context: MessageContext, channel: GroupChannel, updatedMessages: [BaseMessage]) {
       
    }
     
    func messageCollection(_ collection: MessageCollection, context: MessageContext, channel: GroupChannel, deletedMessages: [BaseMessage]) {
       
    }
     
    func messageCollection(_ collection: MessageCollection, context: MessageContext, updatedChannel: GroupChannel) {
       
    }
     
    func messageCollection(_ collection: MessageCollection, context: MessageContext, deletedChannel channelURL: String) {
       
    }
     
    func didDetectHugeGap(_ collection: MessageCollection) {
       
    }
}

When the message UI is no longer needed, call dispose(). Note that the delegate doesn't work when the message collection is disposed.

class ChatViewController: UIViewController {
    // ...

    func dispose() {
        self.collection?.dispose()
    }

    // ...
}