Amazon Simple Notification Service (Amazon SNS)
Amazon SNS is a web service that makes it easy for the developers to publish messages from an application and immediately deliver them to subscribers or other applications. It is designed to make web-scale computing easier for developers.Amazon SNS provides a simple API that allow developers to create and manage topics, subscribe endpoints, and publish messages. The messages can be delivered to different protocols (ie HTTP, email etc) also it can be delivered to SQS (Amazon SQS), which client can decide. Multiple subscription for a single topic is also possible.
Amazon Simple Queue Service (Amazon SQS)
Amazon Simple Queue Service (Amazon SQS) offers a reliable, highly scalable hosted queue for storing messages as they travel between computers. Amazon SQS works by exposing Amazon’s web-scale messaging infrastructure as a web service. Any computer on the Internet can add or read messages without any installed software or special firewall configurations. Components of applications using Amazon SQS can run independently, and do not need to be on the same network, developed with the same technologies, or running at the same time.
* Developers can create an unlimited number of Amazon SQS queues with an unlimited number of messages.
- A queue can be created in 4 regions – US East (Northern Virginia), US West (Northern California), EU (Ireland) and Asia Pacific (Singapore) regions.
- The message body can contain up to 64 KB of text in any format (default is 8KB).
- Messages can be retained in queues for up to 14 days (default is 4 days).
- Messages can be sent and read simultaneously.
* When a message is received, it becomes “locked” while being processed. This keeps other computers from processing the message simultaneously. If the message processing fails, the lock will expire and the message will be available again. In the case where the application needs more time for processing, the “lock” timeout can be changed dynamically via the ChangeMessageVisibility operation.
* Developers can access Amazon SQS through standards-based SOAP and Query interfaces.
* Developers can securely share Amazon SQS queues with others. Queues can be shared with other AWS accounts and Anonymously. Queue sharing can also be restricted by IP address and time-of-day.
UrbanAirShip
Urban Airship provides an easy, affordable way for mobile publishers and developers to deliver push notifications messages and in-app purchase content to their app users on multiple platforms. Urban Airship provides easy-to-integrate tools for app publishers.
If you are an application developer, you can communicate with your users , who have instlalled apps on their mobile devices, which is running on the Apple iOS, Android or Blackberry platforms , even if the app is not open.
Push notifications travel on a data network, on a user’s data plan. They do not require or use SMS, nor do they incur charges for the user on the receiving end. It’s extremely cost effective.
SNS to SQS , PushNotification using Urban Airship with ,sample PHP scripts :
1) Download AWS SDK for PHP from http://aws.amazon.com/sdkforphp/ and use this library for building our script
2) Create a SNS queue , using the AMAZON credentials , subscribe to email endpoint
[php]
<?php
require_once ‘../sdk.class.php’;
define(‘AWS_ACCESS_KEY’, ‘your aws access key’);
define(‘AWS_PRIVATE_KEY’,’your aws private key’);
sns = new AmazonSNS(AWS_ACCESS_KEY, AWS_PRIVATE_KEY); //creating Amazon SNS
$response = $sns->create_topic(‘topic-1′); //creating a new topic in the SNS
$topic_arn = (string) $response->body->CreateTopicResult->TopicArn; //grabing the topic arn
$response = $sns->subscribe($topic_arn, ’email’, ‘required email address’); //The given email address will get a subscription confirmation message , once the user confirms the subscription , the email address will receive the email notification if any message is published to this topic
$q = new CFBatchRequest(10);
$sns->batch($q)->publish($topic_arn, “Any message”); //message publish to the topic arn
$response = $sns->batch($q)->send();
?>
[/php]
3) Sending a SNS message to AMAZON SQS
[php]
<?php
require_once ‘../sdk.class.php’;
define(‘AWS_ACCESS_KEY’, ‘your aws access key’);
define(‘AWS_PRIVATE_KEY’,’your aws private key’);
$sns = new AmazonSNS(AWS_ACCESS_KEY, AWS_PRIVATE_KEY); //creating Amazon SNS
$response = $sns->create_topic(‘topic-1’); //creating a new topic in the SNS
$topic_arn = (string) $response->body->CreateTopicResult->TopicArn; //grabing the topic arn
$sqs = new AmazonSQS(AWS_ACCESS_KEY, AWS_PRIVATE_KEY);
$response = $sqs->create_queue(‘queue-1’); //creating Amazon SQS
$queue_url = (string) $response->body->CreateQueueResult->QueueUrl; //grabing the queue_url
$queue_arn = $sqs->get_queue_arn($queue_url); //grabing the queue_arn
$policy = new CFPolicy($sqs, array(
‘Version’ => ‘2008-10-17’,
‘Id’ => ‘sampleId’,
‘Statement’ => array(
array(
‘Resource’ => $queue_arn,
‘Effect’ => ‘Allow’,
‘Sid’ => ‘rule1’,
‘Action’ => ‘sqs:*’,
‘Condition’ => array(
‘StringEquals’ => array(
‘aws:SourceArn’ => $topic_arn
)
),
‘Principal’ => array(
‘AWS’ => ‘*’
)
)
)
));
$response = $sqs->set_queue_attributes($queue_url, array(
array(‘Name’ => ‘Policy’, ‘Value’ => $policy->get_json())
)); //setting the Policy to SQS
$response = $sns->subscribe($topic_arn, ‘sqs’, $queue_arn); //subscribing the topic to sqs . Now a new message to SNS, will push the message into the SQS
$q = new CFBatchRequest(10);
$sns->batch($q)->publish($topic_arn, “Any message”); //message publish to the topic arn
$response = $sns->batch($q)->send();
?>
[/php]
4) Sending a Push Notification Message along with Push to SQS
[php]
<?php
require_once ‘../sdk.class.php’;
define(‘AWS_ACCESS_KEY’, ‘your aws access key’);
define(‘AWS_PRIVATE_KEY’,’your aws private key’);
$sns = new AmazonSNS(AWS_ACCESS_KEY, AWS_PRIVATE_KEY); //creating Amazon SNS
$response = $sns->create_topic(‘topic-1’); //creating a new topic in the SNS
$topic_arn = (string) $response->body->CreateTopicResult->TopicArn; //grabing the topic arn
$sqs = new AmazonSQS(AWS_ACCESS_KEY, AWS_PRIVATE_KEY);
$response = $sqs->create_queue(‘queue-1’); //creating Amazon SQS
$queue_url = (string) $response->body->CreateQueueResult->QueueUrl; //grabing the queue_url
$queue_arn = $sqs->get_queue_arn($queue_url); //grabing the queue_arn
$policy = new CFPolicy($sqs, array
‘Version’ => ‘2008-10-17’,
‘Id’ => ‘sampleId’,
‘Statement’ => array(
array(
‘Resource’ => $queue_arn,
‘Effect’ => ‘Allow’,
‘Sid’ => ‘rule1’,
‘Action’ => ‘sqs:*’,
‘Condition’ => array(
‘StringEquals’ => array(
‘aws:SourceArn’ => $topic_arn
)
),
‘Principal’ => array(
‘AWS’ => ‘*’
)
)
)
));
$response = $sqs->set_queue_attributes($queue_url, array(
array(‘Name’ => ‘Policy’, ‘Value’ => $policy->get_json())
)); //setting the Policy to SQS
$response = $sns->subscribe($topic_arn, ‘sqs’, $queue_arn); //subscribing the topic to sqs . Now a new message to SNS, will push the message into the SQS
$response = $sns->subscribe($topic_arn, ‘http’, “http url”); //for ex. push_msg.php is residing in the EC2 server, which can be accessable as
//http://your_ec2_server.com/push_msg.php
$q = new CFBatchRequest(10);
$sns->batch($q)->publish($topic_arn, “Any message”); //message publish to the topic arn
$response = $sns->batch($q)->send();
?>
[/php]
Your push_msg.php can be as follows
[php]
<?php
if ( $_SERVER[‘REQUEST_METHOD’] === ‘POST’ )
{
$json = trim(file_get_contents(‘php://input’));
$obj = json_decode($json);
$type = $obj->{‘Type’};
$message = $obj->{‘Message’};
$message_id = $obj->{‘MessageId’};
if ( $obj->{‘Type’} === ‘SubscriptionConfirmation’ )
{
$subcriptionurl = $obj->{‘SubscribeURL’};
$ch = curl_init($subcriptionurl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
}
$c = curl_init();
curl_setopt($c, CURLOPT_HTTPHEADER, array(‘Accept: application/json’, ‘Content-Type: application/json’));
curl_setopt($c, CURLOPT_URL, ‘https://go.urbanairship.com/api/push/’);
curl_setopt($c, CURLOPT_USERPWD, ‘<YOUR URBANAIRSHIP Application Key>:<YOUR URBANAIRSHIP Application Master Secret>’);
curl_setopt($c, CURLOPT_POST, True);
curl_setopt($c, CURLOPT_HEADER, False);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
$notifications = array (‘device_tokens’=> array (‘<THE TOKEN OF THE DEVICE, WHICH SHOULD GET THE NOTIFICATION MSG>’), ‘aps’=> array (‘alert’=> $message , ‘badge’=>1, ‘sound’=>’default’), ‘msg’=> array ( ‘id’=> $message_id)) ;
curl_setopt($c, CURLOPT_POSTFIELDS, json_encode($notifications));
$content = curl_exec($c);
}
?>
[/php]
Update (2016) : Urban Airship has evolved much and a detail on Urban Airship push notification integration is updated in the blog.
Nice article…..I am a happy user of Amazon S3 I always enjoy learning about it,I use tool named Bucket Explorer and I implement SNS using this tool and its really handy to use the tool and its nice to use it….
thank you for this stuff
That’s the most useful, plain English, simple explanation I’ve seen on using and integrating AWS SNS and SQS. And code to boot. Thanks!
And now, as of 11/2012, SNS can be sent to SMS. It’s freakin Acronymmania!
Thanks Eric …
Glad to know that it helped you … 🙂