从我的PHP服务器发送亚马逊SNS [英] Sending Amazon SNS from my PHP server

查看:591
本文介绍了从我的PHP服务器发送亚马逊SNS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我都在Android和iOS平台的应用程序。他们两人都是注册与Amazon SNS。这是做成功了,因为如果我有设备令牌,那么我就可以登录亚马逊我的应用程序仪表板,并且可以从他们的控制台发送SNS。

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.

我希望它自动化的化妆。我的意思是有我自己的PHP管理网站(和API)的应用程序。我要添加其他页面的管理工具,可以请求亚马逊SNS派单的有效载荷与设备标识符,注册密钥和消息体提供了请求。

I want the 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.

当我已经注册了我的应用程序,以亚马逊,不应该他们给我提供了一些键和秘密,我可以用它来拨打他们的服务通过HTTP?

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?

推荐答案

是的,这是可能的。 从下载亚马逊网络服务(AWS)PHP SDK这里并按照他们的指示使用这个在你的Web服务器API。获取平台应用程序ARN的为iOS和Android,访问密钥ID和从Amazon控制台密钥。再试试下面的code,并按照注释掉的说明:

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 ($iOS_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/>";
            }
        }
    }
}   
?>

在code测试和它的作品,随意改变,因为你的需要。

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

这篇关于从我的PHP服务器发送亚马逊SNS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆