How to send a success notification after a custom action ?

Hello, i'm trying to send a notification after a custom action but no notification get fired. My code : Action::make('reset_user_password') ->action( fn (User $record) => $this->resetPassword($record) ) ->requiresConfirmation() ->successNotification( Notification::make() ->success() ->title('User password reset!'), );
Solution:
But sending the notification directly in the action works!
Jump to solution
11 Replies
awcodes
awcodes14mo ago
You have to ->send() the Notification. Add that as the last method to your Notification.
charleswilfriedk
charleswilfriedkOP14mo ago
Even inside a successNotification()? I use the same syntax with the prebuild actions and the successNotification works as intended! It's. only with this custom one that i get noting.
awcodes
awcodes14mo ago
Yes. The prebuilt actions are calling it on their own. If you new up the class then you have to send it.
charleswilfriedk
charleswilfriedkOP14mo ago
Hum.. with the send(), the notification is fired every time the page is updated
awcodes
awcodes14mo ago
please share your code with the send()
charleswilfriedk
charleswilfriedkOP14mo ago
Action::make('reset_user_password')
->action(
fn (User $record) => $this->resetPassword($record)
)
->requiresConfirmation()
->successNotification(
Notification::make()
->success()
->title('User password reset!')
->send(),
);
Action::make('reset_user_password')
->action(
fn (User $record) => $this->resetPassword($record)
)
->requiresConfirmation()
->successNotification(
Notification::make()
->success()
->title('User password reset!')
->send(),
);
awcodes
awcodes14mo ago
Ok. Since you are using a custom action.
Action::make('reset_user_password')
->action(function (User $record) {
$this->resetPassword($record);
$this->sendSuccessNotification();
})
->requiresConfirmation()
->successNotification(function (Notification $notification): Notification {
return Notification::make()
->success()
->title('User password reset!');
});
Action::make('reset_user_password')
->action(function (User $record) {
$this->resetPassword($record);
$this->sendSuccessNotification();
})
->requiresConfirmation()
->successNotification(function (Notification $notification): Notification {
return Notification::make()
->success()
->title('User password reset!');
});
But since you are in a custom action you can also just send the notification directly in the ->action() since there's no need to register it like you would on a prebuilt action:
Action::make('reset_user_password')
->action(function (User $record) {
$this->resetPassword($record);

Notification::make()
->success()
->title('User password reset!')
->send();
})
->requiresConfirmation();
Action::make('reset_user_password')
->action(function (User $record) {
$this->resetPassword($record);

Notification::make()
->success()
->title('User password reset!')
->send();
})
->requiresConfirmation();
Either way. '->successNotification()` only registers the notification that will be called when the action is run. So you still have to 'call' it in your '->action()'
charleswilfriedk
charleswilfriedkOP14mo ago
Action::make('reset_user_password')
->action(function (User $record) {
$this->resetPassword($record);
$this->sendSuccessNotification();
})
->requiresConfirmation()
->successNotification(function (Notification $notification): Notification {
return Notification::make()
->success()
->title('User password reset!');
});
Action::make('reset_user_password')
->action(function (User $record) {
$this->resetPassword($record);
$this->sendSuccessNotification();
})
->requiresConfirmation()
->successNotification(function (Notification $notification): Notification {
return Notification::make()
->success()
->title('User password reset!');
});
With this one the sendSuccessNotification is not found.
Solution
charleswilfriedk
charleswilfriedk14mo ago
But sending the notification directly in the action works!
awcodes
awcodes14mo ago
Makes sense. Depends on what class the underlying action. You should check out the prebuilt actions code sometime. It’ll give a better understanding of how they work. Once you have that you can even make your own reusable pre built actions. If you need them. Glad you have it working though. Cheers.
charleswilfriedk
charleswilfriedkOP14mo ago
Thx for the help I will look into it
Want results from more Discord servers?
Add your server