Publishing a Message
Function
Publish messages to a topic. After the message ID is returned, the message has been saved and is to be pushed to the subscribers of the topic. Three message formats are supported:
- message
- message_structure
- message_template_name
You only need to specify one of the three message formats. If you specify all of them at the same time, they take effect in the following sequence: message_structure > message_template_name > message.
For e-mail and SMS messages, if an account sends more than 1,000 messages in 10 minutes, the excessive messages may be lost.
URI
POST /v2/{project_id}/notifications/topics/{topic_urn}/publish
|
Parameter |
Mandatory |
Type |
Description |
|---|---|---|---|
|
project_id |
Yes |
String |
Specifies the project ID. |
|
topic_urn |
Yes |
String |
Specifies the resource identifier of the topic, which is unique. To obtain the resource identifier, see Querying Topics. |
Request Parameters
|
Parameter |
Mandatory |
Type |
Description |
|---|---|---|---|
|
X-Auth-Token |
Yes |
String |
Specifies the user token. It can be obtained by calling the IAM API (value of X-Subject-Token in the response header). |
|
Parameter |
Mandatory |
Type |
Description |
|---|---|---|---|
|
subject |
No |
String |
Specifies the message subject, which is used as the email subject when you publish email messages. The length of the subject cannot exceed 512 bytes. |
|
message |
No |
String |
Specifies the message content. The content is a UTF-8-coded character string of no more than 256 KB. If the endpoint is a phone number that exceeds 490 characters, SMN will cut it off. The SMS message cannot contain square brackets ([]). For application subscribers, messages must comply with format requirements of the mobile push platform. For details, see the format of the application message body. Otherwise, the mobile app will fail to receive messages. HMS, APNS, and APNS_SANDBOX mobile push platforms are supported.
NOTE:
At least one of the three parameters: message, message_structure, and message_template_name, must be set. If all of them are set, the setting takes effect in the following sequence: message_structure > message_template_name > message |
|
message_structure |
No |
String |
Specifies the message structure, which contains JSON character strings. Specify protocols in the structure, which can be email, sms, http, https, dms, functiongraph, functionstage, HMS, APNS, and APNS_SANDBOX. For details about HMS, APNS, and APNS_SANDBOX message formats, see Application Message Format. The default protocol is mandatory. If the system fails to match any other protocols, the default message is sent.
NOTE:
At least one of the three parameters: message, message_structure, and message_template_name, must be set. If all of them are set, the setting takes effect in the following sequence: message_structure > message_template_name > message |
|
message_template_name |
No |
String |
Specifies the message template name, which can be obtained according to Querying the Message Template List. Three message formats are supported:
You must specify one of the three message formats. If you specify all of them at the same time, they take effect in the following sequence: message_structure > message_template_name > message. |
|
tags |
No |
Map<String,String> |
Specifies the dictionary consisting of variable parameters and values. The value cannot be left blank. This parameter is mandatory when you use a message template to publish messages. The key in the dictionary is the parameter name in the message template and contains a maximum of 21 characters. The value in the dictionary is the value after the key in the message template is replaced. The value cannot exceed 1 KB. |
|
time_to_live |
No |
String |
Specifies the maximum retention period of a message in SMN. After the retention period expires, SMN does not send this message. Unit: second Default value: 3600s (one hour) The retention period must be a positive integer less than or equal to 86,400 (3600 x 24). |
Response Parameters
Status code: 200
|
Parameter |
Type |
Description |
|---|---|---|
|
request_id |
String |
Specifies the request ID, which is unique. |
|
message_id |
String |
Specifies the message ID, which is unique. |
Status code: 400
|
Parameter |
Type |
Description |
|---|---|---|
|
request_id |
String |
Specifies the request ID, which is unique. |
|
code |
String |
Specifies the error code. |
|
message |
String |
Describes the error message. |
Status code: 403
|
Parameter |
Type |
Description |
|---|---|---|
|
request_id |
String |
Specifies the request ID, which is unique. |
|
code |
String |
Specifies the error code. |
|
message |
String |
Describes the error message. |
Status code: 404
|
Parameter |
Type |
Description |
|---|---|---|
|
request_id |
String |
Specifies the request ID, which is unique. |
|
code |
String |
Specifies the error code. |
|
message |
String |
Describes the error message. |
Status code: 429
|
Parameter |
Type |
Description |
|---|---|---|
|
request_id |
String |
Specifies the request ID, which is unique. |
|
code |
String |
Specifies the error code. |
|
message |
String |
Describes the error message. |
Status code: 500
|
Parameter |
Type |
Description |
|---|---|---|
|
request_id |
String |
Specifies the request ID, which is unique. |
|
code |
String |
Specifies the error code. |
|
message |
String |
Describes the error message. |
Example Requests
- Publishing a message
POST https://{SMN_Endpoint}/v2/{project_id}/notifications/topics/urn:smn:regionId: f96188c7ccaf4ffba0c9aa149ab2bd57:test_create_topic_v2/publish { "subject" : "test message v2", "message" : "Message test message v2", "time_to_live" : "3600" } - Publishing messages using message structure
POST https://{SMN_Endpoint}/v2/{project_id}/notifications/topics/urn:smn:regionId: f96188c7ccaf4ffba0c9aa149ab2bd57:test_create_topic_v2/publish { "subject" : "test message v2", "message_structure" : "{\n \"default\": \"xxx\",\n \"APNS\": \"{\\\"aps\\\":{\\\"alert\\\":{\\\"title\\\":\\\"xxx\\\",\\\"body\\\":\\\"xxx\\\"}}}\"\n}", "time_to_live" : "3600" } - Publishing messages using a template
POST https://{SMN_Endpoint}/v2/{project_id}/notifications/topics/urn:smn:regionId: f96188c7ccaf4ffba0c9aa149ab2bd57:test_create_topic_v2/publish { "subject" : "test message template v2", "message_template_name" : "confirm_message", "time_to_live" : "3600", "tags" : { "topic_urn" : "topic_urn3331", "topic_id" : "topic_id3332" } }
Example Responses
Status code: 200
OK
{
"request_id" : "6a63a18b8bab40ffb71ebd9cb80d0085",
"message_id" : "bf94b63a5dfb475994d3ac34664e24f2"
}
Status Codes
|
Status Code |
Description |
|---|---|
|
200 |
OK |
|
400 |
Bad Request |
|
403 |
Unauthorized |
|
404 |
Not Found |
|
500 |
Internal Server Error |
Error Codes
See Error Codes.
Last Article: Message Publishing
Next Article: Resource Tag Operations
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.