dti70601 2014-02-04 15:56
浏览 169
已采纳

从我的PHP服务器发送Amazon 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.

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?

  • 写回答

3条回答 默认 最新

  • duanlinghe8417 2014-02-07 14:44
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch