简体   繁体   中英

How to send push notification from web to iOS and android device using AWS SNS in Laravel 5.1?

I have coded like,

$sns = App::make('aws')->createClient('sns');

    $data = json_encode([
        'APNS' => ['apns' => [
                'alert' => $message
            ]
        ]
    ]);

    Log::info($data);

    $sns->publish(array(
        'Message' => $data,
        'TargetArn' => "arn:aws:sns:us-west-2:360542326270:endpoint/APNS_SANDBOX/Testtest/20a75cd1-da25-3331-8126-4db497cbdd5e"
    ));

I'am getting the error as below:

[Aws\Sns\Exception\SnsException]                                                                                                                                                        

Error executing "Publish" on " https://sns.us-east-1.amazonaws.com "; AWS HTTP error: Client error: POST https://sns.us-east-1.amazonaws.com resulted in a 400 Bad Request response:

  <Type>Sender</Type>                                                                                                                                                                 
  <Code>InvalidPara (truncated...)                                                                                                                                                    

InvalidParameter (client): Invalid parameter: TargetArn Reason: No endpoint found for the target arn specified -

  <Type>Sender</Type>                                                                                                                                                                 
  <Code>InvalidParameter</Code>                                                                                                                                                       
  <Message>Invalid parameter: TargetArn Reason: No endpoint found for the target arn specified</Message>                                                                              

<RequestId>18937e90-5453-5e30-bf89-315a59c3f2af</RequestId>                                                                                                                           

[GuzzleHttp\\Exception\\ClientException]
Client error: POST https://sns.us-east-1.amazonaws.com resulted in a 400 Bad Request response:

  <Type>Sender</Type>                                                                             
  <Code>InvalidPara (truncated...) 

Can anyone help me to find the solution? Thanks in advance..

Check out this forum post . Basically, the issue stems from the fact that your Topic/Target arn is for us-west-2 . But by default your SNS client is pointing to us-east-1 . You need to call setRegion on snsClient.

This is how the post says to do it:

snsClient.setRegion(Region.getRegion(Regions.US_WEST_2));

You'll probably have to translate into the PHP laravel specific client you are using.

I have updated the config file with my AWS account Key and secret in the config/aws.php file by following the link https://github.com/aws/aws-sdk-php-laravel . By this, it gets worked. Previously, I have updated in my .env file. Now, it works well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM