در حال بارگذاری ...

Introducing Nova Notifications0:01

Well, I can barely believe it, but we're almost at the end of the series. There are a couple more topics I'd like to touch on before we wrap everything up, the first of which is notifications. You've likely noticed in the UI, and maybe you've even played around with it if you have your own copy of Nova installed, this little bell icon that, when clicked, opens up a notification tray. But how can we send notifications to our administrators? Well, let me show you how. Remember how we set up our customer discount action to run in the background in batches? It would be very useful to know, as an administrator, when this task has completed, because currently we can run the action, but we don't really know when everything is finished, when all of the customers have received their discount.

Sending After Batch Completion0:42

because currently we can run the action, but we don't really know when everything is finished, when all of the customers have received their discount. Let's see if we can change that. So in our action, I've come down to the withBatch method, and specifically the then callback that we set up a couple of episodes ago, which, as we discussed, will only execute when all of the customers we have selected have received their discount email. So this is the perfect place to send the notification. Let's first build the notification. So I'm going to create a variable called notification, which is going to be equal to a Nova notification, and as with most things in Nova, there is a static make method.

Composing Notification Message1:13

So I'm going to create a variable called notification, which is going to be equal to a Nova notification, and as with most things in Nova, there is a static make method. One of the methods we can chain onto this is message, and this is where we'll define the content inside the notification. Let's hard code it for now. Something like "20% discount sent to the following customers," and then we want to include the customer resource IDs. Well, we already know that these resource IDs are included on the $batch received inside the closure, so let's glue them together using the array_join method, and I'll glue them using a comma, and then the final glue will be an "and."

so let's glue them together using the array join method, and I'll glue them using a comma, and then the final glue will be an and. So it should say something like 20% discount sent to the following customers, 69, 70, and 71. The Nova notification also allows us to select an icon to display. Again, these are Heroicons, and there's a Heroicon called currency-dollar, which I think makes sense for this, seeing as we are giving a discount. Another option we can chain on is the type of notification. So this could be success, it could be info, it could be warning or danger. In our case, I think info makes a lot of sense. We are being informed that this job has completed successfully.

Passing User to Job2:22

In our case, I think info makes a lot of sense. We are being informed that this job has completed successfully. Finally, we want to remove the hard coded 20% here and pass the actual percentage, which we receive inside the action fields. So let's make sure we have access to that by including it in the scope of this closure, and then before the message here, I'm going to grab the fields discount like so, and prepend it to our message. We've built the notification, but we actually need to send it to a notifiable, a User in this case. Because this job is queued, we won't be able to use anything like auth user.

a User in this case. Because this job is queued, we won't be able to use anything like auth()->user(). It won't know who that User is. So instead, we're going to have to pass the User into the constructor when we instantiate this action. We can add that property as a private $user, and when we resolve it inside the action's method from our CustomerResource, I'll add in the $user parameter, which will be equal to the current User that's logged in for this particular request. Now that we have access to the User,

Testing the Notification3:22

which will be equal to the current User that's logged in for this particular request. Now that we have access to the User, we can obviously make use of that down here at the bottom inside the then closure. I'll say this User::notify(NovaNotification); Let's see if it works, shall we? From our customer's index, let's go ahead and select, say, 70 and 69. We'll send them a customer discount. I'll give them 15% and run the action. So now our queue is picking up these two jobs and processing them. It'll take a few seconds to run,

So now our queue is picking up these two jobs and processing them. It'll take a few seconds to run, but if we wait for it to complete and keep our eye on the notification icon up here, we should see a notification dot appear, which it does. We can click this, and sure enough, here we have our 15% discount sent to the following customers, 70 and 69. I'm able to mark this as viewed, and you can see the red bar disappears. I can mark all as red if I have multiple notifications. Note that the icon color is blue because we marked it as the type of info, and I'm also able to delete this notification.

Note that the icon color is blue because we marked it as the type of info, and I'm also able to delete this notification if I don't want to leave it hanging around inside the admin panel. So it's almost guaranteed that your application will need to send notifications to your administrators when significant events occur, when long-running tasks have completed, when there's an action that needs to be completed, and thankfully, Nova makes it a breeze to implement notifications, send them out, and see them directly from the Nova admin panel.

send them out, and see them directly from the Nova admin panel.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟