向主题发布 - Amazon Simple Notification Service
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

向主题发布

您可以通过使用多个电话号码订阅 Amazon SNS 主题,一次将一条 SMS 消息发布至这些手机号码。SNS 主题是您可以添加订阅者并随后向所有订阅者发布消息的通信渠道。订阅者将收到发布至主题的所有信息,直到您取消订阅或者订阅者已退出来自您的 Amazon 账户的 SMS 消息为止。

向主题发送消息(控制台)

要创建主题,请执行以下操作

如果您还没有要发送 SMS 消息的主题,请完成以下步骤。

  1. 登录 Amazon SNS 控制台

  2. 在控制台菜单中,选择支持 SMS 消息的 Amazon 区域

  3. 在导航窗格中,选择主题

  4. Topics(主页)页面上,选择 Create topic(创建主题)。

  5. Create topic(创建主题)页面上的 Details(详细信息)下,执行以下操作:

    1. 对于 Type(类型),选择 Standard(标准)。

    2. 对于 Name(名称),输入一个主题名称。

    3. (可选)对于 Display name(显示名称),请为您的 SMS 消息输入自定义前缀。在您向主题发送消息时,Amazon SNS 会在显示名称之前加上右尖括号 (>) 和空格。显示名称不区分大小写,Amazon SNS 会将显示名称转换为大写字符。例如,如果主题的显示名称是 MyTopic,而消息是 Hello World!,则该消息会显示为:

      MYTOPIC> Hello World!
  6. 选择创建主题。主题的名称和 Amazon Resource Name (ARN) 显示在 Topics(主题)页面。

创建 SMS 订阅

您可以使用订阅通过仅向主题发布一次消息,将 SMS 消息发送给多个收件人。

注意

当您开始使用 Amazon SNS 发送 SMS 消息时,您的 Amazon 账户位于 SMS 沙盒中。SMS 沙盒为您提供了一个安全的环境,让您可以尝试 Amazon SNS 功能,而不会拿您作为 SMS 发件人的声誉冒险。在您的账户位于 SMS 沙盒时,您可以使用 Amazon SNS 的所有功能,但您只能向已验证的目标电话号码发送 SMS 消息。有关更多信息,请参阅SMS 沙盒

  1. 登录 Amazon SNS 控制台

  2. 在导航窗格中,选择订阅

  3. Subscriptions(订阅)页面上,选择 Create subscription(创建订阅)。

  4. Create subscription(创建订阅)页面上的 Details(详细信息)下,执行以下操作:

    1. 对于 Topic ARN(主题 ARN),输入或选择要向其发送 SMS 消息的主题的 Amazon Resource Name (ARN)。

    2. 对于 Protocol(协议),选择 SMS

    3. 对于 Endpoint(终端节点),输入要订阅主题的电话号码。

  5. 选择创建订阅。订阅信息显示在 Subscriptions(订阅)页面。

    要添加更多电话号码,请重复以下步骤。您还可以添加其他类型的订阅,例如电子邮件。

发送邮件

当您向一个主题发布消息时,Amazon SNS 会尝试将该消息传输至订阅该主题的每个电话号码。

  1. Amazon SNS 控制台中的 Topics(主题)页面上,选择要向其发送 SMS 消息的主题的名称。

  2. 在主题详细信息页面上,选择发布消息

  3. Publish message to topic(将消息发布到主题)页面上的 Message details(消息详细信息)下,执行以下操作:

    1. 对于 Subject(主题),请将该字段留空,除非您的主题包含电子邮件订阅,并且您想要同时发布到电子邮件和 SMS 订阅。Amazon SNS 使用您输入的 Subject(主题)作为电子邮件主题行。

    2. (可选)对于 Time to Live (TTL)(存活时间 (TTL)),输入 Amazon SNS 向任何移动应用程序终端节点订阅者发送 SMS 消息所需的秒数。

  4. Message body(消息正文)下,执行以下操作:

    1. 对于 Message structure(消息结构),选择 Identical payload for all delivery protocols(所有传输协议的负载相同)向订阅了您的主题的所有协议类型发送相同消息。或者,选择 Custom payload for each delivery protocol(针对每个传输协议的自定义负载)为不同协议类型的订阅者自定义消息。例如,您可以输入电话号码订阅者的默认消息,以及电子邮件订阅者的自定义消息。

    2. 对于 Message body to send to the endpoint(要发送到终端节点的消息正文),输入您的消息或每个传输协议的自定义消息。

      如果您的主题包含显示名称,Amazon SNS 会将其添加到消息,这会增加消息的长度。显示名称的长度是名称的字符数加上两个字符,也就是 Amazon SNS 添加的右尖括号 (>) 和空格。

      有关 SMS 消息大小配额的信息,请参阅 发布到移动电话

  5. (可选)对于 Message attributes(消息属性),添加消息元数据,如时间戳、签名和 ID。

  6. 选择发布消息。Amazon SNS 会发送 SMS 消息并显示成功消息。

向主题发送消息(Amazon 开发工具包)

要使用 Amazon 开发工具包,您必须使用您的凭证对其进行配置。有关更多信息,请参阅 Amazon 开发工具包和工具参考指南中的共享配置和凭证文件

以下代码示例显示了如何:

  • 创建 Amazon SNS 主题。

  • 使用手机号码订阅主题。

  • 向主题发布 SMS 消息,以使所有订阅的电话号码一次接收消息。

Java
适用于 Java 2.x 的 SDK
注意

在 GitHub 上查看更多内容。在 Amazon 代码示例存储库 中查找完整示例,了解如何进行设置和运行。

创建一个主题并返回其 ARN。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.sns.SnsClient; import software.amazon.awssdk.services.sns.model.CreateTopicRequest; import software.amazon.awssdk.services.sns.model.CreateTopicResponse; import software.amazon.awssdk.services.sns.model.SnsException; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class CreateTopic { public static void main(String[] args) { final String usage = """ Usage: <topicName> Where: topicName - The name of the topic to create (for example, mytopic). """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String topicName = args[0]; System.out.println("Creating a topic with name: " + topicName); SnsClient snsClient = SnsClient.builder() .region(Region.US_EAST_1) .build(); String arnVal = createSNSTopic(snsClient, topicName); System.out.println("The topic ARN is" + arnVal); snsClient.close(); } public static String createSNSTopic(SnsClient snsClient, String topicName) { CreateTopicResponse result; try { CreateTopicRequest request = CreateTopicRequest.builder() .name(topicName) .build(); result = snsClient.createTopic(request); return result.topicArn(); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; } }

为终端节点订阅主题。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.sns.SnsClient; import software.amazon.awssdk.services.sns.model.SnsException; import software.amazon.awssdk.services.sns.model.SubscribeRequest; import software.amazon.awssdk.services.sns.model.SubscribeResponse; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class SubscribeTextSMS { public static void main(String[] args) { final String usage = """ Usage: <topicArn> <phoneNumber> Where: topicArn - The ARN of the topic to subscribe. phoneNumber - A mobile phone number that receives notifications (for example, +1XXX5550100). """; if (args.length < 2) { System.out.println(usage); System.exit(1); } String topicArn = args[0]; String phoneNumber = args[1]; SnsClient snsClient = SnsClient.builder() .region(Region.US_EAST_1) .build(); subTextSNS(snsClient, topicArn, phoneNumber); snsClient.close(); } public static void subTextSNS(SnsClient snsClient, String topicArn, String phoneNumber) { try { SubscribeRequest request = SubscribeRequest.builder() .protocol("sms") .endpoint(phoneNumber) .returnSubscriptionArn(true) .topicArn(topicArn) .build(); SubscribeResponse result = snsClient.subscribe(request); System.out.println("Subscription ARN: " + result.subscriptionArn() + "\n\n Status is " + result.sdkHttpResponse().statusCode()); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }

设置消息的属性,例如发件人的 ID、最高价格及其类型。消息属性是可选的。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.sns.SnsClient; import software.amazon.awssdk.services.sns.model.SetSmsAttributesRequest; import software.amazon.awssdk.services.sns.model.SetSmsAttributesResponse; import software.amazon.awssdk.services.sns.model.SnsException; import java.util.HashMap; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class SetSMSAttributes { public static void main(String[] args) { HashMap<String, String> attributes = new HashMap<>(1); attributes.put("DefaultSMSType", "Transactional"); attributes.put("UsageReportS3Bucket", "janbucket"); SnsClient snsClient = SnsClient.builder() .region(Region.US_EAST_1) .build(); setSNSAttributes(snsClient, attributes); snsClient.close(); } public static void setSNSAttributes(SnsClient snsClient, HashMap<String, String> attributes) { try { SetSmsAttributesRequest request = SetSmsAttributesRequest.builder() .attributes(attributes) .build(); SetSmsAttributesResponse result = snsClient.setSMSAttributes(request); System.out.println("Set default Attributes to " + attributes + ". Status was " + result.sdkHttpResponse().statusCode()); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }

向主题发布消息。消息将会发送到每个订阅者。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.sns.SnsClient; import software.amazon.awssdk.services.sns.model.PublishRequest; import software.amazon.awssdk.services.sns.model.PublishResponse; import software.amazon.awssdk.services.sns.model.SnsException; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class PublishTextSMS { public static void main(String[] args) { final String usage = """ Usage: <message> <phoneNumber> Where: message - The message text to send. phoneNumber - The mobile phone number to which a message is sent (for example, +1XXX5550100).\s """; if (args.length != 2) { System.out.println(usage); System.exit(1); } String message = args[0]; String phoneNumber = args[1]; SnsClient snsClient = SnsClient.builder() .region(Region.US_EAST_1) .build(); pubTextSMS(snsClient, message, phoneNumber); snsClient.close(); } public static void pubTextSMS(SnsClient snsClient, String message, String phoneNumber) { try { PublishRequest request = PublishRequest.builder() .message(message) .phoneNumber(phoneNumber) .build(); PublishResponse result = snsClient.publish(request); System.out .println(result.messageId() + " Message sent. Status was " + result.sdkHttpResponse().statusCode()); } catch (SnsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }