本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
在创建 Amazon SNS 主题并为终端节点订阅主题后,可以将消息发布到主题。发布消息时,Amazon SNS 会尝试将消息传输给订阅的终端节点。
要使用 AWS Management Console将消息发布到 Amazon SNS 主题
登录 Amazon SNS 控制台
。 -
在左侧导航窗格中,选择主题。
-
在 Topics(主题)页上,选择一个主题,然后选择Publish message(发布主题)。
控制台将打开 Publish message to topic(将消息发布到主题)页面。
-
在 Message details(消息详细信息)部分中,执行以下操作:
-
(可选)输入消息 Subject(主题)。
-
对于 FIFO topic(FIFO 主题),输入 Message group ID(消息组 ID)。同一消息组中的消息按消息的发布顺序传输。
-
对于 FIFO 主题,请输入 Message deduplication ID(消息重复数据删除 ID)。如果您为主题启用了 Content-based message deduplication(基于内容的消息重复数据删除)设置,则此 ID 为可选项。
-
(可选)对于 mobile push notifications(移动推送通知),输入 Time to Live (TTL)(存活时间 (TTL))值(以秒为单位)。这是推送通知服务(例如 Apple 推送通知服务 (APNs) 或 Firebase Cloud Messaging (FCM))将消息传递到端点所需的时间。
-
-
在 Message body (消息正文) 部分中,执行以下操作之一:
-
选择 Identical payload for all delivery protocols(完全相同负载用于所有交付协议),然后输入消息。
-
选择 Custom payload for each delivery protocol(对每个交付协议使用自定义负载),然后输入 JSON 对象定义要发送给每个协议的消息。
有关更多信息,请参阅 使用特定于平台的有效载荷发布 Amazon SNS 通知。
-
-
在 Message attributes(消息属性)部分中,添加您希望 Amazon SNS 与订阅属性
FilterPolicy
相匹配的任何属性,以确定订阅的终端节点是否对发布的消息感兴趣。-
对于 Type(类型),选择属性类型,例如 String.Array。
注意
对于属性类型 String.Array,请将该数组放入方括号 (
[]
) 内。在该数组内,将字符串值加入双引号内。数字以及关键字true
、false
和null
无需加引号。 -
输入属性名称,例如
customer_interests
。 -
输入属性值,例如
["soccer", "rugby", "hockey"]
。
如果属性类型为 String、String.Array 或 Number,Amazon SNS 会首先依据订阅的筛选策略(如果存在)来评估该消息属性,然后再将消息发送至该订阅,前提是筛选策略范围没有明确设置为
MessageBody
。有关更多信息,请参阅 Amazon SNS 消息属性。
-
-
选择发布消息。
消息将发布到主题,且控制台将打开主题的 Details(详细信息)页面。
使用 AWS SDK 将消息发布到主题
要使用 S AWS DK,必须使用您的凭据对其进行配置。有关更多信息,请参阅和工具参考指南中的共享配置AWS SDKs 和凭据文件。
以下代码示例演示如何使用 Publish
。
- 适用于 .NET 的 SDK
-
注意
还有更多相关信息 GitHub。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 向主题发布消息。
using System; using System.Threading.Tasks; using Amazon.SimpleNotificationService; using Amazon.SimpleNotificationService.Model; /// <summary> /// This example publishes a message to an Amazon Simple Notification /// Service (Amazon SNS) topic. /// </summary> public class PublishToSNSTopic { public static async Task Main() { string topicArn = "arn:aws:sns:us-east-2:000000000000:ExampleSNSTopic"; string messageText = "This is an example message to publish to the ExampleSNSTopic."; IAmazonSimpleNotificationService client = new AmazonSimpleNotificationServiceClient(); await PublishToTopicAsync(client, topicArn, messageText); } /// <summary> /// Publishes a message to an Amazon SNS topic. /// </summary> /// <param name="client">The initialized client object used to publish /// to the Amazon SNS topic.</param> /// <param name="topicArn">The ARN of the topic.</param> /// <param name="messageText">The text of the message.</param> public static async Task PublishToTopicAsync( IAmazonSimpleNotificationService client, string topicArn, string messageText) { var request = new PublishRequest { TopicArn = topicArn, Message = messageText, }; var response = await client.PublishAsync(request); Console.WriteLine($"Successfully published message ID: {response.MessageId}"); } }
使用组、复制和属性选项向主题发布消息。
/// <summary> /// Publish messages using user settings. /// </summary> /// <returns>Async task.</returns> public static async Task PublishMessages() { Console.WriteLine("Now we can publish messages."); var keepSendingMessages = true; string? deduplicationId = null; string? toneAttribute = null; while (keepSendingMessages) { Console.WriteLine(); var message = GetUserResponse("Enter a message to publish.", "This is a sample message"); if (_useFifoTopic) { Console.WriteLine("Because you are using a FIFO topic, you must set a message group ID." + "\r\nAll messages within the same group will be received in the order " + "they were published."); Console.WriteLine(); var messageGroupId = GetUserResponse("Enter a message group ID for this message:", "1"); if (!_useContentBasedDeduplication) { Console.WriteLine("Because you are not using content-based deduplication, " + "you must enter a deduplication ID."); Console.WriteLine("Enter a deduplication ID for this message."); deduplicationId = GetUserResponse("Enter a deduplication ID for this message.", "1"); } if (GetYesNoResponse("Add an attribute to this message?")) { Console.WriteLine("Enter a number for an attribute."); for (int i = 0; i < _tones.Length; i++) { Console.WriteLine($"\t{i + 1}. {_tones[i]}"); } var selection = GetUserResponse("", "1"); int.TryParse(selection, out var selectionNumber); if (selectionNumber > 0 && selectionNumber < _tones.Length) { toneAttribute = _tones[selectionNumber - 1]; } } var messageID = await SnsWrapper.PublishToTopicWithAttribute( _topicArn, message, "tone", toneAttribute, deduplicationId, messageGroupId); Console.WriteLine($"Message published with id {messageID}."); } keepSendingMessages = GetYesNoResponse("Send another message?", false); } }
将用户的选择应用于发布操作。
/// <summary> /// Publish a message to a topic with an attribute and optional deduplication and group IDs. /// </summary> /// <param name="topicArn">The ARN of the topic.</param> /// <param name="message">The message to publish.</param> /// <param name="attributeName">The optional attribute for the message.</param> /// <param name="attributeValue">The optional attribute value for the message.</param> /// <param name="deduplicationId">The optional deduplication ID for the message.</param> /// <param name="groupId">The optional group ID for the message.</param> /// <returns>The ID of the message published.</returns> public async Task<string> PublishToTopicWithAttribute( string topicArn, string message, string? attributeName = null, string? attributeValue = null, string? deduplicationId = null, string? groupId = null) { var publishRequest = new PublishRequest() { TopicArn = topicArn, Message = message, MessageDeduplicationId = deduplicationId, MessageGroupId = groupId }; if (attributeValue != null) { // Add the string attribute if it exists. publishRequest.MessageAttributes = new Dictionary<string, MessageAttributeValue> { { attributeName!, new MessageAttributeValue() { StringValue = attributeValue, DataType = "String"} } }; } var publishResponse = await _amazonSNSClient.PublishAsync(publishRequest); return publishResponse.MessageId; }
-
有关 API 详细信息,请参阅《适用于 .NET 的 AWS SDK API Reference》中的 Publish。
-