简体   繁体   English

Laravel Nexmo SMS通知未发送

[英]Laravel Nexmo SMS Notifications not sending

I am trying to send SMS notifications using Nexmo in Laravel, I have been following the official documentation guide . 我正在尝试使用Laravel中的Nexmo发送SMS通知,我一直在遵循官方文档指南

For some reason my notifications do not send, but I don't get any errors at all. 由于某种原因,我的通知无法发送,但是我完全没有收到任何错误。

I've set up my Notification class: 我已经设置了Notification类:

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

}

In my Booking model I have added the routeNotificationForNexmo() method to customize the phone number the notification is delivered to: 在我的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;
    }

}

In my BookingController I am sending the actual SMS notification in my update method: 在我的BookingController中,我以update方法发送实际的SMS通知:

 /**
 * 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))
}

When I update a record it saves without any errors at all, but I don't receive the sms. 当我更新记录时,它可以完全保存而没有任何错误,但是我没有收到短信。 Anyone know what I'm doing wrong here? 有人知道我在做什么错吗?

Thanks 谢谢

EDIT 编辑

The notifications are being stored in the database but the data is empty: 通知正在存储在数据库中,但数据为空:

在此处输入图片说明

The issue was that in the via method in the Notification class I had to set nexmo as the channel: 问题是在Notification类的via方法中,我必须将nexmo设置为通道:

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

It would default to database, hence why I was getting records in my db. 它将默认为数据库,因此为什么我要在数据库中获取记录。 Now it sends the sms fine. 现在,它可以发送短信了。

If you want to store the notifications in the database as well as send the sms through nexmo you can just return both channels: 如果要将通知存储在数据库中以及通过nexmo发送短信,则可以返回两个通道:

return ['nexmo', 'database'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM