Use Rich Message

Message is more than text. Rich content (such as image, video, audio, etc.) is very important for user experience. Magnet Message has built-in support for rich message by using attachments. The following illustrates how you can send rich message using our APIs.

Send Message with Attachment

The following use case describes how to send a message with an attachment.

// Construct the message let content = ["foo": "bar"] let attachment = MMAttachment(fileURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("test_image", ofType: "jpeg")), mimeType: "image/jpeg", name: "test image", description: "testing rich message") let message = MMXMessage(toRecipients: [currentUser], messageContent: content) message.addAttachment(attachment) // Send as in-app message message.sendWithSuccess({ _ in //Message sent successfully }, failure: { error in //Failed to send message }) // Send message to a channel let channel: MMXChannel = ... channel.publishMessage(message, success: { //Message sent successfully }, failure: { error in //Failed to send message })

Receive Message with Attachment

The following use case describes how to receive a message with an attachment.

// Message received by registering for MMXDidReceiveMessageNotification notification let message: MMXMessage = ... if let attachments = message.attachments { for attachment in attachments { } }

Consume Attachment

There are several ways to consume the attachment:

  1. Use as URL resource:

    // Load the image attachment into an UIImageView for example // attachment.downloadURL contains the image URL

  2. Download as NSData:

    attachment.downloadDataWithSuccess({ data in }, failure: { error in

  3. Download to File:

    // Download to a specific file let someFile: NSURL = ... attachment.downloadToFile(someFile, success: { }, failure: { error in }) // Download to tmp file generated by the SDK attachment.downloadFileWithSuccess({ fileURL in }, failure: { error in })

  4. Download to InputStream:

    attachment.downloadInputStream({ inputStream, length in //NOTE : The content InputStream should be consumed in a backgroud thread }, failure: { error in })

Customizable Payloads

Magnet Message allows you to send full objects or "payloads" in your message if desired. Any message can have a payload attached to it. Currently, Magnet message only supports attaching one payload at a time. However, you can create a wrapper object that contains an array of MMModels and easily send the array in the payload. Payloads are subclasses of the class MMModel that adhere to the MMXPayload protocol.

For example, here is a toy car class:

class ToyCar: MMModel, MMXPayload { //ContentType MMXPayload protocol static var contentType: String { return "ToyCar" } var pushConfig: String? { return ToyCar.contentType } var name = "" var price = 5.0 }

As you may notice, this class inherits MMModel and implements the MMXPayload protocol.

Register Object to Attach it to Message

There is only one thing left to do to make this send-able inside a message. The toy car class must be registered with Magnet Message so it is automatically serialized and deserialized. Add this code to the app delegate's didFinishLaunchingWithOptions method:

MMXPayloadRegister.registerClassForPayloads(ToyCar.self)

Send Object in Message

Now you can send this object inside a message:

let car = ToyCar()! car.name = "MatchBox" car.price = 2.5 let content = ["message": "Check out my new car object!"] let message = MMXMessage(toRecipients: [MMUser.currentUser()!], messageContent: content) // all you have to do it add it here! message.payload = car channel.publishMessage(message, success: { //Message sent successfully }, failure: { error in //Failed to send message })

Listen to Messages and Deserialize Object

We can use MMXNotificationCenter to listen to incoming messages and then easily deserialize the object from the MMXMessage:

MMXNotificationCenter.center.add(context: self, forChannel: nil) { message in if let car = message.payload as? ToyCar { print("name: \(car.name) price: \(car.price)"); } MMXNotificationCenter.center.remove(context: self) }


Next Steps

See how to create a Public Forum in your app.