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 NSDictionary *content = @{@"foo": @"bar"}; MMAttachment *attachment = [[MMAttachment alloc] initWithFileURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test_image" ofType:@"jpeg"]] mimeType:@"image/jpeg" name:@"test image" description:@"testing rich message"]; MMXMessage *message = [MMXMessage messageToRecipients:recipients messageContent:content]; [message addAttachment:attachment]; // Send as in-app message [message sendWithSuccess:^(NSSet<NSString *> * _Nonnull invalidUsers) { //Message sent successfully } failure:^(NSError * _Nonnull error) { //Failed to send message }]; // Send message to a channel MMXChannel *channel = ...; [channel publishMessage:message success:^{ //Message sent successfully } failure:^(NSError * _Nonnull error) { //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 MMXMessage *message = ...; for (MMAttachment *attachment in message.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:^(NSData * _Nonnull data) { } failure:^(NSError * _Nonnull error) { }];

  3. Download to File:

    // Download to a specific file NSURL *someFile = ...; [attachment downloadToFile:someFile success:^{ } failure:^(NSError * _Nonnull error) { }]; // Download to tmp file generated by the SDK [attachment downloadFileWithSuccess:^(NSURL * _Nonnull fileURL) { } failure:^(NSError * _Nonnull error) { }]

  4. Download to InputStream:

    [attachment downloadInputStreamWithSuccess:^(NSInputStream * _Nonnull inputStream, int64_t length) { } failure:^(NSError * _Nonnull error) { }];

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 containing an array of MMModels and easily send an 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:

@interface ToyCar: MMModel<MMXPayload> @property(copy, nullable) NSString *name; @property(assign) double price; //ContentType MMXPayload protocol @property (nonatomic, readonly, copy) NSString * _Nullable pushConfig; @end @implementation ToyCar //ContentType MMXPayload protocol + (NSString * _Nonnull)contentType { return @"ToyCar"; } - (NSString *)pushConfig { return [ToyCar contentType]; } @end

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.class];

Send Object in Message

Now you can send this object inside a message:

ToyCar *car = [[ToyCar alloc] init]; = @"MatchBox"; car.price = 2.5; NSDictionary<NSString *, NSString *>* content = @{@"message": @"Check out my new car object!"}; MMXMessage *message = [MMXMessage messageToRecipients:[NSSet setWithObject:[MMUser currentUser]] messageContent:content]; message.payload = car; // all you have to do it add it here! [channel publishMessage:message success:^{ //Message sent successfully } failure:^(NSError * error) { //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 defaultCenter] addWithContext:self forChannel:nil onReceive:^(MMXMessage * message) { if ([message.payload isKindOfClass: ToyCar.class]) { ToyCar *car = (ToyCar *)message.payload; NSLog(@"name: %@ price: %f",, car.price); } [[MMXNotificationCenter defaultCenter] removeWithContext: self]; }];

Next Steps

See how to create a Public Forum in your app.