DEV Community

Cover image for Laravel Notifications - create your custom notification channel
Senik Hakobyan
Senik Hakobyan

Posted on • Edited on

7 1

Laravel Notifications - create your custom notification channel

Hello community!

In this post I want to share how I created a custom Notification channel to handle SMS notifications via Twilio Programmable SMS product.

This article is not a tutorial about integrating Twilio with Laravel, so I assume you've already installed Twilio PHP Helper Library and setup all necessary configurations (SID, Token, From Number etc.).

The SMS definition

First of all, I want to describe the definition of SMS in my code. For that, we need just two things - recipient and content.

namespace App\Notifications\Messages;

class SmsMessage
{
    /**
     * @var string
     */
    private string $_recipient;

    /**
     * @var string
     */
    private string $_content;

    /**
     * @return string
     */
    public function getRecipient(): string
    {
        return $this->_recipient;
    }

    /**
     * @param string $recipient
     * @return $this
     */
    public function setRecipient(string $recipient): SmsMessage
    {
        $this->_recipient = $recipient;
        return $this;
    }

    /**
     * @return string
     */
    public function getContent(): string
    {
        return $this->_content;
    }

    /**
     * @param string $content
     * @return $this
     */
    public function setContent(string $content): SmsMessage
    {
        $this->_content = $content;
        return $this;
    }
}
Enter fullscreen mode Exit fullscreen mode

This is a simple value object, that has a general purpose - type safety.

The Channel

Now I'm creating a notification channel, that has a single responsibility - send the SMS message.

namespace App\Channels;

use App\Services;
use App\Notifications\Messages\SmsMessage;
use Illuminate\Notifications\Notification;

class TwilioSms
{
    /**
     * @param $notifiable
     * @param Notification $notification
     * @throws \Twilio\Exceptions\TwilioException
     */
    public function send($notifiable, Notification $notification)
    {
        $message = $notification->toTwilioSms($notifiable);

        (new Services\TwilioSms())->sendSms(
            $message->getRecipient(),
            $message->getContent()
        );
    }
}
Enter fullscreen mode Exit fullscreen mode

You've probably noticed the Services\TwilioSms class. It's just a wrapper that I've created to interact with Twilio SDK.
You can find it here - https://github.com/c0d3b0t/twilio-sms-client

The Notification

Finally I've defined the via and toTwilioSms methods in my Notification class.

/**
 * Get the notification's delivery channels.
 *
 * @param  mixed  $notifiable
 * @return array
 */
public function via($notifiable)
{
    return [\App\Channels\TwilioSms::class];
}

/**
 * @param $notifiable
 * @return SmsMessage
 */
public function toTwilioSms($notifiable): SmsMessage
{
    return (new SmsMessage())
        ->setContent("Whatever message you need.")
        ->setRecipient($notifiable->phone);
}
Enter fullscreen mode Exit fullscreen mode

That's all!

Summary

This code is running on PHP 7.4 version (due to typed properties).

Thanks for reading! Happy coding :)

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay