消息通知服务 SMN-示例1:使用主题发布消息:操作步骤

时间:2023-11-01 16:20:33

操作步骤

  1. Token认证,具体操作请参考认证鉴权
  2. 创建Topic。
    1. 发送“POST https://{SMN_Endpoint}/v2/{project_id}/notifications/topics”。
    2. 在Request Header中增加“X-Auth-Token”。
    3. 在Request Body中传入如下参数:
      详细的参数说明请参见创建主题
      {    "name": "test_topic_v2", //Topic名称(必填String)    "display_name": "test" , //显示名(非必填String)    "enterprise_project_id": "0" //企业项目ID(企业项目场景下为必填String)}
      响应示例:
      {         "request_id": "6a63a18b8bab40ffb71ebd9cb80d0085",         "topic_urn": "urn:smn:regionId:f96188c7ccaf4ffba0c9aa149ab2bd57:test_topic_v2" }
  3. 添加订阅。
    1. 发送“POST https://{SMN_Endpoint}/v2/{project_id}/notifications/topics/{topic_urn}/subscriptions”。

      其中,topic_urn可在2.c的返回体中获取。

    2. 在Request Header中增加“X-Auth-Token”。
    3. 在Request Body中传入参数如下。
      {         "protocol": "email", //订阅协议(必填String)         "endpoint": "xxx@xx.com", //邮件地址(必填String)         "remark": "订阅终端" //备注(非必填String) }
      响应示例:
      {    "subscriptions_result": [{        "request_id": "126c17f805934e5eadf647b0860c7a0a",        "subscription_urn": "urn:smn:regionId:f96188c7ccaf4ffba0c9aa149ab2bd57:test_topic_v2",        "http_code": 201}]}

      可为Topic添加多种以及多个订阅终端,支持的订阅协议请参考订阅。

    4. 订阅终端会收到一条确认订阅的消息,请在邮箱中进行确认订阅的操作。
  4. 发送消息。
    1. 发送“POST https://{SMN_Endpoint}/v2/{project_id}/notifications/topics/{topic_urn}/publish”。
    2. 在Request Header中增加“X-Auth-Token”。
    3. 在Request Body中传入参数如下:
      {        "subject":"test message v2", //标题,邮件订阅终端使用(非必填String)        "message": "Message test message v2." //需要发送的消息(必填String) }

      响应示例:

      {        "request_id": "6a63a18b8bab40ffb71ebd9cb80d0085",       "message_id": "992bbd32819448af95f83814cb3c7454"  }

      已完成3.c的确认订阅操作的订阅终端都能接收到该消息,否则无法接收消息。

support.huaweicloud.com/api-smn/smn_api_70001.html