使用 Laravel 的 AWS SNS 推送通知

AWS SNS Push Notification using Laravel

我搜索了 AWS 文档并浪费了几个小时,但找不到使用 Laravel 8 发送推送通知的代码。有人可以帮助使用 Laravel 发送 AWS SNS 推送通知吗?

我找到了我的解决方案

首先需要添加这个composer包

"aws/aws-sdk-php"

生成端点ARN的代码

$platformApplicationArn = config('services.sns.android_arn');

$client = new SnsClient([
        'version'     => '2010-03-31',
        'region'      => config('services.sns.region'),
        'credentials' => new Credentials(
            config('services.sns.key'),
            config('services.sns.secret')
        ),
    ]);

    $result = $client->createPlatformEndpoint(array(
        'PlatformApplicationArn' => $platformApplicationArn,
        'Token'                  => $deviceToken,
    ));

    $endPointArn = isset($result['EndpointArn']) ? $result['EndpointArn'] : '';

发送推送通知的代码

    $client = new SnsClient([
            'version'     => '2010-03-31',
            'region'      => config('services.sns.region'),
            'credentials' => new Credentials(
                config('services.sns.key'),
                config('services.sns.secret')
            ),
        ]);

    $fcmPayload = json_encode(
            [
                'notification' => 
                    [
                        'title' => 'Test Notification',
                        'body'  => 'Hi from RB',
                        'sound' => 'default',
                    ],
            ]
        );

    $message = json_encode(['GCM' => $fcmPayload]);

    $client->publish([
          'TargetArn'        => $userDeviceToken->endpoint_arn,
          'Message'          => $message,
          'MessageStructure' => 'json',
    ]);