AWS – SNS

SNS is a publisher/subscriber messaging service. SNS makes easier for you to set up, operate, and send notifications from the cloud. It pushes notifications to Apple, Google, Fire OS, and Windows devices. Besides pushing notifications to mobile devices, SNS can also deliver messages to SMS text message, email (SES), SQS, or to any HTTP endpoint.

SNS notifications can trigger Lambda functions which take the notifications as payloads.

Amazon SNS isn’t currently compatible with FIFO queues but standard queues can subscribe to a topic. When you subscribe an Amazon SQS queue to an Amazon SNS topic, Amazon SNS uses HTTPS to forward messages to Amazon SQS.

SNS provides topics where subscribers can subscribe to and receive notifications. One topic can support deliveries to multiple endpoint types. Like for example if you group together iOS, Android, and SMS into a topic, when you publish to that topic all devices will get the same message.

SNS text messaging offers 100 free messages every month. Once you have gone over 100 messages then you will be charged with $0.00645 a message. This price varies from phone company to phone company.

Create a topic

	@Override
	public String createTopic(String name) {
		final CreateTopicRequest createTopicRequest = new CreateTopicRequest(name);
		CreateTopicResult createTopicResult = amazonSNS.createTopic(createTopicRequest);

		return createTopicResult.getTopicArn();
	}

Subscribe to a topic

	@Override
	public boolean subscribePhone(String topicArn, String phone) {
		final SubscribeRequest subscribeRequest = new SubscribeRequest(topicArn, "sms", "+1"+phone);
		amazonSNS.subscribe(subscribeRequest);
		return true;
	}

	@Override
	public boolean subscribeEmail(String topicArn, String email) {
		final SubscribeRequest subscribeRequest = new SubscribeRequest(topicArn, "email", email);
		amazonSNS.subscribe(subscribeRequest);
		return true;
	}
	@Override
	public boolean subscribeQueue(String topicArn, String queueUrl) {
		String subscriptionArn = Topics.subscribeQueue(amazonSNS, amazonSQS, topicArn, queueUrl);
		return (subscriptionArn!=null && subscriptionArn.isEmpty()==false) ? true : false;
	}

Unsubscribe from a topic

	@Override
	public boolean unsubscribe(String subscriptionArn) {
		UnsubscribeRequest unsubscribeRequest = new UnsubscribeRequest(subscriptionArn);
		
		UnsubscribeResult unsubscribeResult = amazonSNS.unsubscribe(unsubscribeRequest);
		
		return (unsubscribeResult!=null) ? true : false;
	}

Send a message to a topic

	@Override
	public boolean sendMsgToTopic(String topicArn, String msg) {
		final PublishRequest publishRequest = new PublishRequest(topicArn, msg);
		final PublishResult publishResponse = amazonSNS.publish(publishRequest);
		return (publishResponse.getMessageId()!=null) ? true : false;
	}

Send an SMS message to a phone

	@Override
	public boolean text(String phone, String message) {
		PublishResult result = amazonSNS.publish(new PublishRequest()
                .withMessage(message)
                .withPhoneNumber("+1"+phone)
                .withMessageAttributes(new HashMap<String, MessageAttributeValue>()));
		
		return (result.getMessageId()!=null) ? true : false;
	}

Source code on Github

AWS SNS developer guide

AWS SNS API guide




Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!

required
required


Leave a Reply

Your email address will not be published. Required fields are marked *