Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending Amazon SNS from my PHP server

I have an application both in Android and iOS platforms. Both of them are registered with Amazon SNS. This is successfully done, because if I have the device tokens, then I can login to my applications dashboard in Amazon, and can send SNS from their console.

I want it make it automated. I mean have my own PHP admin site (and API) for the applications. I want add another page to the admin site, that can request the amazon SNS to send single payload with device identifier, registration keys and message body provided with the request.

First question - Is it possible? I have seen Urban Airship allows it, so it is usual that amazon also does?

Second question - What is the process? Since I am working on this for one of my client and all the docs are not accessible to me. My client is unable to explain it to amazon.

When I have registered my apps to amazon, shouldn't they provide me some keys and secrets that I can use to call their service over http?

like image 499
zinnuree Avatar asked Feb 04 '14 15:02

zinnuree


People also ask

Does PHP work with AWS?

PHP on AWSSimplifies use of AWS services by providing a set of libraries that are consistent and familiar for PHP developers. A Laravel plugin that integrates AWS services with your application using the latest version of AWS SDK For PHP.

How do I send to SNS?

To send a message In the Amazon SNS console , on the Topics page, choose the name of the topic to which you want to send SMS messages. On the topic details page, choose Publish message.

What is the difference between Amazon SNS and SQS?

Differences between SQS and SNS configurationsAn SQS message is stored on the queue for up to 14 days until it is successfully processed by a subscriber. SNS does not retain messages so if there are no subscribers for a topic, the message is discarded. SNS topics may broadcast to multiple targets.

Does Amazon SNS use twilio?

Twilio today announced a new collaboration with Amazon's AWS platform. The company says it is “helping AWS provide the delivery of SMS messages through the Amazon Simple Notification Service (SNS)” and notes that SNS users will now benefit from Twilio's experience in sending bulk messages.


2 Answers

Yes, it is possible. Download the Amazon Web Service (AWS) PHP SDK from here and follow their instructions to use this in you web server API. Get the Platform Application ARN's for both iOS and android, access key id and the secret key from Amazon console. And then try the code below and follow the commented out instructions:

<?php

require '<path to this file>/aws.phar';
use Aws\Sns\SnsClient;

if(isset($_POST['submit']))
{
    $push_message = $_POST['push_message'];

    if(!empty($push_message))
    {
        // Create a new Amazon SNS client
        $sns = SnsClient::factory(array(
            'key'    => '<access key>',
            'secret' => '<app secret>',
            'region' => '<region code>'
            ));

        // region code samples: us-east-1, ap-northeast-1, sa-east-1, ap-southeast-1, ap-southeast-2, us-west-2, us-gov-west-1, us-west-1, cn-north-1, eu-west-1

        $iOS_AppArn = "<iOS app's Application ARN>";
        $android_AppArn = "<android app's Application ARN>";

        // Get the application's endpoints
        $iOS_model = $sns->listEndpointsByPlatformApplication(array('PlatformApplicationArn' => $iOS_AppArn));
        $android_model = $sns->listEndpointsByPlatformApplication(array('PlatformApplicationArn' => $android_AppArn));

        // Display all of the endpoints for the iOS application
        foreach ($iOS_model['Endpoints'] as $endpoint)
        {
            $endpointArn = $endpoint['EndpointArn'];
            echo $endpointArn;
        }

        // Display all of the endpoints for the android application
        foreach ($android_model['Endpoints'] as $endpoint)
        {
            $endpointArn = $endpoint['EndpointArn'];
            echo $endpointArn;
        }

        // iOS: Send a message to each endpoint
        foreach ($iOS_model['Endpoints'] as $endpoint)
        {
            $endpointArn = $endpoint['EndpointArn'];

            try
            {
                $sns->publish(array('Message' => $push_message,
                    'TargetArn' => $endpointArn));

                echo "<strong>Success:</strong> ".$endpointArn."<br/>";
            }
            catch (Exception $e)
            {
                echo "<strong>Failed:</strong> ".$endpointArn."<br/><strong>Error:</strong> ".$e->getMessage()."<br/>";
            }
        }

        // android: Send a message to each endpoint
        foreach ($android_model['Endpoints'] as $endpoint)
        {
            $endpointArn = $endpoint['EndpointArn'];

            try
            {
                $sns->publish(array('Message' => $push_message,
                    'TargetArn' => $endpointArn));

                echo "<strong>Success:</strong> ".$endpointArn."<br/>";
            }
            catch (Exception $e)
            {
                echo "<strong>Failed:</strong> ".$endpointArn."<br/><strong>Error:</strong> ".$e->getMessage()."<br/>";
            }
        }
    }
}   
?>

The code is tested and it works, feel free to change as your need.

like image 176
Shabib Avatar answered Oct 07 '22 04:10

Shabib


If you want to send alert sound and badge number with custom payload replace this code block // iOS: Send a message to each endpoint foreach ($iOS_model['Endpoints'] as $endpoint)

with this code block

    foreach ($iOS_model['Endpoints'] as $endpoint)
{
    $endpointArn = $endpoint['EndpointArn'];

    try
    {
        $sns->publish(array(
        'TargetArn' => $endpointArn,
        'MessageStructure' => 'json',
        'Message' => json_encode(array(
            'default' => $title,
            'APNS_SANDBOX' => json_encode(array(
                'aps' => array(
                    'alert' => $title,
                    'sound' => 'default',
                    'badge' => 1
                    ),
                    // Your custom payload if needed
                    'whatever' => 'here',
                    'andwhatever' => 'here'
                    ))

            ))
    ));


        echo "1";//Success push
    }
    catch (Exception $e)
    {
        echo "2";//Failed push
    }
}
like image 21
Alex McPherson Avatar answered Oct 07 '22 05:10

Alex McPherson