Amazon Simple Notification Service
Developer Guide (API Version 2010-03-31)

Using the AWS SDK for .NET with Amazon SNS

The AWS SDK for .NET provides a class named AmazonSimpleNotificationServiceClient that you can use to interact with Amazon SNS. For information about downloading the AWS SDK for .NET, go to AWS SDK for .NET.

The AmazonSimpleNotificationServiceClient class defines methods that map to underlying Amazon SNS Query API actions. (These actions are described in the Amazon SNS API Reference). When you call a method, you must create a corresponding request object and response object. The request object includes information that you must pass with the actual request. The response object includes information returned from Amazon SNS in response to the request.

For example, the AmazonSimpleNotificationServiceClient class provides the CreateTopic method to create a topic to which notifications can be published. This method maps to the underlying CreateTopic API action. You create a CreateTopicRequest object to pass information with the CreateTopic method.

The following import statements are used with the provided C# samples.

using Amazon.SimpleNotificationService; using Amazon.SimpleNotificationService.Model;

The following example shows how to create a new Amazon SNS client, set the Amazon SNS endpoint to use, and then create a new topic.

Note

In some of the following examples, the GetCachedResponseMetadata method is used to show how to programmatically retrieve the request ID for a previously executed successful Amazon SNS request. This is typically used for debugging issues and is helpful when requesting assistance from Amazon Web Services.

Create a Topic

//create a new SNS client and set endpoint AmazonSimpleNotificationServiceClient snsClient = new AmazonSimpleNotificationServiceClient(Amazon.RegionEndpoint.USEast1); //create a new SNS topic CreateTopicRequest createTopicRequest = new CreateTopicRequest("MyNewTopic"); CreateTopicResponse createTopicResponse = snsClient.CreateTopic(createTopicRequest); //print TopicArn Console.WriteLine(createTopicResponse.TopicArn); //get request id for CreateTopicRequest from SNS metadata Console.WriteLine("CreateTopicRequest - " + createTopicResponse.ResponseMetadata.RequestId);

When you run this example, the following is displayed in the console output window:

arn:aws:sns:us-east-1:123456789012:MyNewTopic CreateTopicRequest - 93f7fc90-f131-5ca3-ab18-b741fef918b5

The TopicArn is assigned to a string variable to use in additional operations.

String topicArn = createTopicResponse.TopicArn;

The following examples show how to subscribe to, publish to, and delete a topic.

Subscribe to a Topic

//subscribe to an SNS topic SubscribeRequest subRequest = new SubscribeRequest(topicArn, "email", "name@example.com"); SubscribeResponse subResponse = snsClient.Subscribe(subRequest); //get request id for SubscribeRequest from SNS metadata Console.WriteLine("SubscribeRequest - " + subResponse.ResponseMetadata.RequestId); Console.WriteLine("Check your email and confirm subscription.");

When you run this example, the following is displayed in the console output window of your IDE:

SubscribeRequest - 9b7ff59a-f917-533a-a6bd-be4bf6df0acf Check your email and confirm subscription.

Publish to a Topic

//publish to an SNS topic String msg = "My text published to SNS topic with email endpoint"; PublishRequest publishRequest = new PublishRequest(topicArn, msg); PublishResponse publishResult = snsClient.Publish(publishRequest); //print MessageId of message published to SNS topic Console.WriteLine("MessageId - " + publishResult.MessageId);

When you run this example, the following is displayed in the console output window of your IDE:

MessageId - 9b888f80-15f7-5c30-81a2-c4511a3f5229

Delete a Topic

//delete an SNS topic DeleteTopicRequest deleteTopicRequest = new DeleteTopicRequest(topicArn); DeleteTopicResponse deleteTopicResponse = snsClient.DeleteTopic(deleteTopicRequest); //get request id for DeleteTopicRequest from SNS metadata Console.WriteLine("DeleteTopicRequest - " + deleteTopicResponse.ResponseMetadata.RequestId);

When you run this example, the following is displayed in the console output window of your IDE:

DeleteTopicRequest - 067a4980-4e93-5bfc-b88c-0251415bc852

For more information about .NET and AWS, see AWS Guide for .NET Developers.

For more information about the AWS SDK for .NET;, see Getting Started with the AWS SDK for .NET.