从我的PHP服务器发送Amazon SNS

16

我有一个应用程序可以在Android和iOS平台上运行。它们都已经注册了亚马逊SNS。这个操作成功完成了,因为如果我拥有设备标记,我就可以登录到我的应用程序仪表板中,并可以从其控制台发送SNS。

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

第一个问题-这可能吗?我看到Urban Airship允许这样做,所以亚马逊也应该可以吧?

第二个问题-流程是什么?由于我正在为我的客户之一而开发,而所有文档对我来说都无法访问。我的客户无法向亚马逊解释这个问题。

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

3个回答

27

是的,这是可能的。 从这里下载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
@Moebius 如果你仍需要帮助,请查看我下面的答案。 - Alex McPherson
1
嘿,有什么方式可以联系到你吗?我想讨论一下关于SNS和PHP以及管理推送通知的问题。我已经设置好了一切,包括AWS SDK设置和服务器运行。我也能够从AWS控制台发送推送通知,但现在正在尝试在php中管理它。我是一名开发人员,会编写和实现任何伪代码,但我想与您联系,因为在阅读了您的答案后,您似乎知道自己在说什么。您可以通过Skype或电子邮件与我联系吗?kataria.pavan@gmail.com或pavankataria - Pavan
你能从以下代码中捕获错误吗?try { $sns->publish(array('Message' => $push_message, 'TargetArn' => $endpointArn)); } catch(Exception $e){ } 我认为这不会阻止应用程序关闭。 - AHMED.D

5
如果您希望在自定义有效载荷中发送警报声和标记数字,请使用以下代码块替换此代码块: // 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编程方式实现,假设它会触发用户确认以启用/授权我的应用程序的推送通知?我所有的设备ID / 令牌(Android和iOS)都进入mysql数据库,所以我希望我可以循环遍历该数据库以分别发送给iOS用户和Android用户...下载SDK并花费接下来的一两天时间! - tamak
嗨@tamak,您肯定可以按照这里的指南https://medium.com/aws-activate-startup-blog/a-guide-to-amazon-simple-notification-service-mobile-push-self-registration-for-ios-a2502e8d5fbd进行操作。 - Alex McPherson
如何在iOS中设置通知的标题和正文? - Vaibhav Saran

0

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

                $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
                ));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接