Publish topic messages with message attributes
Message attributes let you override delivery details at publish time -- for example, changing the recipient phone number for a Short Message Service subscription or the recipient email address for a Direct Mail subscription -- without modifying the subscription itself.
This page provides a Java SDK example for both use cases.
How message attributes work
When you publish a message to a topic, the topic pushes that message to every subscriber. By default, each subscriber receives messages at the endpoint defined during subscription creation. Message attributes override these defaults per publish call:
Short Message Service subscriptions -- Override the recipient phone number.
Direct Mail subscriptions -- Override the recipient email address, email subject, and body format (plain text or HTML).
Message attributes are optional. Without them, the topic delivers messages using each subscriber's original endpoint configuration.
Prerequisites
Before you begin, make sure that you have:
Java SDK 1.4.0 or later installed. For details, see Install the SDK
An endpoint and credentials configured. For details, see Configure endpoint and credentials
Example code
This example publishes two messages to a topic named TestTopic: one with Short Message Service attributes and one with Direct Mail attributes. Both methods use a shared publishMessageWithAttributes helper that handles the publish call and prints the result.
package com.aliyun.mns.sample.topic;
import com.aliyun.mns.client.CloudAccount;
import com.aliyun.mns.client.CloudTopic;
import com.aliyun.mns.client.MNSClient;
import com.aliyun.mns.common.utils.ServiceSettings;
import com.aliyun.mns.model.DmAttributes;
import com.aliyun.mns.model.DysmsAttributes;
import com.aliyun.mns.model.MessageAttributes;
import com.aliyun.mns.model.RawTopicMessage;
import com.aliyun.mns.model.TopicMessage;
public class PublishMessageWithAttributesDemo {
public static void main(String[] args) {
String topicName = "TestTopic";
String body = "hello world!";
CloudAccount account = new CloudAccount(ServiceSettings.getMNSAccountEndpoint());
// this client need only initialize once
MNSClient client = account.getMNSClient();
publishDysmsMessage(client, topicName, body);
publishDmMessage(client, topicName, body);
client.close();
}
/**
* Publishes a message with Short Message Service attributes.
* Overrides the recipient phone number defined in the subscription.
*/
private static void publishDysmsMessage(MNSClient client, String topicName, String body) {
CloudTopic topic = client.getTopicRef(topicName);
RawTopicMessage message = new RawTopicMessage();
message.setMessageBody(body);
// Dynamically modify the mobile phone number that receives messages from the Short Message Service subscriber.
DysmsAttributes dysmsAttributes = new DysmsAttributes();
dysmsAttributes.setPhoneNumber("1350000xxxx");
MessageAttributes attributes = new MessageAttributes();
attributes.setDysmsAttributes(dysmsAttributes);
publishMessageWithAttributes(topic, message, attributes);
}
/**
* Publishes a message with Direct Mail attributes.
* Overrides the recipient address, subject, and body format
* defined in the subscription.
*/
private static void publishDmMessage(MNSClient client, String topicName, String body) {
CloudTopic topic = client.getTopicRef(topicName);
RawTopicMessage message = new RawTopicMessage();
message.setMessageBody(body);
DmAttributes dmAttributes = new DmAttributes();
// Dynamically modify the email address that receives messages from the Direct Mail subscriber.
dmAttributes.setMailAddress("test@example.com");
// Dynamically modify the subject of the email from the Direct Mail subscriber.
dmAttributes.setSubject("test subject");
// Dynamically set the email body format for the Direct Mail subscriber to HTML. If not specified, the default is plain text.
dmAttributes.setIsHtml(true);
MessageAttributes attributes = new MessageAttributes();
attributes.setDmAttributes(dmAttributes);
publishMessageWithAttributes(topic, message, attributes);
}
private static void publishMessageWithAttributes(CloudTopic topic, RawTopicMessage message, MessageAttributes attributes) {
try {
TopicMessage publishResultMsg = topic.publishMessage(message, attributes);
System.out.println("message publish.");
System.out.println("reqId:" + publishResultMsg.getRequestId());
System.out.println("msgId:" + publishResultMsg.getMessageId());
System.out.println("md5:" + publishResultMsg.getMessageBodyMD5());
} catch (Exception e) {
e.printStackTrace();
System.out.println("publish message error.");
}
}
}Key classes and methods
| Class | Purpose |
|---|---|
MessageAttributes | Container for channel-specific attribute objects (DysmsAttributes, DmAttributes). Pass it to topic.publishMessage() alongside the message body. |
DysmsAttributes | Defines Short Message Service overrides. Call setPhoneNumber() to specify the recipient phone number. |
DmAttributes | Defines Direct Mail overrides. Call setMailAddress(), setSubject(), and setIsHtml() to control the recipient address, subject line, and body format. |
Response fields
A successful publishMessage() call returns a TopicMessage object with these fields:
| Field | Description |
|---|---|
getRequestId() | Unique request ID for troubleshooting. |
getMessageId() | ID assigned to the published message. |
getMessageBodyMD5() | MD5 hash of the message body for content integrity verification. |