Creating a topic

The classes and functions related to the SNS service are available in the SimpleNotificationService library, which must be imported in the application program.

An SNS topic can be created simply by invoking the CreateTopic() function of the AmazonSimpleNotificationServiceClient class. The following code shows how to create an SNS topic and set its attributes:

using System;

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

namespace packtpubSNSDemo
{
class SNSDemo
{
public static void Main(string[] args)
{
var sns_object = new AmazonSimpleNotificationServiceClient();

string email_id = "[email protected]";

try
{
// Create an SNS topic called "PACKT-PUB"
Console.WriteLine("Creating topic...");
var Topic_Arn_id = sns_object.CreateTopic(new CreateTopicRequest
{
Name = "PACKT-PUB"
}).TopicArn;

// Set attributes for the topic
sns_object.SetTopicAttributes(new SetTopicAttributesRequest
{
TopicArn = Topic_Arn_id,
AttributeName = "DisplayName",
AttributeValue = "PACKT-PUB"
});
}
catch (AmazonSimpleNotificationServiceException exception)
{
Console.WriteLine("Error !");
Console.WriteLine(exception.ErrorCode);
}
}
}
}

Once a topic is created, users can then subscribe to the topic. Let's write code to add a user as a subscriber to the topic that we created.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.253.62