本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
在您 create an Amazon SNS topic (建立 Amazon SNS 主題) 並讓端點 subscribe (訂閱) 主題之後,您可以將訊息 publish (發布) 至主題。發布訊息時,Amazon SNS 會嘗試將訊息傳送到已訂閱的端點。
將訊息發布到使用 AWS Management Console的 Amazon SNS 主題
登入 Amazon SNS 主控台
。 -
在左側導覽窗格中,選擇 Topics (主題)。
-
在 Topics (主題) 頁面上,選取主題然後選擇 Publish message (發布訊息)。
主控台會開啟 Publish message to topic (將訊息發布至主題) 頁面。
-
在 Message details(訊息詳細資訊) 區段中,執行下列動作:
-
(選用) 輸入訊息 Subject (主旨)。
-
對於 FIFO topic (FIFO 主題),輸入 Message group ID (訊息群組 ID)。相同訊息群組中的訊息會依發布順序傳遞。
-
對於 FIFO 主題,請輸入 Message deduplication ID (訊息重複資料刪除 ID)。如果您啟用內容為基礎的訊息重複資料刪除的主題設定,此 ID 是選擇性的。
-
(選用) 針對 mobile push notifications (行動推播通知),輸入 Time to Live (TTL) (存留時間 (TTL)) 值 (以秒為單位)。這是推播通知服務 (例如 Apple 推播通知服務 (APN) 或 Firebase 雲端訊息 (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 (數字),且篩選政策範圍未明確設為
MessageBody
,Amazon SNS 會以訂閱的篩選政策 (如果在傳送訊息到該訂閱之前出現) 來評估訊息屬性。如需詳細資訊,請參閱Amazon SNS 訊息屬性。
-
-
選擇 Publish message (發佈訊息)。
訊息會發布到主題,主控台會開啟主題的 Details (詳細資訊) 頁面。
使用 AWS SDK 將訊息發布至主題
若要使用 AWS 開發套件,您必須使用 登入資料進行設定。如需詳細資訊,請參閱 AWS SDK和工具參考指南中的共享的配置和認證文件。
下列程式碼範例示範如何使用 Publish
。
- SDK for .NET
-
注意
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 詳細資訊,請參閱 AWS SDK for .NET API 參考中的發佈。
-