【问题标题】:Sending Amazon SNS from my PHP server从我的 PHP 服务器发送 Amazon SNS
【发布时间】:2014-02-28 16:47:01
【问题描述】:

我在 Android 和 iOS 平台都有一个应用程序。他们都在 Amazon SNS 上注册。这已成功完成,因为如果我有设备令牌,那么我可以登录到我在 Amazon 中的应用程序仪表板,并可以从他们的控制台发送 SNS。

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

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

第二个问题 - 流程是什么?因为我正在为我的一位客户做这件事,所以我无法访问所有文档。我的客户无法向亚马逊解释。

当我将我的应用程序注册到亚马逊时,他们不应该向我提供一些我可以用来通过 http 调用他们的服务的密钥和秘密吗?

【问题讨论】:

    标签: php android ios urbanairship.com amazon-sns


    【解决方案1】:

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

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

    代码已经过测试并且可以正常工作,请随时根据需要进行更改。

    【讨论】:

    • 我想知道如何发送带有徽章和声音的 APNS 消息。我现在正在寻找那个答案。
    • @Moebius 如果您仍然需要帮助,请在下面查看我的答案。
    • 嘿,有什么方法可以联系到你吗?我想讨论一下 SNS 和 PHP 以及管理推送通知。我已经完成了所有设置、AWS sdk 设置以及正在运行的服务器。我还可以从 AWS 控制台发送推送通知,但现在尝试在 php 中管理它。我是一名开发人员,将知道如何编写和实现任何伪代码,但我想与您联系,因为您在阅读您的答案后似乎知道您在说什么,您可以通过以下方式与我联系吗? Skype 或电子邮件? kataria.pavan@gmail.com 或 pavankataria
    • 你能捕捉到来自try { $sns-&gt;publish(array('Message' =&gt; $push_message, 'TargetArn' =&gt; $endpointArn)); } catch(Exception $e){ } 的错误吗?我认为这不会阻止应用程序关闭
    【解决方案2】:

    如果您想使用自定义有效负载发送警报声音和徽章编号,请替换此代码块 // iOS: Send a message to each endpoint foreach ($iOS_model['Endpoints'] as $endpoint)

    使用此代码块

        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
        }
    }
    

    【讨论】:

    • 我迫不及待地想回到我的生产机器上进行测试。因此,我一直在 Amazon SNS 控制台中四处寻找,我看到了如何(手动)订阅设备令牌(特别是 ios)......可以通过 PHP 以编程方式完成,假设它会触发用户确认启用/ 为我的应用授权推送通知?我的所有设备 ids/tokens(android 和 ios)都进入了 mysql db,所以我希望我可以循环访问该数据库以分别发送给 ios 用户和 android 用户......下载 SDK 并在接下来的一两天使用它!
    • 嗨@tamak 你肯定可以在这里遵循本指南medium.com/aws-activate-startup-blog/…
    • 如何设置iOS通知的标题和正文?
    【解决方案3】:

    我相信向单个设备或用户发送推送通知的最简单方法是通过此代码

                    $snsClient = Aws\Sns\SnsClient::factory(array(
                        'credentials' => array(
                            'key'    => AMAZON_KEY,
                            'secret' => AMAZON_SECRET,
                        ),
                        'region'  => AMAZON_REIGON
                    )); 
    
    
              //you should have variable that have user end single point .
    
               $result = $snsClient->publish(array(
    
                        'Message' => "push text message",
                        'TargetArn' => $user_end_point
                    ));
    

    【讨论】:

      猜你喜欢
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-09
      相关资源
      最近更新 更多