I’ve set up a custom notification channel in Laravel. It make an API call to an external service, such as an Email and SMS provider. When a user requests a notification (like an OTP), the notification triggers this API call to send the message to their email or phone number.
Now, I’m trying to capture the response from this API call, so I can show the user whether the notification was successfully sent or if it failed.
Here is the what the code looks like
$notification = new SendVerificationCode([
"otp" => $otp,
]);
Notification::route('mail', $emailAddress)
->route('mobile_number', $phoneNumber)
->notify($notification);
The issue is that the notify()
method doesn’t return anything. So another approach i tried was to set a class property in SendVerificationCode
and via custom channel update that class property, here also I'm getting null too.I suspect the problem may be because I’m using on-demand notifications which internally uses AnonymousNotifiable
Note: I don't want to use cache or session here coz i fell that will be applying duct tap to the problem.
Update: I’ve also looked into notification events, but that’s more like creating a separate flow based on notifications. When it comes to something like OTPs, I prefer to include the status of whether the email and SMS were successful or not in the response. That seems like a more appropriate flow for this scenario. cc: @C3row
I’ve set up a custom notification channel in Laravel. It make an API call to an external service, such as an Email and SMS provider. When a user requests a notification (like an OTP), the notification triggers this API call to send the message to their email or phone number.
Now, I’m trying to capture the response from this API call, so I can show the user whether the notification was successfully sent or if it failed.
Here is the what the code looks like
$notification = new SendVerificationCode([
"otp" => $otp,
]);
Notification::route('mail', $emailAddress)
->route('mobile_number', $phoneNumber)
->notify($notification);
The issue is that the notify()
method doesn’t return anything. So another approach i tried was to set a class property in SendVerificationCode
and via custom channel update that class property, here also I'm getting null too.I suspect the problem may be because I’m using on-demand notifications which internally uses AnonymousNotifiable
Note: I don't want to use cache or session here coz i fell that will be applying duct tap to the problem.
Update: I’ve also looked into notification events, but that’s more like creating a separate flow based on notifications. When it comes to something like OTPs, I prefer to include the status of whether the email and SMS were successful or not in the response. That seems like a more appropriate flow for this scenario. cc: @C3row
first of all, Laravel's notification system especially with on-demand notifications is built as a fire-and-forget mechanism, so the notify()
method won't return any response.
To capture the response synchronously, you might need to bypass/simulate notify()
and directly call your custom channel’s send method. For example, instead of calling notify()
, instantiate your channel and pass it the notifiable and notification, like so:
$channel = new CustomNotificationChannel();
$response = $channel->send($notifiable, $notification);