Updated on 2023-02-13 GMT+08:00

Publishing a Message

Function

This API is used to 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

If the three formats are specified at the same time, they take effect in the following sequence: message_structure > message_template_name > message

URI

POST /v2/{project_id}/notifications/topics/{topic_urn}/publish

Table 1 Path Parameters

Parameter

Mandatory

Type

Description

project_id

Yes

String

Specifies the project ID.For details about how to obtain the project ID, see Obtaining 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

Table 2 Request header parameters

Parameter

Mandatory

Type

Description

X-Auth-Token

Yes

String

Specifies a user token.

It can be obtained by calling an IAM API. The value of X-Subject-Token in the response header is the user token.

Table 3 Request body parameters

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 message content must be UTF-8-coded and can be 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:Three message formats are supported:messagemessage_structuremessage_template_nameAt least one of them must be set. If they are set at the same time, the effective priority ismessage_structure > message_template_name > message

message_structure

No

String

Specifies the message structure, which contains JSON strings. email, sms, http, https, dms, functionstage, HMS, APNS, or APNS_SANDBOX are supported. For details about HMS, APNS, and APNS_SANDBOX message formats, see the format of the application message body. The default protocol is mandatory. If the system fails to match any other protocols, the message using the default protocol is sent.Note:Three message formats are supported:messagemessage_structuremessage_template_nameAt least one of them must be set. If they are set at the same time, the effective priority ismessage_structure > message_template_name > messageHMS transparent push message{ "hps": { "msg": { "type": 1, "body": { "key": "value" } } }}Huawei notification push message{ "hps": { "msg": { "type": 3, "body": { "content": "Push message content", "title": "Push message content" }, "action": { "type": 1, "param": { "intent": "#Intent;compo=com.rvr/.Activity;S.W=U;end" } } }, "ext": { "biTag": "Trump", "icon": "http://upload.w.org/00/150pxsvg.png" } }}Apple platform message{ "aps": { "alert": "hello world" }}

message_template_name

No

String

Specifies the message template name. To obtain the name, see Querying Message Templates.Note:Three message formats are supported:messagemessage_structuremessage_template_nameAt least one of them must be set. If they are set at the same time, the effective priority ismessage_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

Table 4 Response body parameters

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

Table 5 Response body parameters

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

Table 6 Response body parameters

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

Table 7 Response body parameters

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

Table 8 Response body parameters

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

Table 9 Response body parameters

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 a 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

429

Too Many Requests

500

Internal Server Error

Error Codes

See Error Codes.