教程:将消息发布到 Amazon SNS 主题
在创建主题并为终端节点订阅主题后,可以将消息发布到主题。在发布消息时,Amazon SNS 会尝试将消息传输到已订阅主题的每个终端节点(例如,AWS Lambda、Amazon SQS、HTTP/S 或电子邮件地址)。
以下教程说明如何使用 AWS 管理控制台、AWS SDK for Java和适用于 .NET 的 AWS 开发工具包将消息发布到主题。
使用 AWS 管理控制台将消息发布到 Amazon SNS 主题
-
登录 Amazon SNS 控制台。
-
在导航面板上,选择主题。
-
在主题页面上,选择您之前创建的主题,然后选择发布消息。
-
在 Publish message to topic (将消息发布到主题) 页面上,执行以下操作:
-
(可选)在 Message details (消息详细信息) 部分中,输入 Subject (主题),例如:
Hello from Amazon SNS!
-
在 Message body (消息正文) 部分中,执行以下操作之一:
-
选择 Identical payload for all delivery protocols (完全相同负载用于所有交付协议),然后输入消息,例如:
If you receive this message, publishing a message to an Amazon SNS topic works.
-
选择 Custom payload for each delivery protocol (对每个交付协议使用自定义负载),然后使用 JSON 对象定义要发送给每个协议的消息,例如:
{ "default": "Sample fallback message", "email": "Sample message for email endpoints", "sqs": "Sample message for Amazon SQS endpoints", "lambda": "Sample message for AWS Lambda endpoints", "http": "Sample message for HTTP endpoints", "https": "Sample message for HTTPS endpoints", "sms": "Sample message for SMS endpoints", "APNS": "{\"aps\":{\"alert\": \"Sample message for iOS endpoints\"} }", "APNS_SANDBOX": "{\"aps\":{\"alert\":\"Sample message for iOS development endpoints\"}}", "APNS_VOIP": "{\"aps\":{\"alert\":\"Sample message for Apple VoIP endpoints\"}}", "APNS_VOIP_SANDBOX": "{\"aps\":{\"alert\": \"Sample message for Apple VoIP development endpoints\"} }", "MACOS": "{\"aps\":{\"alert\":\"Sample message for MacOS endpoints\"}}", "MACOS_SANDBOX": "{\"aps\":{\"alert\": \"Sample message for MacOS development endpoints\"} }", "GCM": "{ \"data\": { \"message\": \"Sample message for Android endpoints\" } }", "ADM": "{ \"data\": { \"message\": \"Sample message for FireOS endpoints\" } }", "BAIDU": "{\"title\":\"Sample message title\",\"description\":\"Sample message for Baidu endpoints\"}", "MPNS": "<?xml version=\"1.0\" encoding=\"utf-8\"?><wp:Notification xmlns:wp=\"WPNotification\"><wp:Tile><wp:Count>ENTER COUNT</wp:Count><wp:Title>Sample message for Windows Phone 7+ endpoints</wp:Title></wp:Tile></wp:Notification>", "WNS": "<badge version=\"1\" value=\"42\"/>" }
-
-
在 Message attributes (消息属性) 部分中,添加您希望 Amazon SNS 与订阅属性
FilterPolicy
相匹配的任何属性,以确定订阅的终端节点是否对发布的消息感兴趣。-
选择属性 Type (类型),例如,String.Array。
注意
如果属性类型是 String.Array,请将该数组放入方括号 (
[]
) 内。在该数组内,将字符串值加入双引号内。数字以及关键字true
、false
和null
无需加引号。 -
输入属性的 Name (名称),例如
customer_interests
。 -
输入属性的 Value (值),例如
["soccer", "rugby", "hockey"]
。
如果属性类型是 String、String.Array 或 Number,Amazon SNS 会首先依据订阅的筛选策略(如果存在)来评估该消息属性,然后再将消息发送至该订阅。
有关更多信息,请参阅 Amazon SNS 消息属性。
-
-
选择发布消息。
消息发布到主题,并显示
MyTopic
页面。主题的名称、ARN、(可选)显示名称和主题所有者的 AWS 账户 ID 将显示在详细信息部分中。
-
-
在电子邮件客户端中,检查您之前指定的电子邮件地址,并从 Amazon SNS 中阅读电子邮件。
使用AWS SDK for Java将消息发布到 Amazon SNS 主题
-
指定您的 AWS 凭证。有关更多信息,请参阅 适用于 Java 的 AWS 开发工具包 2.x 开发人员指南 中的设置用于开发的 AWS 凭证和区域。
-
编写您的代码。有关更多信息,请参阅使用 适用于 Java 的开发工具包 2.x。
以下代码摘录将消息发布到主题,然后打印
MessageId
。// Publish a message to an Amazon SNS topic. final String msg = "If you receive this message, publishing a message to an Amazon SNS topic works."; final PublishRequest publishRequest = new PublishRequest(topicArn, msg); final PublishResult publishResponse = snsClient.publish(publishRequest); // Print the MessageId of the message. System.out.println("MessageId: " + publishResponse.getMessageId());
-
编译并运行您的代码。
消息已发布并且已打印
MessageId
,例如:MessageId: 1234a567-bc89-012d-3e45-6fg7h890123i
使用适用于 .NET 的 AWS 开发工具包将消息发布到 Amazon SNS 主题
-
指定您的 AWS 凭证。有关更多信息,请参阅 适用于 .NET 的 AWS 开发工具包 开发人员指南 中的配置 AWS 凭证。
-
编写您的代码。有关更多信息,请参阅使用 适用于 .NET 的 AWS 开发工具包 进行编程。
以下代码摘录将消息发布到主题,然后打印
MessageId
。// Publish a message to an Amazon SNS topic. String msg = "If you receive this message, publishing a message to an Amazon SNS topic works."; PublishRequest publishRequest = new PublishRequest(topicArn, msg); PublishResponse publishResponse = snsClient.Publish(publishRequest); // Print the MessageId of the published message. Console.WriteLine("MessageId: " + publishResponse.MessageId);
-
编译并运行您的代码。
消息已发布并且已打印
MessageId
,例如:MessageId: 1234a567-bc89-012d-3e45-6fg7h890123i