Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Tags more
Archives
Today
Total
관리 메뉴

Memento

Laravel 에서 AWS SNS 메세지 전송 본문

Work/PHP

Laravel 에서 AWS SNS 메세지 전송

벚꽃손님 2020. 7. 29. 17:05

1. AWS SDK for PHP 설치 https://github.com/aws/aws-sdk-php

 

aws/aws-sdk-php

Official repository of the AWS SDK for PHP (@awsforphp) - aws/aws-sdk-php

github.com

composer require aws/aws-sdk-php

 

2. 메세지 전송 코드 작성

use Aws\Credentials\Credentials;
use Aws\Sns\SnsClient;

public function sendMessage(Request $request)
{
    $client = new SnsClient([
        'region' => env('AWS_REGION'),
        'credentials' => new Credentials(
            env('AWS_ACCESS_KEY_ID'),
            env('AWS_SECRET_ACCESS_KEY')
        ),
        'version' => "latest"
    ]);

    $notificationTitle = "Test Notification";
    $notificationMessage = "Hi!! Its a test notification";
    $fcmPayload = json_encode(
        [
            "notification" =>
            [
                "title" => $notificationTitle,
                "body" => $notificationMessage,
                "sound" => 'default'
            ]
        ]
    );

    $message = json_encode(["default" => $notificationMessage, "GCM" => $fcmPayload]);

    try {
        $client->publish([
            'TargetArn' => '[ARN]',
            'Message' => $message,
            'MessageStructure' => 'json'
        ]);
    } catch (\AwsException $e) {
        error_log($e->getMessage());
    }
}

'Work > PHP' 카테고리의 다른 글

PHP Cassandra Driver 설치  (0) 2019.11.27