Amazon Simple Notification Service (Amazon SNS) is a fast, fully-managed, push messaging service. Amazon SNS can deliver messages to mobile devices (i.e. iOS, Android). It transmits push notifications from backend server applications to mobile apps on Apple, Google and Kindle Fire devices using a simple API. You can send a message to a particular device or you can send a message to every device that is subscribed to a particular SNS App.
Best of all, you can start using this feature at no charge. The AWS Free Tier means all AWS customers can send one million push notifications per month across iOS, Android and Kindle platforms at no charge. After that, you pay $0.50 for every million publishes and $0.50 per million push deliveries.
How it works?
Handling Amazon SNS notification messages with PHP is simple. Here's what you need to do to create a mobile app that can receive push notifications:
- Create an app and register devices from the SNS tab of the AWS Management Console.
- Create GCM App for android device and APNS for iOS
- Start by clicking the Add a New App button:
- Enter the application's name, choose a push platform, and enter the credentials (in this case an API key for Google Cloud Messaging for Android) for the platform:
- Then, register the endpoints using the tokens supplied by each user device that registers for your notification service. You can integrate php script at server side to create endpoint dynamically for device token by calling SNS CreatePlatformEndpoint function. This function will return an ARN (Amazon Resource Name) that uniquely identifies the device. Send/Publish messages directly to a specific device by calling the Publish function with the device's ARN.
- Download AWS SDK for PHP use it to write a simple application to publish notifications over devices using SNS API.
<?php
// Load the AWS SDK for PHP
require '/aws-sdk.phar';
// Create a new Amazon SNS client
$sns = Aws\Sns\SnsClient::factory(array('key' => '...',
'secret' => '...',
'region' => 'us-east-1'));
// Get and display the platform applications
print("List All Platform Applications:\n");
$Model1 = $sns->listPlatformApplications();
foreach ($Model1['PlatformApplications'] as $App)
{
print($App['PlatformApplicationArn'] . "\n");
}
print("\n");
// Get the Arn of the first application
$AppArn = $Model1['PlatformApplications'][0]['PlatformApplicationArn'];
// Get the application's endpoints
$Model2 = $sns->listEndpointsByPlatformApplication(array('PlatformApplicationArn' => $AppArn));
// Display all of the endpoints for the first application
print("List All Endpoints for First App:\n");
foreach ($Model2['Endpoints'] as $Endpoint)
{
$EndpointArn = $Endpoint['EndpointArn'];
print($EndpointArn . "\n");
}
print("\n");
// Send a message to each endpoint
print("Send Message to all Endpoints:\n");
foreach ($Model2['Endpoints'] as $Endpoint)
{
$EndpointArn = $Endpoint['EndpointArn'];
try
{
$sns->publish(array('Message' => 'Hello from PHP',
'TargetArn' => $EndpointArn));
print($EndpointArn . " - Succeeded!\n");
}
catch (Exception $e)
{
print($EndpointArn . " - Failed: " . $e->getMessage() . "!\n");
}
}
?>
And voila! The message will appear on the device!