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

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

问题描述

我在 Android 和 iOS 平台上都有一个应用程序.他们都在亚马逊 SNS 上注册.这已成功完成,因为如果我有设备令牌,那么我就可以登录到我在 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.

我想让它自动化.我的意思是为应用程序拥有自己的 PHP 管理站点(和 API).我想向管理站点添加另一个页面,该页面可以请求亚马逊 SNS 发送带有设备标识符、注册密钥和随请求提供的消息正文的单个有效负载.

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.

第一个问题 - 有可能吗?我见过Urban Airship 允许的,所以通常亚马逊也允许?

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?

推荐答案

是的,这是可能的.从此处下载 Amazon Web Service (AWS) PHP SDK,并按照他们的说明在您的网络中使用它服务器 API.从 Amazon 控制台获取适用于 iOS 和 android 的平台应用程序 ARN、访问密钥 ID 和密钥.然后尝试下面的代码并按照注释掉的说明进行操作:

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 AwsSnsSnsClient;

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.

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

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