Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)

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 log in to my applications dashboard on Amazon, and can send SNS from their console.

I want it to make it automated. I mean to have my own PHP admin site (and API) for the applications. I won't add another page to the admin site, that can request the amazon SNS to send a single payload with a 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 with some keys and secrets that I can use to call their service over http?

1 Answer

0 votes
by (44.4k points)

Yes, it is possible. Download the Amazon Web Service (AWS) PHP SDK from here and follow their instructions to use this in your 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.

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
asked Oct 10, 2019 in AWS by yuvraj (19.1k points)
0 votes
1 answer
0 votes
1 answer
asked Nov 29, 2020 in AWS by devin (5.6k points)

Browse Categories

...