2

我正在尝试使用 Nexmo 和 Laravel 发送一些文本消息。当我直接在路由中发送消息时,它工作正常:

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

但是,当我尝试使用通知类发送短信时,我收到错误“凭据错误”。我一直在关注官方文档: https ://laravel.com/docs/5.5/notifications#sms-notifications

这是扩展通知的类:

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

    }

我无法弄清楚为什么我在尝试使用通知类而在路由文件中完成工作时收到“错误凭据”错误?

谢谢!

编辑:调用 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());
  }
}

我还应该补充一点toNexmo()

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

    }
4

2 回答 2

1

原来错误是由一个愚蠢的错误引起的,我搞砸了 config/services.php 中的 nexmo-config:

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

我在上面输入了实际的密钥和秘密......

于 2017-09-09T13:04:00.780 回答
0

您没有提到收到错误时如何发送通知,但是如果您使用队列,则应确保运行

php artisan queue:restart

让进程在队列中运行查看您的更改(例如修改的 .env 或配置)

于 2017-09-09T11:53:07.090 回答