DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Spring Reactive Microservices: A Showcase
  • Flex for J2EE Developers: The Case for Granite Data Services
  • Top ALM Tools and Solutions Providers
  • How To REST With Rails and ActiveResource: Part Three

Trending

  • Efficient API Communication With Spring WebClient
  • How To Introduce a New API Quickly Using Quarkus and ChatGPT
  • Introducing Graph Concepts in Java With Eclipse JNoSQL
  • Supervised Fine-Tuning (SFT) on VLMs: From Pre-trained Checkpoints To Tuned Models

Amazon SNS (Simple Notification Service) Using C# and Visual Studio

In this post, you will learn how to use a new service from Amazon to send notifications to your users on iOS and Android devices.

By 
Amar Srivastava user avatar
Amar Srivastava
·
Jun. 22, 18 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
34.7K Views

Join the DZone community and get the full member experience.

Join For Free

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

  1. Create 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 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.

AWS

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.

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

AWS

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 {  
                TopicArn = 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.

Web Service Notification service application

Opinions expressed by DZone contributors are their own.

Related

  • Spring Reactive Microservices: A Showcase
  • Flex for J2EE Developers: The Case for Granite Data Services
  • Top ALM Tools and Solutions Providers
  • How To REST With Rails and ActiveResource: Part Three

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!