【问题标题】:Laravel Nexmo SMS Notifications not sendingLaravel Nexmo 短信通知未发送
【发布时间】:2017-06-21 19:36:47
【问题描述】:

我正在尝试在 Laravel 中使用 Nexmo 发送短信通知,我一直在关注官方 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))
}

当我更新一条记录时,它完全没有任何错误地保存,但我没有收到短信。有人知道我在这里做错了什么吗?

谢谢

编辑

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

【问题讨论】:

  • 您是否要对通知进行排队?
  • 不,我只是想即时发送。

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


【解决方案1】:

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

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

它将默认为数据库,因此我在我的数据库中获取记录。现在它可以正常发送短信了。

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

return ['nexmo', 'database'];

【讨论】:

    猜你喜欢
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多