Giter VIP home page Giter VIP logo

aws-sns's Introduction

Amazon Simple Notification Service (AWS SNS) notification channel for Laravel

Latest Version on Packagist Software License Build Status StyleCI Quality Score Code Coverage Total Downloads

This package makes it easy to send notifications using AWS SNS with Laravel framework. Since Laravel already ships with SES email support, this package focuses on sending only SMS notifications for now. More advanced features like support for topics could be added in the future.

Contents

Installation

You can install the package via composer:

composer require laravel-notification-channels/aws-sns

Setting up the AWS SNS service

Add your AWS key ID, secret and default region to your config/services.php:

<?php

return [

    // ...

    'sns' => [
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
        'version' => 'latest',
    ],

];

Usage

Now you can use the channel in your via() method inside the notification:

<?php

use NotificationChannels\AwsSns\SnsChannel;
use NotificationChannels\AwsSns\SnsMessage;
use Illuminate\Notifications\Notification;

class AccountApproved extends Notification
{
    public function via($notifiable)
    {
        return [SnsChannel::class];
    }

    public function toSns($notifiable)
    {
        // You can just return a plain string:
        return "Your {$notifiable->service} account was approved!";
        
        // OR explicitly return a SnsMessage object passing the message body:
        return new SnsMessage("Your {$notifiable->service} account was approved!");
        
        // OR return a SnsMessage passing the arguments via `create()` or `__construct()`:
        return SnsMessage::create([
            'body' => "Your {$notifiable->service} account was approved!",
            'transactional' => true,
            'sender' => 'MyBusiness',
        ]);

        // OR create the object with or without arguments and then use the fluent API:
        return SnsMessage::create()
            ->body("Your {$notifiable->service} account was approved!")
            ->promotional()
            ->sender('MyBusiness');
    }
}

In order to let your Notification know which phone are you sending to, the channel will look for the phone, phone_number or full_phone attribute of the Notifiable model. If you want to override this behaviour, add the routeNotificationForSns method to your Notifiable model.

<?php

use Illuminate\Notifications\Notifiable;

class SomeModel {
    use Notifiable;

    public function routeNotificationForSns($notification)
    {
        return '+1234567890';
    }
}

Available SnsMessage methods

  • create([]): Accepts an array of key-values where the keys corresponds to the methods below and the values are passed as parameters;
  • body(''): Accepts a string value for the notification body. Messages with more than 140 characters will be split into multiple messages by SNS without breaking any words;
  • promotional(bool): Sets the delivery type as promotional (default). Optimizes the delivery for lower costs;
  • transactional(bool): Sets the delivery type as transactional. Optimizes the delivery to achieve the highest reliability (it also costs more);
  • sender(string): Up to 11 characters with no spaces, that is displayed as the sender on the receiving device. Support varies by country.

More information about the SMS Attributes can be found on the AWS SNS Docs. It's important to know that the attributes set on the message will override the default ones configured in your AWS account.

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

aws-sns's People

Contributors

atymic avatar brunogaspar avatar claudsonm avatar danielebuso avatar minhtran-vcc avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.