Laravel Nexmo, bad credentials when using notifications

1k Views Asked by At

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');

    }
2

There are 2 best solutions below

2
On

Turns out the error was caused by a silly mistake, I messed up the nexmo-config in config/services.php:

  'nexmo' => [
      'key' => env('NEXMO_KEY'),
      'secret' => env('NEXMO_SECRET'),
      'sms_from' => '15556666666',
  ],

I put in the actual key and secret in the above...

1
On

You haven't mentioned how you send notifications when you are getting the error but if you use queues, you should make sure you run

php artisan queue:restart

to make process run in queue see your changes (for example modified .env or config)