![](/img/trans.png)
[英]'SenderID' issues sending SMS on Amazon SNS (Official PHP SDK)
[英]Sending Amazon SNS from my PHP server
我在Android和iOS平台上都有应用程序。 它们都在Amazon SNS注册。 这是成功完成的,因为如果我有设备令牌,那么我可以登录到亚马逊的应用程序仪表板,并可以从他们的控制台发送SNS。
我想让它自动化。 我的意思是拥有自己的应用程序PHP管理站点(和API)。 我想向管理站点添加另一个页面,可以请求亚马逊SNS发送带有设备标识符,注册密钥和请求提供的消息体的单一有效负载。
第一个问题 - 有可能吗? 我见过Urban Airship允许它,所以亚马逊也常见吗?
第二个问题 - 过程是什么? 因为我正在为我的一个客户工作,所以我无法访问所有文档。 我的客户无法向亚马逊解释。
当我将我的应用程序注册到亚马逊时,他们不应该向我提供一些密钥和秘密,我可以使用它来通过http呼叫他们的服务吗?
对的,这是可能的。 从此处下载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/>";
}
}
}
}
?>
代码经过测试并且可以正常运行,随时根据需要进行更改。
如果要使用自定义有效负载发送警报声音和徽章编号,请替换此代码块// 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
}
}
我相信通过此代码向单个设备或用户发送推送通知的最简单方法
$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
));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.