Skip to main content
Version: 5.9.0

Messages and unread counter

Programmatic message sending

The send(message:) method sends a message to an active chat without user interaction — for example, an order context or the current location.

func send(message: ChatMessage) throws(ChatCenterUISendMessageError)

Parameters:

  • message: ChatMessage — the message model to send.

Throws: ChatCenterUISendMessageError — see Error reference.

Usage example:

do {
// Sending a text message
try chatCenterSDK.send(message: .text("Order #12345 information"))
} catch {
print("Send error: \(error)")
}

Message types (ChatMessage)

TypeDescriptionConstraints
.text(String)Text messageUp to 4000 characters
.image(UIImage)ImageFile size limit (configured server-side)
.location(CGPoint)User location (system)Resending the same point is ignored. x is latitude, y is longitude. The semantics are aligned with the edna server side; the SDK itself does not interpret the values.

Examples:

do {
// Text
try chatCenterSDK.send(message: .text("Hello"))

// Image
if let image = UIImage(named: "screenshot") {
try chatCenterSDK.send(message: .image(image))
}

// Location
try chatCenterSDK.send(message: .location(CGPoint(x: 55.7558, y: 37.6173)))
} catch {
print("Send error: \(error)")
}

Long text messages

.messageTooLong is thrown when trying to send text longer than 4000 characters. On the application side, plan for the following:

  1. Pre-flight check before send — trim or ask the user to shorten the text:

    let maxLength = 4000

    func sendUserText(_ text: String) {
    guard text.count <= maxLength else {
    showAlert("Message is longer than \(maxLength) characters — shorten it or split it into several")
    return
    }
    do {
    try chatCenterSDK.send(message: .text(text))
    } catch {
    handleSendError(error)
    }
    }
  2. Splitting into parts — if splitting is acceptable for UX, split along sentence boundaries rather than character by character. Each part is sent as a separate send(message:) call and arrives to the operator as an independent message — neither the SDK nor the server merge parts or add "1/N" markers.

In the chat screen UI, the SDK trims input longer than 4000 characters starting with iOS 5.6.0 (see Migration to 5.6.0) — but for programmatic sending via send(message:), the client code must enforce the limit.

Send errors

send(message:) throws ChatCenterUISendMessageError. The full list of cases, handling recommendations, and a catch example are in Error reference.

For control over WebSocket behavior (when .webSocketNotActive can occur), see SDK configuration.


Unread message counter

func getUnreadMessagesCount(completion: @escaping (Result<Int, Error>) -> Void) throws(ChatCenterUIError)

Requests the number of unread messages from the server.

Throws: ChatCenterUIError.userNotAuthorized if the user is not authorized.

Usage example:

do {
try chatCenterSDK.getUnreadMessagesCount { [weak self] result in
switch result {
case let .success(count):
// chatButton is a UI element of the parent application
self?.chatButton.badge = count > 0 ? "\(count)" : nil
case let .failure(error):
print("Error getting counter: \(error)")
}
}
} catch {
print("User is not authorized: \(error)")
}
Automatic counter updates

For automatic counter updates, use ChatConfig.unreadMessageCountDelay (Int, seconds; 0 disables it) or subscribe to the delegate ChatCenterUISDKDelegate.chatCenterUI(chatCenter:didChangeUnreadMessages:).