翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
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
をマッチングし、サブスクライブされたエンドポイントが、発行されたメッセージに関心があるかどうかを判断します。-
タイプでは、String.Array などの属性タイプを選択します。
注記
属性のタイプが String.Array である場合は、配列を角括弧 (
[]
) で囲みます。配列内で、値の文字列を二重引用符で囲みます。数字またはキーワードtrue
、false
、null
を引用符で囲む必要はありません。 -
customer_interests
などの属性の名前を入力します。 -
["soccer", "rugby", "hockey"]
などの属性の値を入力します。
属性のタイプが、String、String.Array、または Number である場合、フィルターポリシー (存在する場合) の範囲が明示的に
MessageBody
に設定されていなければ、Amazon SNS はサブスクリプションにメッセージを送信する前に、サブスクリプションのフィルターポリシーに照らしてメッセージ属性を評価します。詳細については、「Amazon SNS メッセージ属性」を参照してください。
-
-
[メッセージの発行] を選択します。
トピックにメッセージが発行され、コンソールがトピックの [詳細] ページを開きます。
AWS SDK を使用してトピックにメッセージを発行するには
AWS SDK を使用するには、認証情報を使用して 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」を参照してください。
-