Giter Site home page Giter Site logo

aws-sns-sms-channel's Introduction

AWS SNS SMS Notifications Channel for Laravel

Total Downloads Build Status Latest Stable Version Software License

Installation

composer require itsnubix/aws-sns-sms-channel

In your config/services.php file enter:

'sns' => [
    'key' => env('AWS_ACCESS_KEY_ID'),
    'secret' => env('AWS_SECRET_ACCESS_KEY'),
    'region' => env('SNS_DEFAULT_REGION'),
],

Notice here that the region is not necessarily your standard AWS_DEFAULT_REGION as only certain regions allow SMS messages to be sent from them. Click here for a list of nodes that allow SMS.

Be sure that the user who owns your access key and secret has at least the following policy on AWS IAM:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "AllowSendingSMSMessages",
      "Effect": "Allow",
      "Action": [
        "sns:Publish",
        "sns:SetSMSAttributes",
        "sns:CheckIfPhoneNumberIsOptedOut"
      ],
      "Resource": ["*"]
    }
  ]
}

Now in your notifications you can do the following:

<?php
namespace App\Notifications;

use App\Models\Order;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Nubix\Notifications\Messages\SmsMessage;

class SendHelloText extends Notification
{
    use Queueable;

    /**
     * Get the notification's delivery channels.
     *
     * @param mixed $notifiable
     *
     * @return array
     */
    public function via($notifiable)
    {
        return ['sms'];
    }
    /**
     * Get the SMS representation of the notification.
     *
     * @param mixed $notifiable
     *
     * @return \App\Channels\Messages\SmsMessage
     */
    public function toSms($notifiable)
    {
        return (new SmsMessage())
            ->content('Hello world');
    }
}

Finally you'll need to extend your notifiable class with the following function so it knows how to route SMS notifications.

<?php
namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Notifications\Notification;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * Return the SMS notification routing information.
     *
     * @param \Illuminate\Notifications\Notification|null $notification
     *
     * @return mixed
     */
    public function routeNotificationForSms(?Notification $notification = null)
    {
        return $this->phone_number;
    }
}

Need more help? Read the article here

aws-sns-sms-channel's People

Contributors

nozkok avatar kylemilloy avatar mankowitz avatar

Stargazers

OBI PASCAL B 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.