Amazon SNS (Simple Notification Service) Using Visual Studio

SNS (Amazon Simple Notification Services)

 
Amazon SNS (Amazon Simple Notification Services) is a notification service used to send the notification to the subscribed endpoint or devices. It is provided as a part of Amazon Web Services. It provides a low-cost infrastructure for the mass delivery of messages, emails, notification, etc.
 
Steps to implementing Amazon SNS
  1. Create a topic - you will get the Topic ARN for which you have to send the notification or send the messages.
  2. Create Application - after this, you have to create an application whether you want to send the notification over the Android application or over the iOS application.
  3. Create Endpoint - you need to create the endpoint and subscribe to that endpoint on which you have to publish the message or send the notification.
  4. Publish Message - the last step is to publish the message or send the notification to the endpoint.
You can follow the same process for using this notification service over the Amazon Console too. You need to login to the console first. After that, you will see a screen like this.
 
 
Type "Simple Notification Service" in the textbox and select from the results shown on the screen.
 
AWS 
 
Here, you will have the list of topics, subscriptions, applications, and endpoints that you create. In the above screenshot, I have created two endpoints - one for Android devices (GSM) and the other one for iOS devices (APNS).
 
For using the ASNS from the console, the steps are the same as described above. I am going to show a working example of the ASNS in .NET using Visual Studio.
Let's begin.
 
Step 1
 
Install AWS Toolkit for Visual Studio.
 
AWS
 
Step 2
 
Create a Windows or web application whatever you want. I have created a Windows.Form application and have taken two radio buttons and one submit button to send a notification.
 
AWS
 
Step 3
 
After this, you need to setup your project to use the Amazon Services. First, you have to specify the Access Key, Secret Key, and Region in web.config in case you are using the web application; and in app.config if you are using the Windows application.
  1. <appSettings>  
  2.     <add key="AWSAccessKey" value="************************" />  
  3.     <add key="AWSSecretKey" value="*******************************************" />  
  4.     <add key="AWSRegion" value="us-east-1" />        
  5. </appSettings>  
Remember, the key name must be in the case as mentioned.
 
Step 4
 
After this, you need to install a few of the necessary .dll files from the NuGet Package Manager. You can add these required libraries from the NuGet Package Manager or PM> console by typing the name of the libraries, as shown in references.
 
AWS
 
Now, it's time to write a few lines of code for the same.
  1. using System;  
  2. using System.Windows.Forms;  
  3. using Newtonsoft.Json;  
  4. using Amazon.SimpleNotificationService;  
  5. using Amazon.SimpleNotificationService.Model;  
  6. namespace AwsSnsPoc {  
  7.     public partial class Form1: Form {  
  8.         public Form1() {  
  9.             InitializeComponent();  
  10.         }  
  11.         private void SendNotificationToDevice_Click(object sender, EventArgs e) {  
  12.             lblMsg.Visible = false;  
  13.             if (!iosDevice.Checked && !andriodDevice.Checked) {  
  14.                 return;  
  15.             }  
  16.             sendnotification();  
  17.         }  
  18.         private void sendnotification() {  
  19.             // this endpoint is for android devices   
  20.             var gcmARN = "arn:aws:sns:us-east-1:501401665234:endpoint/GCM/nameoftopic/***********************";  
  21.             // this endpoint is for ios devices   
  22.             var apnsARN = "arn:aws:sns:us-east-1:502682123213:endpoint/APNS_SANDBOX/nameoftopic/*************";  
  23.             var checkedButton = iosDevice.Checked;  
  24.             // Creating the SNS client   
  25.             var snsClient = new AmazonSimpleNotificationServiceClient();  
  26.             // Creating the topic request and the topic and response  
  27.             var topicRequest = new CreateTopicRequest {  
  28.                 Name = "TestSNSTopic"  
  29.             };  
  30.             var topicResponse = snsClient.CreateTopic(topicRequest);  
  31.             var topicAttrRequest = new SetTopicAttributesRequest {  
  32.                 TopicArn = topicResponse.TopicArn,  
  33.                     AttributeName = "SNSTopic",  
  34.                     AttributeValue = "SNS Test AttrValue"  
  35.             };  
  36.             snsClient.SetTopicAttributes(topicAttrRequest);  
  37.             // Subscribe to the endpoint of the topic  
  38.             var subscribeRequest = new SubscribeRequest() {  
  39.                 TopicArn = topicResponse.TopicArn,  
  40.                     Protocol = "application"// important to chose the protocol as I am sending notification to applications I have chosen application here.  
  41.                     Endpoint = iosDevice.Checked ? apnsARN : gcmARN  
  42.             };  
  43.             var res = snsClient.Subscribe(subscribeRequest);  
  44.             RootObject reqObj = new RootObject();  
  45.             // Publishing the request to the endpoint (takecare of the protocol that is must is sending the json then use json else use sns, email, sqs etc. as per your requirement)   
  46.             PublishRequest publishReq = new PublishRequest() {  
  47.                 TargetArn = subscribeRequest.Endpoint,  
  48.                     MessageStructure = "json",  
  49.                     Message = JsonConvert.SerializeObject(reqObj)  
  50.             };  
  51.             PublishResponse response = snsClient.Publish(publishReq);  
  52.             if (response != null && response.MessageId != null) {  
  53.                 lblMsg.Visible = true;  
  54.                 lblMsg.Text = "Notification Send Successfully";  
  55.             }  
  56.         }  
  57.     }  
  58.     public class Aps {  
  59.         public string alert {  
  60.             get;  
  61.             set;  
  62.         }  
  63.         public string url {  
  64.             get;  
  65.             set;  
  66.         }  
  67.     }  
  68.     public class APNS {  
  69.         public Aps aps {  
  70.             get;  
  71.             set;  
  72.         }  
  73.     }  
  74.     public class GCMData {  
  75.         public string message {  
  76.             get;  
  77.             set;  
  78.         }  
  79.         public string url {  
  80.             get;  
  81.             set;  
  82.         }  
  83.     }  
  84.     public class GCM {  
  85.         public GCMData data {  
  86.             get;  
  87.             set;  
  88.         }  
  89.     }  
  90.     public class RootObject {  
  91.         public string @default {  
  92.             get;  
  93.             set;  
  94.         }  
  95.         public string APNS_SANDBOX {  
  96.             get;  
  97.             set;  
  98.         }  
  99.         public string GCM {  
  100.             get;  
  101.             set;  
  102.         }  
  103.         public RootObject() {  
  104.             @default = "This is the default message which must be present when publishing a message to a topic. The default message will only be used if a message is not present for one of the notification platforms.";  
  105.             APNS apns = new APNS() {  
  106.                 aps = new Aps() {  
  107.                     alert = "Check out these awesome deals!",  
  108.                         url = "www.amazon.com"  
  109.                 }  
  110.             };  
  111.             GCM gcm = new GCM() {  
  112.                 data = new GCMData() {  
  113.                     message = "Check out these awesome deals!",  
  114.                         url = "www.amazon.com"  
  115.                 }  
  116.             };  
  117.             this.APNS_SANDBOX = JsonConvert.SerializeObject(apns);  
  118.             this.GCM = JsonConvert.SerializeObject(gcm);  
  119.         }  
  120.     }  
  121.     //NOTE : One of the most important thing that you need to take care of that is you have to be very careful while forming the Object of the JSON request that you want to send like as I have given the JSON and the Object formation of that too.  
  122.     //the sample of the notification message that we want to send, in Android devices we must get the android deals //message on the notification     
  123.     // {  
  124.     // "default": "This is the default message which must be present when publishing a message to a topic. The default message will only be used if a message is not present for one of the notification platforms.",  
  125.     // "APNS": {"aps":{"alert": "Check out these awesome ios deals!","url":"www.amazon.com"} },  
  126.     // "GCM": {"data":{"message":"Check out these awesome android deals!","url":"www.amazon.com"}},  
  127.     //}  
  128.     #  
  129.     endregion  
  130. }  
If you get the message "Notification send successfully", you have successfully integrated this SNS Service in your application.
 
For the live testing on your devices, you must have to be ready with your device (IOS/Android) on which you are going to test this. I hope, you will enjoy implementing this awesome service of Amazon.
 
If you have any query or questions, I will be happy to give the solution,
 
For the implementation of amazon queue services, you can referer:
Amazon Simple Queue Services using C# 
 
Happy coding! Enjoy.


Similar Articles