Example 2: Publishing a Message Using a Message Template
Scenarios
This section describes how to publish a message using a message template. For details, see Calling APIs.
Involved APIs
- Obtaining a User Token Through Password Authentication
- Creating a Topic
- Adding a Subscription
- Creating a Message Template: Create a message template for quick message sending and reduced the request data volume.
- Publishing a Message
Procedure
- Obtain the token by following instructions in Authentication.
- Create a topic.
- Send request POST https://{SMN_Endpoint}/v2/{project_id}/notifications/topics.
- Add X-Auth-Token to the request header.
- Specify the following parameters in the request body:
For details about the parameters, see Creating a Topic.
{ "name": "publish_by_template", //(Mandatory) Topic name "display_name": "test", //(Optional) Display name "enterprise_project_id": "0" //Enterprise project ID (mandatory in the enterprise project scenario) }Example response{ "request_id": "a8f8161449564f569c19e2a9e3be25c9", "topic_urn": "urn:smn:br-iaas-odin1:0605767e0a00d5762fe7c00d0e33345c:publish_by_template" }
- Add a subscription.
- Send request POST https://{SMN_Endpoint}/v2/{project_id}/notifications/topics/{topic_urn}/subscriptions.
Set topic_urn based on the response in 2.c.
- Add X-Auth-Token to the request header.
- Specify the following parameters in the request body:
{ "protocol": "sms", //(Mandatory) Subscription protocol "endpoint": "86137****3629", //(Mandatory) Email address "remark": "Subscription" //(Optional) Description }Example response
{ "subscriptions_result":[{ "request_id":"126c17f805934e5eadf647b0860c7a0a", "subscription_urn":"urn:smn:br-iaas-odin1:0605767e0a00d5762fe7c00d0e33345c:publish_by_template:3e5ac4544a1947b7b0d9f743fe52bdbd", "http_code":201}] }
You can add multiple subscriptions to a topic.
- The subscription endpoint receives a confirmation message, and you must confirm the subscription in the email.
- Send request POST https://{SMN_Endpoint}/v2/{project_id}/notifications/topics/{topic_urn}/subscriptions.
- Create a message template.
Send request POST https://{SMN_Endpoint}/v2/{project_id}/notifications/message_template.
- Add X-Auth-Token to the request header.
- Specify the following parameters in the request body:
{ "message_template_name": "sms_template_test", "locale": "zh-cn", "content": "{content}", "protocol": "sms" }Example response
{ "request_id": "e67228ac92324a58980e922ca600f8ef", "message_template_id": "cdb113996c5d49149fb97af36d9801ce" }
- The subscription endpoint receives a confirmation message and you must confirm the subscription with your phone.
- Specify the following parameters in the request body:
- Use the message template to publish a message.
- Send request POST https://{SMN_Endpoint}/v2/{project_id}/notifications/topics/{topic_urn}/publish.
- Add X-Auth-Token to the request header.
- Specify the following parameters in the request body:
{ "message_template_name": "sms_template_test", "tags": { "content": "hello test" }, "subject": "template message test" }Example response
{ "message_id": "5da965fd9e2c4036a0542317d944af83", "request_id": "c6be4517f985470e8448e42f9d2ec5a2" }
Only confirmed subscription endpoints can receive the message you publish.
Last Article: Example 1: Publishing Messages to a Topic
Next Article: Example 3: Using Mobile Push
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.