기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.
Amazon SNS 주제를 생성하고 엔드포인트에서 주제를 구독 설정한 후 주제에 메시지를 게시할 수 있습니다. 메시지가 게시되면 Amazon SNS는 구독 설정된 엔드포인트로 메시지를 전달하려고 시도합니다.
AWS Management Console을 사용하여 Amazon SNS 주제에 메시지를 게시하려면
Amazon SNS 콘솔
에 로그인합니다. -
왼쪽 탐색 창에서 주제를 선택합니다.
-
주제 페이지에서 주제를 선택하고 메시지 게시를 선택합니다.
콘솔에서 주제에 메시지 게시 페이지가 열립니다.
-
메시지 세부 정보 섹션에서 다음을 수행합니다.
-
(선택 사항) 메시지 제목을 입력합니다.
-
FIFO 주제에 메시지 그룹 ID를 입력합니다. 동일한 메시지 그룹의 메시지는 게시된 순서대로 전송됩니다.
-
FIFO 주제에 메시지 중복 제거 ID를 입력합니다. 주제에 대해 콘텐츠 기반 메시지 중복 제거 설정을 사용하는 경우 이 ID는 선택 사항입니다.
-
(선택 사항) 모바일 푸시 알림에 초 단위로 유지 시간(TTL)을 입력합니다. 이 시간은 Apple Push Notification Service(APNS) 또는 Firebase Cloud Messaging(FCM)과 같은 푸시 알림 서비스가 메시지를 엔드포인트에 전달해야 하는 시간입니다.
-
-
메시지 본문 섹션에서 다음 중 하나를 수행합니다.
-
모든 전송 프로토콜에 대해 동일한 페이로드를 선택한 다음 메시지를 입력합니다.
-
각 전송 프로토콜에 대해 사용자 지정 페이로드를 선택한 다음 JSON 객체를 입력하여 각 전송 프로토콜에 보낼 메시지를 정의합니다.
자세한 내용은 플랫폼별 페이로드를 사용하여 Amazon SNS 알림 게시 섹션을 참조하세요.
-
-
메시지 속성 섹션에서, Amazon SNS를 구독 속성인
FilterPolicy
와 일치시켜 구독된 엔드포인트가 게시된 메시지와 관련이 있는지 확인하려는 속성을 추가합니다.-
Type에서 속성 유형(예:String.Array)을 선택합니다.
참고
속성 유형 String.Array에 대해 배열을 대괄호로 묶습니다(
[]
). 배열 안에서는 문자열 값을 큰따옴표로 묶습니다. 숫자 또는 키워드true
,false
및null
에는 따옴표가 필요 없습니다. -
속성 Name(예:
customer_interests
)을 입력합니다. -
속성 Value(예:
["soccer", "rugby", "hockey"]
)를 입력합니다.
속성 유형이 String, String.Array 또는 Number인 경우, 필터 정책 범위가 명시적으로
MessageBody
로 설정되지 않았다면 Amazon SNS는 구독에 메시지를 전송하기 전에 구독의 필터 정책(있는 경우)에 따라 메시지 속성을 평가합니다.자세한 내용은 Amazon SNS 메시지 속성 섹션을 참조하세요.
-
-
메시지 게시를 선택합니다.
메시지가 주제에 게시되고 콘솔에서 주제의 세부 정보 페이지가 열립니다.
AWS SDK를 사용하여 주제에 메시지를 게시하려면
AWS SDK를 사용하려면 자격 증명으로 구성해야 합니다. 자세한 정보는 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 참조의 Publish를 참조하세요.
-