protected function schedule(Schedule $schedule)
{
$notifications = Notification::where('created_at',null)->where('notification_schedule','!=',null)->where('notification_schedule','<=',Carbon::now('Asia/Kathmandu')->toDateTimeString())->get();
if(!empty($notifications))
{
foreach($notifications as $notification)
{
$year = Carbon::now('Asia/Kathmandu')->format('Y');
$parsedNotificationSchedule = Carbon::parse($notification->notification_schedule);
$notificationYear = $parsedNotificationSchedule->format('Y');
$notificationDay = $parsedNotificationSchedule->format('d');
$notificationMonth = $parsedNotificationSchedule->format('m');
$notificationHour = $parsedNotificationSchedule->format('H');
$notificationMinute = $parsedNotificationSchedule->format('i');
$cron = $notificationHour.' '.$notificationMinute.' '.$notificationDay.' '. $notificationMonth.' *';
$schedule->call(function () use($year,$notificationYear,$notification) {
if($year == $notificationYear){
$this->sendNotification($notification);
}
})->cron($cron);
}
}
}
protected function sendNotification($notification)
{
try {
//process notification sent
$data = json_decode($notification->detail,true);
$data['is_scheduled'] = true;
event(new NotificationEvent($data));
$notification->created_at = Carbon::now($this->settings->first()->time_zone);
$notification->save();
} catch (\Exception $e) {
\Log::error("exception when sending notification" . $e);
}
}
I want to send notification at a specific date and time. With laravel documentation i found out ->cron() can do this thing. But even after i configured the code in the kernel.php of console. it doens't even run.
CodePudding user response:
You need to have your scheduler running. If the scheduler is not running your scheduled jobs will not be executed.
https://laravel.com/docs/9.x/scheduling#running-the-scheduler
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
CodePudding user response:
You need to start scheduler in background to run cron in laravel.
please put php artisan schedule:run
command in background.