【问题标题】:AWS SNS Push Notification using Laravel使用 Laravel 的 AWS SNS 推送通知
【发布时间】:2021-11-18 11:12:04
【问题描述】:

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

【问题讨论】:

    标签: laravel push-notification amazon-sns


    【解决方案1】:

    我找到了解决办法

    首先需要添加这个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',
        ]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-12
      • 1970-01-01
      • 2023-02-16
      • 2015-05-25
      • 2019-08-29
      • 1970-01-01
      相关资源
      最近更新 更多