AWS SNS delivery status

I am new in Aws, I am using Aws SNS to send notification, i am sending notifications to different topic not to endpoint. This is working perfectly.

When i publish notification, i got array like

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

I am using php at server side,
How can i get notification delivery status of all recepients by this message id?

Thanks in Anticipants.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

You are asking how to obtain notification delivery status of messages sent via Amazon SNS.

The Using Amazon SNS Topic Attributes for Message Delivery Status documentation says:

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.

I could not find a specific API call to request status by message_id. Instead, it appears that the logging information is sent to CloudWatch Logs. You would need to parse the logs to discover the status.

Solution 2

  1. Enable ‘Delivery status logging’ to AWS Cloudwatch More Info
  2. Adjust the user (used in script) to access the AWS Cloudwatch log
  3. Read the log entry from cloudwatch with awssdk for php

See the sample code below

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>";

I believe it will help someone

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply