Laravel - Sending Notification when event starts - laravel

I developed a little Event System. By creating a new event the event startdate can be set on the future.
In this case I´m trying to send out a notification to a user, where is participant of this event, that the event started. How i can solve this?
I tried something like this with scheduling and an own ServiceProvider:
$challenges = Auth::User()->challenges()->where('startdate', '<=', Carbon::now())->get();
foreach($challenges as $challenge) {
Notifynder::category('challenge.started')
->from(1)
->to(1)
->url(url("challenge/$challenge->slug"))
->send();
}
But yes, so the notification will send out everytime..not a good solution. I need them only one time when the event startdate switches from future to now.
Do you have an idea? Many thanks in advance!!
Regards

Personally I would just create a new field in database that would indicate if you sent the notifications. If you did not and the start date is after now you want to send notifications and set the flag to 1/true.
Still, the solution you created is not the best/most performant, since now you'll query database at every single request. The better would be to create some CRON job to perodically check for such thing.
For more information check this out:
https://laravel.com/docs/5.2/scheduling
This will provide you some basic informations about what you need to do to perodically make some actions.You may also want to read about Artisan Commands
so you can run it every x minutes.

Related

Scheduling and rescheduling emails

Background
A workflow I have allows users to book meetings. The time/day of the meeting is stored in a table along with other details like the email address.
I send them an email reminder 30 minutes in advance.
Problem
In order to send them an email, a recurring event is set up once a week to go through the table and schedule the email to be sent on time - 30 minutes.
I've added the ability to reschedule the meeting. The problem that creates is that the emails are already scheduled, so users get the reminders at the original time, which is confusing.
What I want to do
I want to be able to send them the email at the rescheduled time, but there are technical limitations to the platform I use, which are:
I cannot set up cron/recurring more frequently than every day. This would probably be better than every week, but if someone rescheduled within the day, they would still get the wrong email.
I cannot remove scheduled events - so any recurring events-based workflow would still send the original email.
I know - this is pretty limiting, but am I even approaching this in the right way?
given your constraints, I'd probably go with 'resign'.
But in all seriousness, if you can't remove scheduled events (and I'm guessing you can't 'move' them because this is too advanced for your CTO to get their head around) then the only way I see it is to break the email send process into two steps - send scheduled event to PROXY in-front of your email sender, check if there is another event (i.e. can you add some 'cancelled/moved to data to the original one) and if so don't send it.

Laravel Schedule One-Off Events

Users are able to set up a marketing email send time within my app for random dates as they need them. It is crucial that the emails start to go out exactly when they are scheduled. So, the app needs to create something that fires one time for a specific group of emails at a specific date and time down to the minute. There will be many more dates for other sends in the future, but they are all distinct (so need something other than 'run every xxx date')
I could have a Scheduler task that runs every minute that looks at the dates of any pending tasks in the database and moves to the command that sends those that need sending. But I'm running a multi-tenanted app -- likely not overlap, but seems like a huge hit to multiple databases every minute for every tenant to search through potentially thousands of records per tenant.
What I need (I think) is a way to programmatically set a schedule for each send date, and then remove it. I liked this answer or perhaps using ->between($startMin, $endMin), without the every xxx instruction, or even using the cron function within Scheduler to set up one single time for each group that needs to be sent. Not sure I'm using this the way it was intended though?
Would this even work? I programmatically created a schedule from within a test method, and it was added to the schedule queue based on the dump of the $schedule I created, showing all schedules - but it did not show up via this method found from this answer:
$schedule = app()->make(\Illuminate\Console\Scheduling\Schedule::class);
$events = collect($schedule->events())->filter(function (\Illuminate\Console\Scheduling\Event $event) {
return stripos($event->command, 'YourCommandHere');
});
It also did not output anything, so I'm wondering if programmatically creating a schedule outside of Kernel.php is not the way to go.
Is there another way? I don't know scheduler well enough to know if these one-off schedules permanently remain somewhere, are they deleted after their intended single use, taking up memory, etc?
Really appreciate any help or guidance.

Laravel - How to save a task with it's given class and properties and fetch it every minute again?

Goal:
I want to check if a specific job can be processed every minute.
For that, I want to use Task Scheduling.
However, I'm not sure how to solve it with my API end goal of:
SmsNotificaton::send($user, new OrderReceived($order));
How would you do this?
Explination:
I want to save some bucks by not sending an SMS directly to the user, only after 15 minutes went past and the status of the order didn't change.
Inside of new OrderReceived($order) I'm going to have a handle method. That method simply checks if the order status has changed or not. If changed, we send an SMS via my SmsGateway.
Problem:
How would you save the data of $user and new OrderReceived($order) in the database, so that you can call a cron command every minute, to fetch the old data of $user and new OrderReceived($order) again, to check if it is sendable?
It should be very similar to how the Laravel queue system works, but I tried to understand how serialization works, but I cannot grasp how the dispatcher saves the data and so on.
What you want to do is use Jobs & Queues built-in in laravel
You will need to create a new Job for example CheckStatusThenSendSMSJob() which when dispatched will check if order status changed or it should send message
Then instead of sending a message you need to Dispatch A job with a delay
CheckStatusThenSendSMSJob($order)::dispatch->delay(now()->addMinutes(15));
Check more about queues and delayed dispatch in laravel documentation: https://laravel.com/docs/5.8/queues#delayed-dispatching
Also make sure that you actually setup your queue to run in background (in a different process) by either using redis driver or database driver for queues ( By default laravel installation runs queues once they are called synchronously)

Laravel - Send Mail in Future based on condition

I want to send emails to various users based on the schedules they have set.
I read about beanstalkd, queues and Delayed Message Queueing and for now it looks like fitting in:
$when = Carbon::now()->addMinutes($minutes); // i can calculate minutes at this moment
\Mail::to($user)->later($when, new \App\Mail\TestMail);
But i'm not quite sure on few things:
User can cancel a future schedule. In that case how do i cancel an email that's suppose to send in future. Can i set condition somewhere that gets checked before sending the actual email? Tried return false on handle method of \App\Mail\TestMail and it started throwing error
Am i using the right approach. I also read about Scheduler but i don't get how i am going to cancel future emails(if they need to be)
There are many ways to approach this. Personally I would queue the emails on a schedule rather than adding them to the queue for later.
So you run a scheduled task once a day (or hour, or minute) which runs a query to select which users require an email, then using that result set, you add a job to the queue for each result.
This way, if a user unsubscribes, you don't have to worry about removing already queued jobs.
Laravel offers quite a nice interface for creating scheduled jobs (https://laravel.com/docs/5.4/scheduling) which can then be called via a cronjob.

How to get the snooze time for a scheduled reminder?

I have been having some trouble with reminders as of lately. I was wondering how the snooze feature works? When snooze is pressed on a reminder, is a new reminder created or is the same reminder scheduled for later?
The reason i ask is that i want to update a textblock in my app that shows the time of the reminder. My first thought was to look upp the reminder that has been snoozed and check the BeginTime property of the returned ScheduledAction. But this property had not been changed since the reminder was first created.
So, simply put, how do i get the scheduled time for a ScheduledAction including one or more possible snoozes ?
thanks!
You can't get this information.
All you can do is trigger the notification and then it's up to the user to respond to the notification as they feel is appropriate.
That doesn't mean you can't include additional reminder information within your app, if appropriate.

Resources