Created
August 19, 2018 11:20
-
-
Save crnkovic/7fbab9715d496151772506c66515a00e to your computer and use it in GitHub Desktop.
Custom credentials feature for Nexmo in Laravel notifications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// app/Providers/AppServiceProvider.php | |
namespace App\Providers; | |
use Illuminate\Notifications\ChannelManager; | |
use Illuminate\Support\ServiceProvider; | |
use App\Notifications\ChannelManager as MyChannelManager; | |
class AppServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Bootstrap any application services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
// ... rest of the code | |
$this->app->singleton(ChannelManager::class, function ($app) { | |
return new MyChannelManager($app); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// app/Notifications/ChannelManager.php | |
namespace App\Notifications; | |
use Illuminate\Notifications\ChannelManager as BaseChannelManager; | |
class ChannelManager extends BaseChannelManager | |
{ | |
/** | |
* Create an instance of the Nexmo driver. | |
* | |
* @return \App\Notifications\NexmoChannel | |
*/ | |
protected function createNexmoDriver() | |
{ | |
return new NexmoChannel( | |
$this->app['config']['services.nexmo.sms_from'] | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// app/Notifications/NexmoChannel.php | |
namespace App\Notifications; | |
use Nexmo\Client as NexmoClient; | |
use Illuminate\Notifications\Notification; | |
use Illuminate\Notifications\Messages\NexmoMessage; | |
use Nexmo\Client\Credentials\Basic as NexmoCredentials; | |
class NexmoChannel | |
{ | |
/** | |
* The phone number notifications should be sent from. | |
* | |
* @var string | |
*/ | |
protected $from; | |
/** | |
* Create a new Nexmo channel instance. | |
* | |
* @param string $from | |
* @return void | |
*/ | |
public function __construct($from) | |
{ | |
$this->from = $from; | |
} | |
/** | |
* Send the given notification. | |
* | |
* @param mixed $notifiable | |
* @param \Illuminate\Notifications\Notification $notification | |
* @return \Nexmo\Message\Message | |
* @throws \Nexmo\Client\Exception\Exception | |
* @throws \Nexmo\Client\Exception\Request | |
* @throws \Nexmo\Client\Exception\Server | |
*/ | |
public function send($notifiable, Notification $notification) | |
{ | |
if (!$to = $notifiable->routeNotificationFor('nexmo', $notification)) { | |
return; | |
} | |
$message = $notification->toNexmo($notifiable); | |
if (is_string($message)) { | |
$message = new NexmoMessage($message); | |
} | |
$nexmo = new NexmoClient( | |
$this->getCredentials($notifiable) | |
); | |
return $nexmo->message()->send([ | |
'type' => $message->type, | |
'from' => $message->from ?: $this->from, | |
'to' => $to, | |
'text' => trim($message->content), | |
]); | |
} | |
/** | |
* Get the Nexmo credentials for the notifiable user. | |
* | |
* @param $notifiable | |
* @return \Nexmo\Client\Credentials\Basic | |
*/ | |
protected function getCredentials($notifiable) | |
{ | |
// Grab this from anywhere | |
// In this case, $notifiable is User object | |
return new NexmoCredentials( | |
$notifiable->nexmo_key, | |
$notifiable->nexmo_secret | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// app/Notifications/TestNotification.php | |
namespace App\Notifications; | |
use Illuminate\Bus\Queueable; | |
use Illuminate\Notifications\Messages\NexmoMessage; | |
use Illuminate\Notifications\Notification; | |
class Test extends Notification | |
{ | |
use Queueable; | |
/** | |
* Get the notification's delivery channels. | |
* | |
* @param mixed $notifiable | |
* @return array | |
*/ | |
public function via($notifiable) | |
{ | |
return ['nexmo']; | |
} | |
public function toNexmo() | |
{ | |
return new NexmoMessage('This works.'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment