Updated on 2024-04-19 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

Calling Method

For details, see Calling APIs.

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 is a JSON string. The following protocols are supported: email, sms, http, https, dms, functiongraph, functionstage, HMS, APNS, APNS_SANDBOX, dingding, or wechat, feishu, or welink. For details about the message formats of the HMS, APNS, and APNS_SANDBOX protocols, 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. The msgType field must be specified for messages of the DingTalk chatbot, WeCom chatbot, Lark chatbot, or WeLink protocol. The DingTalk, WeCom, or Lark chatbot protocol support messages in text or markdown format. WeLink and WeLink (red version) chatbots support messages in text format.For the DingTalk chatbot protocol, SMN supports the @ group member function through the at fields. When you need to @ group members, set isAtAll to "true". You can enter the phone numbers of persons who need to be @ for the atMobiles field or enter the DingTalk user IDs of persons who need to be @ for the atUserIds field. When you use the atMobiles or atUserIds field, add @ before each phone number or user ID in the message content. For details about the display effect, see the official DingTalk document.Note:The following three message formats are supported:messagemessage_structuremessage_template_nameSet at least one of them. If you set all of them, they take effect in the following sequence:message_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).

message_attributes

No

Array of MessageAttribute objects

Specifies the message attribute list.

Array Length: 0 - 10

Table 4 MessageAttribute

Parameter

Mandatory

Type

Description

name

Yes

String

Specifies the attribute name. name can contain 1 to 32 characters, including only lowercase letters, digits, and underscores (_). name cannot start or end with an underscore, nor contain consecutive underscores.

Minimum: 1

Maximum: 32

type

Yes

String

Specifies the attribute type. The value can be one of the following: STRING STRING_ARRAY PROTOCOL

Enumeration values:

  • STRING

  • STRING_ARRAY

  • PROTOCOL

value

Yes

Object

Specifies the attribute value. When you set type to STRING, value can contain 1 to 32 characters, including only letters, digits, and underscores (). When you set type to STRING_ARRAY, value is an array containing 1 to 10 strings. Each string in the array must be unique. Each string in the array must contain 1 to 32 characters, including only letters, digits, and underscores (). When you set type to PROTOCOL, value is a string array of supported protocol types.

Response Parameters

Status code: 200

Table 5 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 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: 403

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: 404

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: 429

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.

Status code: 500

Table 10 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",
      "message_attributes" : [ {
        "name" : "smn_protocol",
        "type" : "PROTOCOL",
        "value" : [ "email", "sms" ]
      } ]
    }
  • 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",
      "message_attributes" : [ {
        "name" : "smn_protocol",
        "type" : "PROTOCOL",
        "value" : [ "email", "sms" ]
      } ]
    }
  • 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"
      },
      "message_attributes" : [ {
        "name" : "smn_protocol",
        "type" : "PROTOCOL",
        "value" : [ "email", "sms" ]
      } ]
    }

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.