Laravel notification: Check if the notification is sent - laravel

Am Working on laravel 9. I send notification using the notifiable trait and I wish to make some action only if the notification is.
How can I perform this!
thanks,

I think You can check using Notification::failure()
if (Notification::failure() > 0){
//your required function;
}

Related

Laravel best practice sending e-mail through API using templates

As you know many e-mail providers have the feature to use e-mail templates. Through their API you can send e-mails, just by setting the template id and passing through the variables. The predefined template will be send to the user.
For my project I want to use Mailjet API for transactional e-mails. What is the best way to call their email API in my project. Because laravel already has lots of email and notification features so I am looking for the best practice to integrate this in my project.
For example I want so make use of notifications. This notification has to call the mailjet email api and pass through the template id and the needed variables. What is the best way to archive this. With a custom notification channel maybe?
Or are there other good alternatives?
You are right, you can use an alternative mailing service while using Laravel's neat notifications feature. You will need to create a custom notification channel where you can define sending logic by hitting Mailjet's API endpoints. When the notification channel is all set up, you can specify it inside a notification itself where you can define the exact request body.
To make things easier, you may use Mailjet's official PHP wrapper so you don't have to work with raw HTTP requests.
Personally I used the same technique for Twilio SMS and Nexmo (Vonage) SMS Verification codes. I will add some of my code so you can see how I organised things:
class PhoneChannel
{
public function send($notifiable, Notification $notification)
{
if ($notification->resend) {
$this->repeatCall();
} else {
$this->initCall();
}
}
...
class VerifyPhone extends Notification
{
use Queueable;
public bool $resend;
public function __construct(bool $resend)
{
$this->resend = $resend;
}
public function via($notifiable): array
{
return [PhoneChannel::class];
}
}
The point is that you can define a request body inside the notification itself in order to access it later in the notification channel. Just add a method called toMailjet inside a notification, specify your variables, and then access it inside a channel via $notification->toMailjet()

Stripe webhook for laravel

Please ask if you don't understand anything....I am creating stripe subscription payment. i need to handle stripe customer.subscription.deleted event. i have created a webhook for it in my laravel app. but the thing is whenever i send test data from stripe it shows webhook handled. but i am sending email whenever payment is unsuccessful and also logging this it is not showing in log or sending email. I am hosting my website for now using ngrok.. which is build in in laragon.
My controller method
use Laravel\Cashier\Http\Controllers\WebhookController;
class SubscriptionController extends WebhookController
{
public function handleCustomerSubscriptionDeleted($payload){
\Log::info("Webhook is working");
dd($payload);
}
}
my route file is like this
Route::get('/stripe/webhook','SubscriptionController#handleWebhook');
it is not showing in log file this neither it is sending email.
i am following this article please check if it is right way to do this??
Click here
I have added dd() here how does this webhook will be successful?

How to extend OctoberCMS plugin to send Slack notifications?

I want to extend Magic Forms plugin to send Slack notifications at the same time with mail notification but I don't know if it is possible.
For my own plugin I'm using Slack for PHP library.
Yes I Guess you can, Just use composer for plugin add your package to plugin using composer.
then read this document https://skydiver.github.io/october-plugin-forms/docs/advanced/events/#docsNav
you need to use add Event Listeners and fire up your notification
public function boot() {
# Listen for Record after saved
Event::listen('martin.forms.afterSaveRecord', function (&$formdata, $component) {
// here use your slack lib and send notification
// $formData has all data
});
}
if any doubts please comment.

is it possible to notify all users using Laravel notification with pusher?

Is it possible to create/send notification(s) to all users when there is new announcement.
I want all users to be notified using the database notification.
Since most of the tutorials I have seen are all email notification, is this possible? Please attach links or any idea on how I could implement this.
Literally in the documentation:
Alternatively, you may send notifications via the Notification facade.
This is useful primarily when you need to send a notification to
multiple notifiable entities such as a collection of users. To send
notifications using the facade, pass all of the notifiable entities
and the notification instance to the send method:
Notification::send($users, new InvoicePaid($invoice));
So yes, you can send to all user by getting them from the database and sending a notification using the facade.
For example:
Notification::send(User::all(), new InvoicePaid($invoice));

How to send laravel notification to a custom email

I have a laravel application,
I send notifications to a user when he registers. I use the notify() on the model. But I have an issue, How do i send a notification to a custom email address? the same notification.
Here's what i have:
`$admin = Admin::find(1);
$admin->email = 'admin#site.com';
$admin->notify((new NewUserRegistered($user))->delay($when));
$user->notify((new UserRegistered($user))->delay($when));`
i get a user model instance, and customize the email to input the custom email... However, the email sends to the original mail on the model and ignores the edit.
How do i do this please?
Here the code snippet you can use for sending the notification directly to an email address without creating the User or without having the User in the database.
Notification::route('mail', 'me#abdulrehman.pk')->notify(new InvoicePaid($invoice));
As a work around, you can save the model before sending the message
$admin->email = 'admin#site.com';
$admin->save();
after that you can retrieve the email as it was

Resources