Viewing Messages

Text Message

The following method will return a live object of a specific AmityMessage based on messageId. Since it is a live object, remember to observe its changes on top of the token.

var token: AmityNotificationToken?

...

let messageId = "12345"
let messagesObject = messageRepository.getMessage(messageId)
token = messageObject?.observe { messageObject, error in
    if let message = messageObject.object {
         // Handle message result.
    } else {
         // Handle error.
    }
}

The getMessage(messageId) method will get a single message with the specified messageId. To get a collection of messages in a channel, refer to Querying Messages.

Image Message

Images in messages are not downloaded automatically when the client receives a message. In order to save bandwidth, images should be downloaded only when they're about to be displayed. A typical implementation to trigger the download would be in the tableView:cellForRowAtIndexPath: method. You can download an image using downloadImage: or downloadImageAsData: method from the AmityFileRepository class. You need to pass the String instance and the method provides the appropriate image url depending upon the sync state of message.

let fileRepository = AmityFileRepository(client: client)

...

func downloadImageAsURL(from message: AmityMessage) {
    guard let imageInfo = message.getImageInfo() else { return }

    // Download from url and return saved image url.
    fileRepository.downloadImage(fromURL: imageInfo.fileURL, size: .medium) { imageUrl, error in
        // Handle image url and error.
    }
}

func downloadImageAsUIImage(from message: AmityMessage) {
    guard let imageInfo = message.getImageInfo() else { return }
      
    // Download from url and return image.
    fileRepository.downloadImageAsData(fromURL: imageInfo.fileURL, size: .small) { image, size, error in
        // Handle image and error.
    }
}

Please use the appropriate image size depending on your use-case in order to save bandwidth.

  • small is used for image thumbnails, with a maximum image size of 160 pixels per dimension. For example, this should be used for small previews in an image gallery that displays a large amount of images in a grid.

  • medium is used for standard image display, with a maximum image size of 600 pixels per dimension. It's sized to take up about half of a mobile phone's screen size.

  • large is used for full screen image display, with a maximum image size of 1500 pixels per dimension.

  • full is used to get the original image. This size is only valid if the image is uploaded with the property fullImage set to YES. If a full sized image is not available, a large sized image will be returned instead.

The SDK do not cache image.

File/Audio Message

AmityFileRepository provides downloadFile: and downloadFileAsData: method to download the file from URL. The same method can be used to download the audio.

func downloadFileAsURL(from message: AmityMessage) {
    guard let imageInfo = message.getFileInfo() else { return }

    // Download from url and return saved file url.
    fileRepository.downloadFile(fromURL: imageInfo.fileURL) { fileUrl, error in
        // Handle file url.
    }
}

func downloadFileAsData(from message: AmityMessage) {
    guard let imageInfo = message.getFileInfo() else { return }

    // Download from url and return data.
    fileRepository.downloadFileAsData(fromURL: imageInfo.fileURL) { data, error in
        // Handle data.
    }
}swif

Last updated