php - Laravel Notification - Getting API Response from Custom Notification Channel - Stack Overflow

admin2025-04-16  3

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

Share Improve this question edited Feb 4 at 10:29 UnderIdentityCrisis asked Feb 3 at 16:19 UnderIdentityCrisisUnderIdentityCrisis 866 bronze badges 6
  • 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 – UnderIdentityCrisis Commented Feb 4 at 4:24
  • 1 If the notification method (e.g mail) just happened to have a back log of emails to send, you would be waiting ages potentially. I often see on sites a message displayed to say a notification has been sent, if not received click here. Think async v sync – Rooneyl Commented Feb 4 at 10:39
  • @Rooneyl You’ve made a solid point. I haven’t seen this issue in my app yet, but I can’t completely ignore the possibility. Thank you for pointing that out. – UnderIdentityCrisis Commented Feb 4 at 16:05
  • What if I’m not using a background queue for notifications and there’s no backlog in the app? Is there a way to implement this? Since I’m using an API to send the email and the request/response is quick, there won’t be a backlog. In this case, having a response for these notifications would be a good approach, right? @Rooneyl – UnderIdentityCrisis Commented Feb 4 at 16:11
  • the original call is send the notification is web based, ergo stateless. You would need some king of webhook from the channel that you are using to confirm that the notification has been sent, and then check that and let the user know. But if the notification took ages the user would get a random message X minutes later - a bit strange for a user journey. I still would prefer the it has been sent, if not approach – Rooneyl Commented Feb 11 at 13:36
 |  Show 1 more comment

1 Answer 1

Reset to default 0

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);
转载请注明原文地址:http://www.anycun.com/QandA/1744762189a87257.html