Publishes a message to a topic. After the message is published, Simple Message Queue (formerly MNS) pushes it to the specified endpoints.
Request syntax
POST /topics/$TopicName/messages HTTP/1.1Replace $TopicName with the name of the target topic.
Request headers
This operation uses common request headers only. For more information, see Common parameters.
Request body
The request body is an XML message with the following elements.
|
Parameter |
Type |
Required |
Description |
|
MessageBody |
String |
Yes |
The message content. |
|
MessageTag |
String |
No |
The message tag. |
|
MessageAttributes |
Object |
Yes |
The push subscription type. Valid values:
|
The following section describes the parameters of MessageAttributes:
-
If you want to push messages to a mailbox, MessageAttributes must include the DirectMail property.
The value of the DirectMail property is in the JSON format and must include the following parameters. For more information, see SingleSendMail.
Parameter
Type
Required
Sample value
Description
AccountName
String
Yes
direct_mail_account_name****@aliyun-inc.com
The sender address.
Subject
String
Yes
TestMailSubject
The email subject.
AddressType
Number
Yes
0
The address type.
Valid values:
-
0: Random account.
-
1: Sender address.
IsHtml
Number
Yes
0
Specifies whether the email body is in HTML format.
Valid values:
-
0: The email body is not in HTML format.
-
1: The email body is in HTML format.
ReplyToAddress
Boolean
Yes
0
Specifies whether to enable the reply-to address feature.
Valid values:
-
0: Disable the reply-to address feature.
-
1: Enable the reply-to address feature.
-
-
If you want to push messages by SMS, MessageAttributes must include the DirectSMS property.
The value of the DirectSMS property is in the JSON format and must include the following parameters.
Parameter
Type
Required
Example
Description
FreeSignName
String
Yes
$value
The SMS signature.
TemplateCode
String
Yes
$value
The ID of the SMS template.
Type
String
Yes
multiContent
Specifies whether to send text messages in batches.
Valid values:
-
singleContent: Send individually.
-
multiContent: Send in batches.
NoteIf you set Type to multiContent, the message can be processed only by subscriptions whose endpoints are `sms:directsms:anonymous`.
Receiver
String
No
$num1,$num2
The recipient.
This parameter is valid and required only if you set Type to singleContent and the endpoint of the subscription is `sms:directsms:anonymous`. Enter the mobile phone numbers of the recipients. Separate multiple numbers with commas (,).
SmsParams
String
Yes
{\"$num1\": {\"$key1\": \"$value1\", \"$key2\": \"$value2\"}, \"$num2\": {\"$key1\": \"$value1\", \"$key2\": \"$value2\"}}The key-value pairs of the SMS template, in JSON format.
-
If you set Type to singleContent, use the following format for this parameter.
{ "Parameter1":"Value1", "Parameter2":"Value2" }.
-
If you set Type to multiContent, use the following format for this parameter.
{ "PhoneNumber1":{ "Parameter1":"Value1", "Parameter2":"Value2" }, "PhoneNumber2":{ "Parameter1":"Value3", "Parameter2":"Value4" } }
-
-
If you want to push messages by Mobile Push, MessageAttributes must include the Push property.
The value of the Push property is in the JSON format and must include the following parameters. For more information, see Advanced push operations.
Parameter
Type
Required
Example
Description
Target
String
Yes
ALL
The push target type.
Valid values:
-
DEVICE: Push by device.
-
ACCOUNT: Push by account.
-
ALIAS: Push by alias.
-
TAG: Push by tag.
-
ALL: Push to all devices. The interval between two consecutive pushes to all devices of the same type must be at least 1 second.
TargetValue
String
Yes
ALL
The push target.
DeviceType
String
Yes
ALL
The device type.
Valid values:
-
iOS: iOS device.
-
ANDROID: Android device.
-
ALL: All devices.
PushType
String
Yes
MESSAGE
The push type.
Valid values:
-
MESSAGE: Message.
-
NOTICE: Notification.
Title
String
Conditionally. This parameter is required for Android pushes and for iOS message pushes. It is optional for iOS notification pushes.
JavaSDK
Title
Body
String
Yes
Hello From JavaSDK
The message content.
PushTime
String
No
None
The scheduled push time. If you do not set this parameter, the message is pushed immediately.
StoreOffline
Boolean
No
None
Specifies whether to store the message for offline push. The default value is false.
Valid values:
-
true: Store the message for offline push.
-
false: Do not store the message for offline push.
-
The XML namespace must be http://mns.aliyuncs.com/doc/v1/. Requests that omit or use an incorrect namespace return an error.
Response parameters
| Parameter | Type | Example | Description |
|---|---|---|---|
| MessageId | String | D273CD6A89564E54-1-15180395A19-20000\*\*\*\* | A unique identifier for the message within the topic. |
| MessageBodyMD5 | String | 3AC6DD36D2D6B7283F2F490A0975\*\*\*\* | The MD5 hash of the message body. Compare this value against a locally computed hash to verify message integrity. |
Sample success response
Sample request for a Direct Mail push
POST /topics/MyTopic/messages HTTP/1.1
Host: 123456789098****.mns.cn-hangzhou.aliyuncs.com
Date: Tue, 08 Dec 2015 06:13:40 GMT
Content-Length:500
Content-Type:text/xml;charset=utf-8
Authorization: MNS 15B4D3461F177624****:aibzWu1iDEx9LwO56+kHgA3e****
x-mns-version: 2015-06-06
<?xml version="1.0" encoding="utf-8"?>
<Message xmlns="http://mns.aliyuncs.com/doc/v1/">
<MessageBody>Base64 Encoded Result</MessageBody>
<MessageTag>important</MessageTag>
<MessageAttributes>
<DirectMail>
{
"Subject":"TestMailSubject",
"AccountName":"direct_mail_account_name****@aliyun-inc.com",
"ReplyToAddress":0,
"AddressType":0,
"IsHtml":0
}
</DirectMail>
</MessageAttributes>
</Message> Sample request for an SMS push
POST /topics/MyTopic/messages HTTP/1.1
Host: 123456789098****.mns.cn-hangzhou.aliyuncs.com
Date: Tue, 08 Dec 2015 06:13:40 GMT
Content-Length:500
Content-Type:text/xml;charset=utf-8
Authorization: MNS 15B4D3461F177624****:aibzWu1iDEx9LwO56+kHgA3e****
x-mns-version: 2015-06-06
<?xml version="1.0" encoding="utf-8"?>
<Message xmlns="http://mns.aliyuncs.com/doc/v1/">
<MessageBody>content</MessageBody>
<MessageAttributes>
<DirectSMS>
{
"FreeSignName":"$value",
"TemplateCode":"$value",
"Type":"multiContent",
"Receiver":"$num1,$num2",
"SmsParams":"{\"$num1\": {\"$key1\": \"$value1\", \"$key2\": \"$value2\"}, \"$num2\": {\"$key1\": \"$value1\", \"$key2\": \"$value2\"}}"
}
</DirectSMS>
</MessageAttributes>
</Message> Sample request for a Mobile Push
POST /topics/MyTopic/messages HTTP/1.1
Host: 123456789098****.mns.cn-hangzhou.aliyuncs.com
Date: Tue, 08 Dec 2015 06:13:40 GMT
Content-Length:500
Content-Type:text/xml;charset=utf-8
Authorization: MNS 15B4D3461F177624****:aibzWu1iDEx9LwO56+kHgA3e****
x-mns-version: 2015-06-06
<?xml version="1.0" encoding="UTF-8"?>
<Message xmlns="http://mns.aliyuncs.com/doc/v1">
<MessageBody>hello topic</MessageBody>
<MessageAttributes>
<Push>
{
"Target":"ALL",
"TargetValue":"ALL",
"DeviceType":"ALL",
"PushType":"MESSAGE",
"Title":"JavaSDK",
"Body":"Hello From JavaSDK",
"AndroidOpenType":"APPLICATION"
}
</Push>
</MessageAttributes>
</Message> Sample success response
HTTP/1.1 201 Created
Content-Length:120
Content-Type:text/xml;charset=utf-8
x-mns-request-id:56667514B2B71C9C1600****
x-mns-version:2015-06-06
<?xml version="1.0" encoding="utf-8"?>
<Message xmlns="http://mns.aliyuncs.com/doc/v1/">
<MessageId>D273CD6A89564E54-1-15180395A19-20000****</MessageId>
<MessageBodyMD5>3AC6DD36D2D6B7283F2F490A0975****</MessageBodyMD5>
</Message>Error codes
For errors common to all operations, see Common parameters.
The following table lists the error codes specific to the PublishMessage operation.
| HTTP status code | Error code | Error message | Description |
|---|---|---|---|
| 404 | TopicNotExist | The topic you provided does not exist. | The topic name in the request URI does not match any existing topic. Verify the topic name. |