繁体   English   中英

使用 Laravel 的 AWS SNS 推送通知

[英]AWS SNS Push Notification using Laravel

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

我找到了我的解决方案

首先需要添加这个作曲家package

"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',
    ]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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