SMS Notifications Are Not Working on Laravel 5.5
muhamadhhassan opened this issue · comments
I am trying to use the release 2.0.6
with a laravel 5.5 app I get no exceptions but the SMS was not sent
service.php
'twilio' => [
'auth_token' => env('TWILIO_TOKEN'),
'account_sid' => env('TWILIO_SID'),
'from' => env('TWILIO_NUMBER')
]
Notification Class
namespace App\Notifications;
use Illuminate\Notifications\Notification;
use NotificationChannels\Twilio\TwilioChannel;
use NotificationChannels\Twilio\TwilioSmsMessage;
class ReservationMade extends Notification Implements
{
/**
* 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 [TwilioChannel::class];
}
public function toTwilio($notifiable)
{
return (new TwilioSmsMessage())
->content('bla');
}
}
In User.php
public function routeNotificationForTwilio()
{
return $this->phone;
}
I struggled with this too. The issue is that this "Twilio notifications channel" under "vendor/laravel-notification-channels/twilio" uses the Twillio SDK under "vendor/twilio/sdk/Twilio/" for API call transport. The SDK is maintained by a separate team and unfortunately it does not throw Laravel exceptions.
Here is what I had to do to work around the issue:
Quick workaround: Edit the Rest Client Class to report to your storage/logs/laravel.log on the request / response to Twilio API endpoint.
- Edit vendor/twilio/sdk/Twilio/Rest/Client.php, and update the request() method and change
return $this->getHttpClient()->request(
$method,
$uri,
$params,
$data,
$headers,
$username,
$password,
$timeout
);
TO
info(print_r("method=$method", 1));
info(print_r("uri=$uri", 1));
info(print_r($data, 1));
info(print_r($headers, 1));
info(print_r("username=$username", 1));
info(print_r("password=$password", 1));
$result = $this->getHttpClient()->request(
$method,
$uri,
$params,
$data,
$headers,
$username,
$password,
$timeout
);
info(print_r($result, 1));
return $result;
Longer-Term workaround: Use composer to override the default Rest\Client Class with your customized class that does some conditional reporting based upon a configuration in your .env file.
(1) Step 1. Create this folder and customized class in your Laravel App
app\MyVendor\twilio\Rest\Client.php
(2) Step 2. Update Composer.json to exclude this Class namespace
"autoload": {
"exclude-from-classmap": [
"vendor/twilio/sdk/Twilio/Rest/Client.php"
]
},
(3) Step 3. Tell composer where to find your custom class using psr-4
"autoload": {
"psr-4": {
"App\": "app/",
"Twilio\": "app/MyVendor/twilio/"
},
},
(4) Step 4: Refresh composer
composer dump-autoload
(5) Step 5. Execute your code and observe logs for the call response from Twillio
(6) Step 6. Grab a coffee
@lancemitchell Thank you for your reply, the solution you suggested worked and I can get the request options now.
I found that the request is made to this endpoint https://api.twilio.com/2010-04-01/Accounts/{account_sid}/Messages.json
which retrieves all the messages sent by my account!
@lancemitchell which kind of exceptions are failing to capture?
You mention
it does not throw Laravel exceptions
but what would they be? Can we capture the Twilio Exception and re-throw them with Laravel Notification compatible ones?
On laravel 5.6 as well. Does not work out sending the sms.
Should be fixed.
Doesn't work in laravel 9
I can confirm that is not working for me neither in Laravel 9.38.0 (with laravel-notification-channels/twilio 3.2.0 and twilio/sdk 6.43.1) . No exceptions, but the SMS is not sent. Nothing on my Twilio logs neither (as if it never reached Twilio).