Im trying to send some text-messages using Nexmo with Laravel. When I send the message directly in the route it works fine:
Route::get('/sms/send/{to}', function(\Nexmo\Client $nexmo, $to){
$message = $nexmo->message()->send([
'to' => $to,
'from' => '@me',
'text' => 'Sending SMS from Laravel. yay!!!'
]);
Log::info('sent message: ' . $message['message-id']);
});
But when I try to sen an sms using a Notification-class I get the error "Bad credentials". I've been following the official documentation from: https://laravel.com/docs/5.5/notifications#sms-notifications
Here is the class that extends Notification:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\NexmoMessage;
class KeepGoing extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['nexmo'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toNexmo($notifiable)
{
return (new NexmoMessage)
->content('Your SMS message content');
}
I cant figure out why I get a "Bad credentials"-error when trying to use the Notification-class while its working when done in the route-file?
Thank you!
EDIT: The controller that calls notify():
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use App\Notifications\KeepGoing;
class NotificationController extends Controller
{
public function index()
{
$user = User::first();
$user->notify(new KeepGoing());
}
}
I should also add that toNexmo()
gets called:
public function toNexmo($notifiable)
{
// If i dump($notifiable) I can see the user.
// The $notifiable has the attribute phone_number which is
// what the notificationsystem looks for...
return (new NexmoMessage)
->content('Your SMS message content');
}
Turns out the error was caused by a silly mistake, I messed up the nexmo-config in config/services.php:
I put in the actual key and secret in the above...