Amazon SNS (Simple Notification Service) Using C# and Visual Studio
SNS (Amazon Simple Notification Services)
Amazon SNS (Amazon Simple Notification Services) is a notification service used to send the notifications 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, notifications, etc.
Steps to Implementing Amazon SNS
Create topic - you will get the Topic ARN for which you have to send the notification or send the messages.
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.
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.
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.
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 is 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.
Step 2
Create a Windows or web application; whichever you prefer. I have created a Windows.Form application and have made two radio buttons and one submit button to send notifications.
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 file in case you are using the web application, and in app.config if you are using the Windows application.
<appSettings> <add key="AWSAccessKey" value="************************" /> <add key="AWSSecretKey" value="*******************************************" /> <add key="AWSRegion" value="us-east-1" /> </appSettings>
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 the references.
Now, it's time to write a few lines of code for this functionality.
using System;
using System.Windows.Forms;
using Newtonsoft.Json;
using Amazon.SimpleNotificationService;
using Amazon.SimpleNotificationService.Model; namespace AwsSnsPoc
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void SendNotificationToDevice_Click(object sender, EventArgs e)
{
lblMsg.Visible = false; if (!iosDevice.Checked && !andriodDevice.Checked) { return; }
sendnotification();
}
private void sendnotification()
{
// this endpoint is for android devices
var gcmARN = "arn:aws:sns:us-east-1:501401665234:endpoint/GCM/nameoftopic/***********************";
// this endpoint is for ios devices
var apnsARN = "arn:aws:sns:us-east-1:502682123213:endpoint/APNS_SANDBOX/nameoftopic/*************";
var checkedButton = iosDevice.Checked;
// Creating the SNS client
var snsClient = new AmazonSimpleNotificationServiceClient();
// Creating the topic request and the topic and response
var topicRequest = new CreateTopicRequest { Name = "TestSNSTopic" };
var topicResponse = snsClient.CreateTopic(topicRequest);
var topicAttrRequest = new SetTopicAttributesRequest
{
opicArn = topicResponse.TopicArn,
AttributeName = "SNSTopic",
AttributeValue = "SNS Test AttrValue"
};
snsClient.SetTopicAttributes(topicAttrRequest);
// Subscribe to the endpoint of the topic
var subscribeRequest = new SubscribeRequest()
{
TopicArn = topicResponse.TopicArn,
Protocol = "application", // important to chose the protocol as I am sending notification to applications I have chosen application here.
Endpoint = iosDevice.Checked ? apnsARN : gcmARN
};
var res = snsClient.Subscribe(subscribeRequest);
RootObject reqObj = new RootObject();
// 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)
PublishRequest publishReq = new PublishRequest()
{
TargetArn = subscribeRequest.Endpoint MessageStructure = "json",
Message = JsonConvert.SerializeObject(reqObj)
};
PublishResponse response = snsClient.Publish(publishReq);
if (response != null && response.MessageId != null)
{
lblMsg.Visible = true;
lblMsg.Text = "Notification Send Successfully";
}
}
}
public class Aps
{
public string alert { get; set; }
public string url { get; set; }
}
public class APNS
{
public Aps aps { get; set; }
}
public class GCMData
{
public string message { get; set; }
public string url { get; set; }
}
public class GCM
{
public GCMData data { get; set; }
}
public class RootObject
{
public string @default { get; set; }
public string APNS_SANDBOX { get; set; }
public string GCM { get; set; }
public RootObject()
{
@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.";
APNS apns = new APNS()
{
aps = new Aps()
{
alert = "Check out these awesome deals!",
url = "www.amazon.com" }
};
GCM gcm = new GCM()
{
data = new GCMData()
{
message = "Check out these awesome deals!",
url = "www.amazon.com"
}
};
this.APNS_SANDBOX = JsonConvert.SerializeObject(apns);
this.GCM = JsonConvert.SerializeObject(gcm);
}
}
//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.
//the sample of the notification message that we want to send, in Android devices we must get the android deals
//message on the notification
// { // "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.", // "APNS": {"aps":{"alert": "Check out these awesome ios deals!","url":"www.amazon.com"} },
// "GCM": {"data":{"message":"Check out these awesome android deals!","url":"www.amazon.com"}}, //} # endregion
}
If you get the message, "Notification sent successfully," you have successfully integrated this SNS Service into your application.
For live testing on your devices, you must be ready with your device (iOS/Android) on which you are going to test this. I hope, you will enjoy implementing this awesome service from Amazon.
If you have any queries or questions, I will be happy to give the solution.
Happy coding! Enjoy.
Amazon SNS (Simple Notification Service) Using C# and Visual Studio的更多相关文章
- AWS系列-Amazon Simple Notification Service (SNS)
SNS是一项 Web 服务,用于协调和管理向订阅终端节点或客户交付或发送消息的过程.在 Amazon SNS 中有两种类型的客户端:发布者和订阅者,也称为生产者和消费者.发布者通过创建消息并将消息发送 ...
- Amazon SQS(Simple Queue Service) 简单介绍
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/hongchangfirst/article/details/25877059 SQS即Simple ...
- Visual Studio 2013中引入Web Service的简单方法visual studio 引用 wsdl
http://blog.csdn.net/wangzhongbo_24/article/details/49954191 Web Service有三种表示方式 三种方式分别为WSDL.Endpoint ...
- Amazon SNS移动推送更新——新增百度云推送和Windows平台支持
Amazon SNS(Simple Notification Service)是一种基于云平台的消息通知和推送服务. SNS提供简单的 Web 服务接口和基于浏览器的管理控制台让用户可以简易设置.执行 ...
- 【转载】在 Visual Studio 2012 中创建 ASP.Net Web Service
在 Visual Studio 2012 中创建 ASP.Net Web Service,步骤非常简单.如下: 第一步:创建一个“ASP.Net Empty Web Application”项目 创建 ...
- [AWS vs Azure] 云计算里AWS和Azure的探究(6) - Amazon Simple Storage Service 和 Microsoft Azure Blob Storage
这几天Nasuni公司出了一份报告,分析了各个云厂商的云存储的性能,包括Amazon S3,Azure Blob Storage, Google Drive, HP以及Rackspace.其中性能上A ...
- S3 服务(Simple Storage Service简单存储服务) 简介(与hdfs同一级)
图1 spark 相关 亚马逊云存储之S3(Simple Storage Service简单存储服务) (转 ) S3是Simple Storage Service的缩写,即简单存储服务.亚马逊的名 ...
- HTTPSQS(HTTP Simple Queue Service)消息队列
HTTPSQS(HTTP Simple Queue Service)是一款基于 HTTP GET/POST 协议的轻量级开源简单消息队列服务,使用 Tokyo Cabinet 的 B+Tree Key ...
- 管理ONS(Oracle Notification Service)
Onsctl Onsctl这个命令是用来管理ONS(Oracle Notification Service)是OracleClustser实现FAN Event Push模型的基础. Oracle N ...
随机推荐
- 用node.js express设置路径后 子路径下的页面访问静态资源路径出问题
在routes/news_mian.js 设置了访问news_main.html 的路径 '/',通知设置一个访问news-page.html的子路径'/newspage'子路径.但是在访问loacl ...
- Arcpy多线程热力图
起因是这样一段对话,领导:你会用脚本生成热力图图片吗?我:可以研究下.领导:那这个需求就给你了.我:...... 经过一番研究,研究出大概的思路,先将有经纬度的表中的数据筛选出表并生成 ...
- javaweb登陆过滤器实现
在web.xml中配置登陆过滤器: <!-- 配置登陆过滤器 --> <filter> <filter-name>loginFilter</filter-na ...
- 我想要革命想要解脱——bootstrap常见问题及解决方式
最近一个月,恍若隔世,天天加班,昨晚终于发版了,今天才喘一口气.有时候,即便你工作效率再怎么高,撸码再怎么快也无可避免的会加班.不信的话,可以先给你定一个交付时间,然后不断的给你加需求,就让你一个人做 ...
- Android下获取FPS的几种方法
FPS(Frames Per Second)是关乎Android用户体验最为重要的指标之一,而在VR中更是如此.为了评估VR系统.VR SDK及Unity应用的性能,通常会实时获取FPS并将其显示出来 ...
- MongoDB:配置与安装
一.配置环境 1.1进行安装下载msi文件 需要注意的一点是,在安装过程中的图示界面不要勾选左下角“安装MongoDB可视化工具”.这是一个客户端管理工具,在后面会具体描述其功能.由于安装时间非常长, ...
- Android 使用Glide加载网络图片等比例缩放
在做android图片加载的时候,由于手机屏幕受限,很多大图加载过来的时候,我们要求等比例缩放,比如按照固定的宽度,等比例缩放高度,使得图片的尺寸比例得到相应的缩放,但图片没有变形.显然按照andro ...
- IntelliJ IDEA简介及简单操作
IntelliJ IDEA简介 IDEA 全称IntelliJ IDEA,是用于java语言开发的集成环境(也可用于其他语言),IntelliJ在业界被公认为最好的java开发工具之一 ...
- Mysql增量写入Hdfs(二) --Storm+hdfs的流式处理
一. 概述 上一篇我们介绍了如何将数据从mysql抛到kafka,这次我们就专注于利用storm将数据写入到hdfs的过程,由于storm写入hdfs的可定制东西有些多,我们先不从kafka读取,而先 ...
- 06-Nodejs介绍
06-Nodejs介绍 打开Nodejs英文网:https://nodejs.org/en/ 中文网:http://nodejs.cn/ 我们会发现这样一句话: 翻译成中文如下: Node.js 是一 ...