fylzero's avatar
Level 67

Trap SMS (Nexmo) notifications when running local dev environment

I'm wondering if anyone has a good pattern or way to do something similar to what Mailtrap or Helo do for emails but for SMS notifications?

I use Nexmo with Laravel notifications and don't want text messages getting send out and ideally I'd like to log them in a way that I can see they are being sent and the content of the messages when working locally.

I'm sure I can figure out a way to do this but wondered if anyone has anything solid they've done to handle this.

Thanks in advance!

0 likes
3 replies
piljac1's avatar
piljac1
Best Answer
Level 28

Never had to do it, but my approach would probably be the following :

  1. Create a custom notification channel which logs the content of the notification and the notifiable
  2. Implement a conditional logic based on your current environment in your Laravel notification class :
/**
 * Get the notification channels.
 *
 * @param  mixed  $notifiable
 * @return array|string
 */
public function via($notifiable)
{
    // LogChannel would be your custom notification channel
    $smsChannel = app()->environment('local') ? LogChannel::class : 'nexmo';

    return [$smsChannel];
}
Snapey's avatar

I think I did similar but swapped the target phone number for my own

fylzero's avatar
Level 67

@piljac1 Thanks!

This is where I landed... just fired them to Slack if local.

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return app()->isLocal() ? 'slack' : 'nexmo';
    }

    /**
     * Get the Nexmo / SMS representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return NexmoMessage
     */
    public function toNexmo($notifiable)
    {
        return (new NexmoMessage)->content($this->message);
    }

    /**
     * Get the Slack representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return SlackMessage
     */
    public function toSlack($notifiable)
    {
        return (new SlackMessage)->to('#sms')->content($this->message);
    }

Please or to participate in this conversation.