从我的 PHP 服务器发送 Amazon SNS

2022-08-31 00:38:16

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

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

第一个问题 - 可能吗?我看过Urban Airship允许它,所以亚马逊通常也这样做吗?

第二个问题 - 过程是什么?由于我正在为我的一个客户处理这个问题,并且所有文档都无法访问。我的客户无法向亚马逊解释。

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


答案 1

是的,这是可能的。从此处下载 Amazon Web Service (AWS) PHP 开发工具包,并按照其说明在您的 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/>";
            }
        }
    }
}   
?>

代码经过测试并正常工作,您可以根据需要随意更改。


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

推荐