AWS SNS:为什么我的“ ghost”订阅未显示在Topic中

时间:2019-08-20 15:15:45

标签: amazon-web-services push-notification amazon-sns aws-php-sdk

我在使用Amazon AWS SNS时遇到一个奇怪的问题:使用aws-php-sdk创建订阅和主题时,总是存在“幽灵”或“不可见”订阅。

AWS Console screenshot

如您所见,此订阅存在于“订阅”标签中。但是,当我单击主题链接(此处为cav_56826)时,我看不到任何订阅

AWS Topic screenshot

你们已经遇到过类似的问题吗?这怎么可能发生?

这是我的简化代码:

  try
    {
        $arn = "arn:aws:sns:eu-west-1:XXXXXXXXXXXXXXXXX:app/APNS_VOIP_SANDBOX/ios_cav";
        $topics = array("allUsers", "cav_56826");
        $topicsToSubcribe = array();

        foreach ($topics as $topic)
        {
            $res = $this->snsClient->createTopic(['Name' => $topic]);
            if ($res->get('@metadata')['statusCode'] == 200)
            {
                array_push($topicsToSubcribe, $res->get('TopicArn'));
            }
            else
            {
                throw new Exception("An error occured during Amazon SNS createTopic", $res->get('@metadata')['statusCode']);
            }
        }

        $SNSEndPointData = $this->snsClient->createPlatformEndpoint([
            'PlatformApplicationArn' => $arn,
            'Token'                  => $token
        ]);

        foreach ($topicsToSubcribe as $topic)
        {
            $this->snsClient->subscribe([
                'Protocol' => "application",
                'Endpoint' => $SNSEndPointData->get('EndpointArn'),
                'TopicArn' => $topic
            ]);
        }
    }
    catch (\Exception $e)
    {
       // Logs some errors
    }

1 个答案:

答案 0 :(得分:2)

在进行了一些更改之后,使用PHP 5.6.40和AWS开发工具包PHP 3.122.0(已找到here)和下面的代码,我可以看到预期的/正确的行为正在发生。

<?php

require '/usr/src/myapp/aws.phar';
$sdk = new Aws\Sdk([
    'region'   => 'us-east-1',
    'version'  => 'latest',
]);

$snsClient = $sdk->createSns();
$token = "XX:YY";

try
    {
        $arn = "arn:aws:sns:us-east-1:360479286475:app/GCM/test-stackoverflow";
        $topics = array("allUsers", "cav_56826");
        $topicsToSubcribe = array();

        foreach ($topics as $topic)
        {
            $res = $snsClient->createTopic(['Name' => $topic]);
            if ($res->get('@metadata')['statusCode'] == 200)
            {
                array_push($topicsToSubcribe, $res->get('TopicArn'));
            }
            else
            {
                throw new Exception("An error occured during Amazon SNS createTopic", $res->get('@metadata')['statusCode']);
            }
        }

        $SNSEndPointData = $snsClient->createPlatformEndpoint([
            'PlatformApplicationArn' => $arn,
            'Token'                  => $token
        ]);

        foreach ($topicsToSubcribe as $topic)
        {
            $snsClient->subscribe([
                'Protocol' => "application",
                'Endpoint' => $SNSEndPointData->get('EndpointArn'),
                'TopicArn' => $topic
            ]);
        }
    }
    catch (\Exception $e)
    {
       // Logs some errors
    } 

?>

所有订阅: all subscriptions

“ allUsers”订阅: enter image description here

希望这会有所帮助。