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 4.5
 
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.

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

Syntax

C#
public virtual PublishResponse Publish(
         PublishRequest request
)

Parameters

request
Type: Amazon.SimpleNotificationService.Model.PublishRequest

Container for the necessary parameters to execute the Publish service method.

Return Value
Type: Amazon.SimpleNotificationService.Model.PublishResponse
The response from the Publish service method, as returned by SimpleNotificationService.

Exceptions

ExceptionCondition
AuthorizationErrorException Indicates that the user has been denied access to the requested resource.
EndpointDisabledException Exception error indicating endpoint disabled.
InternalErrorException Indicates an internal service error.
InvalidParameterException Indicates that a request parameter does not comply with the associated constraints.
InvalidParameterValueException
NotFoundException Indicates that the requested resource does not exist.
PlatformApplicationDisabledException Exception error indicating platform application disabled.

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