Laravel Nexmo短信通知未发送

时间:2017-06-21 19:36:47

标签: php laravel laravel-5 sms laravel-notification

我正在尝试使用Nexmo在Laravel中发送短信通知,我一直关注官方documentation guide

由于某种原因,我的通知不会发送,但我根本没有收到任何错误。

我已经设置了我的通知类:

<?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\Message\NexmoMessage;

class bookingAdded 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 ['mail'];
    }


    /**
     * Get the Nexmo / SMS representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return NexmoMessage
     */
    public function toNexmo($notifiable)
    {
        return (new NexmoMessage)
                    ->content('Your SMS message content');
    }

}

在我的Booking模型中,我添加了routeNotificationForNexmo()方法来自定义通知发送到的电话号码:

namespace App;

    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Notifications\Notifiable;

    class Booking extends Model
    {           
        use Notifiable;

    /**
     * Route notifications for the Nexmo channel.
     *
     * @return string
     */
    public function routeNotificationForNexmo()
    {
        $intl_number = preg_replace('/^07/','447', $this->customer_phone);
        return $intl_number;
    }

}

BookingController我的update方法中发送实际的短信通知:

 /**
 * Update the specified resource in storage.
 *
 * @param  int  $id
 * @return Response
 */
public function update(BookingRequest $request, $id)
{       
        $booking = Booking::find($id);
        $booking->notify(new bookingAdded($booking))
}

当我更新记录时,它会保存而没有任何错误,但我没有收到短信。谁知道我在这里做错了什么?

由于

修改

通知存储在数据库中,但数据为空:

enter image description here

1 个答案:

答案 0 :(得分:1)

问题是,在通知类的via方法中,我必须将nexmo设置为频道:

     /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['nexmo'];
    }

它会默认为数据库,因此我在数据库中获取记录的原因。现在它发送短信。

如果您想将通知存储在数据库中以及通过nexmo发送短信,您只需返回两个频道:

return ['nexmo', 'database'];
相关问题