PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, February 14, 2022

[FIXED] AWS SNS Push Notification using Laravel

 February 14, 2022     amazon-sns, laravel, push-notification     No comments   

Issue

I have searched AWS documentation and wasted a couple of hours but could not find the code to send push notifications using Laravel 8. Can anybody help in sending AWS SNS push notifications using Laravel?


Solution

I found my solution

first need to add this composer package

"aws/aws-sdk-php"

The code for generate endpoint 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'] : '';

The code for send push notification

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


Answered By - Bhargav Rangani
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing