تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Broadcasting to Subscribers0:00

We can send notifications to an individual, and that's great, but we want to also send notifications en masse when something important happens. Let's look at how that can work. I think the best place to start is inside of our Push Notification service, because we have our method to send to an individual user. We should also have a method to send to all subscribers, so we'll call this method Send To All Subscribers. And what we want is the payload, and if we really wanted to be flexible, we could pass in something that would scope the notifications to a certain set of users, so that we could do something like this, to where we will start off with a query, and then if we have a scope, then we will simply use that. We'll just call that scope, passing in the query, and that would build the query to, you know, filter down to the users that we wanted to send the notification to. But when you start dealing with a bunch of records, we want

passing in the query, and that would build the query to, you know, filter down to the users that we wanted to send the notification to. But when you start dealing with a bunch of records, we want to process them in chunks, so we'll chunk by ID in chunks of 100, so that we can work with those chunks of subscriptions, and we, of course, inevitably want to send the payload. But the first thing that we should check is to be sure that we have some subscriptions, because if it's empty, then, well, there's nothing to send. And then from there, it's just a matter of, well, doing what we've done before, to where we iterate over the subscriptions, and create the push subscription, so that we can queue the notification, and all of that stuff. So really, it makes a lot of sense to extract pretty much this entire for each loop, and let's just call it queue for subscriptions, to where we will pass in the subscriptions, and the payload, and that's going to do practically

Extracting Queue Logic2:01

extract pretty much this entire for each loop, and let's just call it queue for subscriptions, to where we will pass in the subscriptions, and the payload, and that's going to do practically everything else. So that here, after we ensure that we have some subscriptions, we will call that queue for subscriptions, passing in those subscriptions, and the payload. So now we just need that method, protected function queue for subscriptions, to where we will have an iterable called subscriptions, and the payload. Then we'll just paste in that code, everything else should be fine, so that the next thing that we need is our job, because ideally, we don't want to just push these subscriptions straight from the controller. We would want to queue them up in a job, and let the job do that separately. So let's make that job, we'll call it broadcast push notification, and let's go ahead and open that new file, and let's do this for the constructor. We naturally want the payload,

Creating Broadcast Job2:56

the job do that separately. So let's make that job, we'll call it broadcast push notification, and let's go ahead and open that new file, and let's do this for the constructor. We naturally want the payload, and this is where we could start to incorporate that scope. Let's say that we wanted to send a notification just to a certain segment of our employees. Maybe they're pumpers, maybe they're foremen, shoot, maybe it's someone in the office that needs to be aware that these things have changed. We could have a certain segment of employees that would receive this notification. We could do something like this, where we would have our scope, and then we could check if the segment of our employees is equal to pumpers. Then we could set our scope equal to a function that's going to accept the query, so that we can build that query, where it has user, and let's say that we had roles set up, so that the role would be pumper, or something along those

to a function that's going to accept the query, so that we can build that query, where it has user, and let's say that we had roles set up, so that the role would be pumper, or something along those lines. We, of course, don't have that set up, so I'm just going to comment that out. But that's the general idea, so that it gives us the flexibility to send notifications to whoever we need to. But then the last thing that we need to do is send that notification to all of the subscribers, which the push notification service could be injected here, so that we could then send to all subscribers, passing in the payload, and then if we had the scope, we could pass that as well. But then it's just a matter of dispatching this job. We could do that by, you know, setting up events, but let's keep things simple, and let's go to our reading controller, because this is where we manipulate those readings. So we don't want to store. Let's say that anytime that a meter is

Dispatching from Controller4:47

events, but let's keep things simple, and let's go to our reading controller, because this is where we manipulate those readings. So we don't want to store. Let's say that anytime that a meter is updated, then we want to send out a notification. So the payload would have a title, which could be reading updated, or let's do meter reading updated, because that's a little more clear. The body could then include, you know, the actual information, and we can be as vague or as specific as we want. So here we're just going to say that the meter name had a reading that was updated to a given value, but then too we want to include the URL, just in case if someone wants to tap or click, then we will take them to the page for that meter, so that they could look at all of the information. And then, of course, once we have the payload, we'll call broadcast push notification dispatch, and then the payload. And once again, if we wanted to include a certain segment, then

Testing Notification Flow5:43

information. And then, of course, once we have the payload, we'll call broadcast push notification dispatch, and then the payload. And once again, if we wanted to include a certain segment, then we could easily do that. So now all we have to do is work our queue, php artisan queue work, and then we will test this out. Let's view one of our meters, and I'm just going to open it up in a new tab. This has two readings on it, and we're just going to edit one of them. Really doesn't matter which one, really doesn't matter which or what value we set this to. Whenever we update this, we should get a notification. I'm going to close that tab, so that there's our notification. If I click on this, it will open up a new tab that takes us back to that meter. And so now that we have this ability to send out these notifications, we need to also allow the user to unsubscribe from these notifications. And we will look at that in the next episode.

ability to send out these notifications, we need to also allow the user to unsubscribe from these notifications. And we will look at that in the next episode.

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