Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.
Dopo aver creato un argomento Amazon SNS e avervi iscritto un endpoint, puoi pubblicare messaggi in tale argomento. Quando viene pubblicato un messaggio, Amazon SNS tenta di recapitare il messaggio a Endpoint iscritti.
Per pubblicare messaggi su argomenti Amazon SNS utilizzando il AWS Management Console
Accedi alla console Amazon SNS
. -
Nel pannello di navigazione a sinistra, selezionare Topics (Argomenti).
-
Nella pagina Topics (Argomenti), seleziona un argomento e scegli Publish messaggio (Pubblica messaggio).
La console apre la pagina Pubblica messaggio in argomento.
-
Nella sezione Basic details (Dettagli di base), eseguire le seguenti:
-
(Facoltativo) Immettere un messaggio Oggetto.
-
Per un Argomento FIFO, immettere un ID gruppo di messaggi. I messaggi nello stesso gruppo di messaggi vengono recapitati nell'ordine in cui vengono pubblicati.
-
Per un argomento FIFO, immettere un ID deduplicazione messaggi. Questo ID è facoltativo se è stata attivata l’impostazione Deduplicazione dei messaggi basata sul contenuto per l'argomento.
-
(Facoltativo) Per Notife push per dispositivi, immettere un valore Time to Live (TTL) in secondi. Questo è il periodo di tempo che un servizio di notifica push, come Apple Push Notification Service (APNs) o Firebase Cloud Messaging (FCM), ha a disposizione per recapitare il messaggio all'endpoint.
-
-
Nella sezione Message body (Corpo del messaggio), eseguire una delle seguenti operazioni:
-
Scegliere Identical payload for all delivery protocols (Payload identico per tutti i protocolli di consegna) e immettere il messaggio.
-
Scegliere Custom payload for each delivery protocol (Payload personalizzato per ogni protocollo di consegna) e inserire l'oggetto JSON per definire il messaggio da inviare a ogni protocollo.
Per ulteriori informazioni, consulta Pubblicazione di notifiche Amazon SNS con payload specifici della piattaforma.
-
-
Nella sezione Message attributes (Attributi messaggio), aggiungere tutti gli attributi che Amazon SNS deve confrontare con l'attributo di sottoscrizione
FilterPolicy
per decidere se l'endpoint sottoscritto è interessato al messaggio pubblicato.-
Per Type (Tipo), scegliere un tipo di attributo, ad esempio String.Array.
Nota
Per il tipo di attributo String.Array, racchiudi la matrice tra parentesi (
[]
). All'interno della matrice, racchiudi i valori di stringa tra virgolette. Non hai bisogno di usare le virgolette per i numeri o per le parole chiavetrue
,false
enull
. -
Immettere un attributo Nome, ad esempio
customer_interests
. -
Immettere un attributo Valore, ad esempio
["soccer", "rugby", "hockey"]
.
Se il tipo di attributo è String, String.Array o Number, Amazon SNS valuta l'attributo del messaggio rispetto alla policy di filtro di una sottoscrizione, se presente, prima di inviare il messaggio a tale sottoscrizione se l'ambito della policy di filtro non impostato in modo esplicito su
MessageBody
.Per ulteriori informazioni, consulta Attributi messaggio di Amazon SNS.
-
-
Seleziona Publish message (Pubblica messaggio).
Il messaggio viene pubblicato all'argomento e la console apre la pagina dell’argomento Dettagli.
Pubblicazione di un messaggio in un argomento tramite AWS
Per utilizzare un AWS SDK, devi configurarlo con le tue credenziali. Per ulteriori informazioni, consulta I file di configurazione e credenziali condivisi nella and Tools Reference AWS SDKs Guide.
Gli esempi di codice seguenti mostrano come utilizzare Publish
.
- SDK per .NET
-
Nota
C'è di più su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS
. Pubblicare un messaggio in un argomento.
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}"); } }
Pubblica un messaggio in un argomento con opzioni di gruppo, duplicazione e attributo.
/// <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); } }
Applica le selezioni dell'utente all'azione di pubblicazione.
/// <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; }
-
Per informazioni dettagliate sulle API, consulta Pubblicazione nella Documentazione di riferimento per le API AWS SDK per .NET .
-