Laravel Nexmo, bad credentials when using notifications

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(NexmoClient $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 AppNotifications;

use IlluminateBusQueueable;
use IlluminateNotificationsNotification;
use IlluminateContractsQueueShouldQueue;
use IlluminateNotificationsMessagesMailMessage;
use IlluminateNotificationsMessagesNexmoMessage;

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 IlluminateNotificationsMessagesMailMessage
     */
    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 AppHttpControllers;

use IlluminateHttpRequest;
use AppUser;
use AppNotificationsKeepGoing;

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

    }

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)


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...

链接地址: http://www.djcxy.com/p/33100.html

上一篇: Nexmo API短信发送收据

下一篇: Laravel Nexmo,使用通知时证书不好