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
HashMap<String, String> content = new HashMap<String, String>();
content.put("foo", "bar");
Attachment attachment = new Attachment(getContext().getResources().openRawResource(
com.magnet.mmx.test.R.raw.test_image), "image/jpeg",
"test image", "testing rich message");
MMXMessage message = new MMXMessage.Builder()
.recipients(recipients)
.content(content)
.attachments(attachment)
.build();
// Send as in-app message
String messageId = message.send(new MMXMessage.OnFinishedListener<String>() {
public void onSuccess(String result) {
//Message sent successfully
}
public void onFailure(MMXMessage.FailureCode code, Throwable ex) {
//Failed to send message
}
});
// Send message to a channel
MMXChannel channel = ...;
channel.publish(message, new MMXChannel.OnFinishedListener<String>() {
public void onSuccess(String result) {
//Message sent successfully
}
public void onFailure(MMXMessage.FailureCode code, Throwable ex) {
//Failed to send message
}
});
Receive Message with Attachment
The following use case describes how to receive a message with an attachment.
// Message received from MMX.EventListener
MMXMessage message = ...;
for(Attachment attachment : message.getAttachments()) {
}
Consume Attachment
There are several ways to consume the attachment:
Use as URL resource:
// Load the image attachment to ImageView with Picasso
Picasso.with(context).load(attachment.getDownloadUrl()).into(imageView);
Download to byte array:
attachment.download(new Attachment.DownloadAsBytesListener() {
@Override public void onComplete(byte[] content) {
}
@Override public void onError(Throwable throwable) {
}
});
Download to File:
// Download to a specific path
attachment.download("data/attachments/foo.png", new Attachment.DownloadAsFileListener() {
@Override public void onComplete(File content) {
}
@Override public void onError(Throwable throwable) {
}
});
// Download to a specific file
File someFile = ...
attachment.download(someFile, new Attachment.DownloadAsFileListener() {
@Override public void onComplete(File content) {
}
@Override public void onError(Throwable throwable) {
}
});
// Download to tmp file generated by the SDK
attachment.download(new Attachment.DownloadAsFileListener() {
@Override public void onComplete(File content) {
}
@Override public void onError(Throwable throwable) {
}
});
Download to InputStream:
attachment.download(new Attachment.DownloadAsStreamListener() {
@Override public void onComplete(InputStream content) {
//NOTE : The content InputStream should be consumed in a backgroud thread
}
@Override public void onError(Throwable throwable) {
}
});
Next Steps
See how to create a Public Forum in your app.