我正在尝试使用 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');
}