AWS SNS 交付状态

AWS SNS delivery status

我是 Aws 的新手,我正在使用 Aws SNS 发送通知,我将通知发送到不同的主题而不是端点。这工作得很好。

当我收到 publish 通知时,我得到了类似

的数组
object(Aws\Result)#84 (1) {
    ["data":"Aws\Result":private]=>
       array(2) {
         ["MessageId"]=>
         string(36) "************-7a29-591f-8765-************"
         ["@metadata"]=>
         array(4) {
         ["statusCode"]=>
         int(200)
         ["effectiveUri"]=>
        string(40) "https://sns.ap-southeast-1.amazonaws.com"
        ["headers"]=>
        array(4) {
            ["x-amzn-requestid"]=>
            string(36) "************-b737-5831-abf4-************"
            ["content-type"]=>
            string(8) "text/xml"
            ["content-length"]=>
            string(3) "294"
            ["date"]=>
            string(29) "Fri, 28 Oct 2016 08:59:05 GMT"
        }
        ["transferStats"]=>
            array(1) {
               ["http"]=>
               array(1) {
               [0]=>
               array(0) {}
            }
        }
    }
}

我在服务器端使用 php, 我如何通过此 message id 获取所有收件人的通知发送状态?

感谢各位的期待

您问的是如何获取通过 Amazon SNS 发送的消息的通知传送状态

Using Amazon SNS Topic Attributes for Message Delivery Status 文档说:

Amazon SNS provides support to log the delivery status of notification messages sent to topics with the following Amazon SNS endpoints:

  • Application
  • HTTP
  • Lambda
  • SQS

After you configure the message delivery status attributes, log entries will be sent to CloudWatch Logs for messages sent to a topic subscribed to an Amazon SNS endpoint.

我找不到 message_id 对请求状态的特定 API 调用。相反,日志记录信息似乎被发送到 CloudWatch Logs。您需要解析日志以发现状态。

  1. 启用 'Delivery status logging' 到 AWS Cloudwatch More Info
  2. 调整用户(在脚本中使用)访问 AWS Cloudwatch 日志
  3. 使用 awssdk 从 cloudwatch 读取日志条目 php

查看下面的示例代码

require 'inc/awsSdkForPhp/aws-autoloader.php';
$params = array(
    'credentials' => array(
        'key' => '<YOUR KEY>',
        'secret' => '<YOUR SECRET>',
    ),
    'region' => 'us-east-1', //  your aws from SNS region
    'version' => 'latest'
);
$cwClient = new \Aws\CloudWatchLogs\CloudWatchLogsClient($params);
$queryRes = $cwClient->startQuery([
  'endTime' => 1621231180,  // UNIX TIMESTAMP 
  'logGroupName' => 'sns/us-east-1/***/DirectPublishToPhoneNumber', // YOUR LOG GROUP NAME
  'queryString' => 'fields @timestamp, status, @message
  | filter notification.messageId="5a419afc-c4b3-55b3-85f9-c3e7676b2dd2"', // YOUR MESSAGE ID
  'startTime' => 1620954551 // START UNIX TIMESTAMP 
]);

$qryID = $queryRes->get('queryId');
sleep(3); // To wait the execution to be completed.
$resultObj =  $cwClient->getQueryResults(array(
  'queryId' => $qryID, // REQUIRED
));

//echo "<pre>";print_r($resultObj);echo "</pre>";
$result = $resultObj->get('results');

$jsnRs = json_decode($result[0][2]['value']); // TO get the delivery array 

echo "<br>status :".$jsnRs->status;
echo "<br>phone Carrier :".$jsnRs->delivery->phoneCarrier;
echo "<br>provider Response :".$jsnRs->delivery->providerResponse;
echo "<pre>";print_r($jsnRs);echo "</pre>";

我相信它会对某人有所帮助