Charrua
111
1
Laravel

Creating Nexmo custom notification channel

Posted 7 months ago by Charrua

Hello, I'm creating a new Nexmo notification channel because I need to add some client-ref parameter to the Nexmo API request.

The original channel that packs with Laravel 5.6 has a send method:

/**
     * Send the given notification.
     *
     * @param  mixed  $notifiable
     * @param  \Illuminate\Notifications\Notification  $notification
     * @return \Nexmo\Message\Message
     */
    public function send($notifiable, Notification $notification)
    {
        if (! $to = $notifiable->routeNotificationFor('nexmo')) {
            return;
        }
        $message = $notification->toNexmo($notifiable);
        if (is_string($message)) {
            $message = new NexmoMessage($message);
        }
        return $this->nexmo->message()->send([
            'type' => $message->type,
            'from' => $message->from ?: $this->from,
            'to' => $to,
            'text' => trim($message->content),
        ]);
    }

I need to add the parameter as:

        return $this->nexmo->message()->send([
            'type' => $message->type,
            'from' => $message->from ?: $this->from,
            'to' => $to,
            'text' => trim($message->content),
        'client-ref' => 'some text...',
        ]);

So I tried to duplicate the channel and instantiate it as Laravel documentation on creating custom channel (https://laravel.com/docs/5.6/notifications#custom-channels)

I have created NexmoCustomSmsChannel and only add my parameter, so the final channel is a duplicate from the original only with one extra parameter:

<?php

namespace App\Channels;

use Nexmo\Client as NexmoClient;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\NexmoMessage;


class NexmoCustomSmsChannel 
{
    /**
     * The Nexmo client instance.
     *
     * @var \Nexmo\Client
     */
    protected $nexmo;

    /**
     * The phone number notifications should be sent from.
     *
     * @var string
     */
    protected $from;

    /**
     * Create a new Nexmo channel instance.
     *
     * @param  \Nexmo\Client  $nexmo
     * @param  string  $from
     * @return void
     */
    public function __construct(NexmoClient $nexmo, $from)
    {
        $this->from = $from;
        $this->nexmo = $nexmo;
    }

    /**
     * Send the given notification.
     *
     * @param  mixed  $notifiable
     * @param  \Illuminate\Notifications\Notification  $notification
     * @return \Nexmo\Message\Message
     */
    public function send($notifiable, Notification $notification)
    {
        if (! $to = $notifiable->routeNotificationFor('nexmo')) {
            return;
        }

        $message = $notification->toVoice($notifiable);

        if (is_string($message)) {
            $message = new NexmoMessage($message);
        }

        return $this->nexmo->message()->send([
            'type' => $message->type,
            'from' => $message->from ?: $this->from,
            'to' => $to,
            'text' => trim($message->content),
            'client-ref' =>  'some text...',
        ]);
    }
}

And my notification is like:

<?php

namespace App\Notifications\Appointments;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

use App\Channels\NexmoCustomSmsChannel;


class AppIntroNew extends Notification implements ShouldQueue
{
    use Queueable;


    private $appointment;


    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($appointment)
    {
        $this->appointment = $appointment;
    }



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



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

The error I get is:

[2018-08-18 04:23:58] local.ERROR: Target [Nexmo\Client\Credentials\CredentialsInterface] is not instantiable while building [App\Channels\NexmoCustomSmsChannel, Nexmo\Client]. {"exception":"[object] (Illuminate\Contracts\Container\BindingResolutionException(code: 0): Target [Nexmo\Client\Credentials\CredentialsInterface] is not instantiable while building [App\Channels\NexmoCustomSmsChannel, Nexmo\Client]. at /Users/.../Documents/Web/.../app/vendor/laravel/framework/src/Illuminate/Container/Container.php:918)

Any ideas?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.