从我的PHP服务器发送Amazon SNS

 皇家陈小小2_328 发布于 2023-01-29 14:04

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

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

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

第二个问题 - 过程是什么?因为我正在为我的一个客户工作,所以我无法访问所有文档.我的客户无法向亚马逊解释.

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

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

    2023-01-29 14:07 回答
  • 对的,这是可能的.从此处下载Amazon Web Service(AWS)PHP SDK,并按照其说明在您的Web服务器API中使用它.获取适用于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/>";
                }
            }
        }
    }   
    ?>
    

    代码经过测试并且可以正常运行,随时根据需要进行更改.

    2023-01-29 14:07 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有