Reputation: 6435
I am trying to send SMS notifications using Nexmo in Laravel, I have been following the official documentation guide.
For some reason my notifications do not send, but I don't get any errors at all.
I've set up my Notification class:
<?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:
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:
/**
* 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:
Upvotes: 0
Views: 1606
Reputation: 6435
The issue was that in the via
method in the Notification class I had to set nexmo
as the channel:
/**
* 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:
return ['nexmo', 'database'];
Upvotes: 2