AWS SDK Version 2 for .NET
API Reference

AWS services or capabilities described in AWS Documentation may vary by region/location. Click Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

.NET Framework 3.5
 
Container for the parameters to the Publish operation. Sends a message to all of a topic's subscribed endpoints. When a messageId is returned, the message has been saved and Amazon SNS will attempt to deliver it to the topic's subscribers shortly. The format of the outgoing message to each subscribed endpoint depends on the notification protocol selected.

To use the Publish action for sending a message to a mobile endpoint, such as an app on a Kindle device or mobile phone, you must specify the EndpointArn. The EndpointArn is returned when making a call with the CreatePlatformEndpoint action. The second example below shows a request and response for publishing to a mobile endpoint.

Inheritance Hierarchy

System.Object
  Amazon.Runtime.AmazonWebServiceRequest
    Amazon.SimpleNotificationService.AmazonSimpleNotificationServiceRequest
      Amazon.SimpleNotificationService.Model.PublishRequest

Namespace: Amazon.SimpleNotificationService.Model
Assembly: AWSSDK.dll
Version: (assembly version)

Syntax

C#
public class PublishRequest : AmazonSimpleNotificationServiceRequest
         IRequestEvents

The PublishRequest type exposes the following members

Constructors

NameDescription
Public Method PublishRequest() Empty constructor used to set properties independently even when a simple constructor is available
Public Method PublishRequest(string, string) Instantiates PublishRequest with the parameterized properties
Public Method PublishRequest(string, string, string) Instantiates PublishRequest with the parameterized properties

Properties

NameTypeDescription
Public Property Message System.String Gets and sets the property Message.

The message you want to send to the topic.

If you want to send the same message to all transport protocols, include the text of the message as a String value.

If you want to send different messages for each transport protocol, set the value of the MessageStructure parameter to json and use a JSON object for the Message parameter. See the Examples section for the format of the JSON object.

Constraints: Messages must be UTF-8 encoded strings at most 256 KB in size (262144 bytes, not 262144 characters).

JSON-specific constraints:

  • Keys in the JSON object that correspond to supported transport protocols must have simple JSON string values.
  • The values will be parsed (unescaped) before they are used in outgoing messages.
  • Outbound notifications are JSON encoded (meaning that the characters will be reescaped for sending).
  • Values have a minimum length of 0 (the empty string, "", is allowed).
  • Values have a maximum length bounded by the overall message size (so, including multiple protocols may limit message sizes).
  • Non-string values will cause the key to be ignored.
  • Keys that do not correspond to supported transport protocols are ignored.
  • Duplicate keys are not allowed.
  • Failure to parse or validate any key or value in the message will cause the Publish call to return an error (no partial delivery).

Public Property MessageAttributes System.Collections.Generic.Dictionary<System.String, Amazon.SimpleNotificationService.Model.MessageAttributeValue> Gets and sets the property MessageAttributes.
Public Property MessageStructure System.String Gets and sets the property MessageStructure.

Set MessageStructure to json if you want to send a different message for each protocol. For example, using one publish action, you can send a short message to your SMS subscribers and a longer message to your email subscribers. If you set MessageStructure to json, the value of the Message parameter must:

  • be a syntactically valid JSON object; and
  • contain at least a top-level JSON key of "default" with a value that is a string.

You can define other top-level keys that define the message you want to send to a specific transport protocol (e.g., "http").

For information about sending different messages for each protocol using the AWS Management Console, go to Create Different Messages for Each Protocol in the Amazon Simple Notification Service Getting Started Guide.

Valid value: json

Public Property Subject System.String Gets and sets the property Subject.

Optional parameter to be used as the "Subject" line when the message is delivered to email endpoints. This field will also be included, if present, in the standard JSON messages delivered to other endpoints.

Constraints: Subjects must be ASCII text that begins with a letter, number, or punctuation mark; must not include line breaks or control characters; and must be less than 100 characters long.

Public Property TargetArn System.String Gets and sets the property TargetArn.

Either TopicArn or EndpointArn, but not both.

Public Property TopicArn System.String Gets and sets the property TopicArn.

The topic you want to publish to.

Examples

This example shows how to create, subscribe to, and publish a topic.

Create, subscribe to, and publish topic example

var snsClient = new AmazonSimpleNotificationServiceClient();

var topicRequest = new CreateTopicRequest
{
  Name = "CodingTestResults"
};

var topicResponse = snsClient.CreateTopic(topicRequest);

var topicAttrRequest = new SetTopicAttributesRequest
{
  TopicArn = topicResponse.TopicArn,
  AttributeName = "DisplayName",
  AttributeValue = "Coding Test Results"
};

snsClient.SetTopicAttributes(topicAttrRequest);

snsClient.Subscribe(new SubscribeRequest
{
  Endpoint = "johndoe@example.com",
  Protocol = "email",
  TopicArn = topicResponse.TopicArn
});

// Wait for up to 2 minutes for the user to confirm the subscription.
DateTime latest = DateTime.Now + TimeSpan.FromMinutes(2);

while (DateTime.Now < latest)
{
  var subsRequest = new ListSubscriptionsByTopicRequest
  {
    TopicArn = topicResponse.TopicArn
  };

  var subs = snsClient.ListSubscriptionsByTopic(subsRequest).Subscriptions;

  var sub = subs[0];

  if (!string.Equals(sub.SubscriptionArn,
    "PendingConfirmation", StringComparison.Ordinal))
  {
    break;
  }

  // Wait 15 seconds before trying again.
  System.Threading.Thread.Sleep(TimeSpan.FromSeconds(15));
}

snsClient.Publish(new PublishRequest
{
  Subject = "Coding Test Results for " +
    DateTime.Today.ToShortDateString(),
  Message = "All of today's coding tests passed.",
  TopicArn = topicResponse.TopicArn
});
      

Version Information

.NET Framework:
Supported in: 4.5, 4.0, 3.5

.NET for Windows Store apps:
Supported in: Windows 8.1, Windows 8

.NET for Windows Phone:
Supported in: Windows Phone 8.1, Windows Phone 8