How to send notification to specific users using Laravel Scheduler?

732 Views Asked by At

I ask this question while I have done researches to find clear solution for this problem but many of available answers are just how to use Laravel Scheduler itself!

So I am creating a simple website with Laravel to let users create reminders for themselves.
Users can receive reminder either on their Emails or if they are logged in, they receive a notification alert something like Facebook Notifications.
I am not sure if I am following the right path or not but I'm using Laravel Scheduler to send reminders to each specific user.
I also use Events in the Laravel to push recent changes every 1 minute to users but my problem is, all users receive same notification. For example if I want to remind an appointment to Mr X, Ms Y also receives same exact notification!!!

At this point, I have two questions:
1 - Is using Laravel Scheduler a good idea for this project or not? If not then what technology or method is recommended?
2 - If I have to use Laravel Scheduler for this project, so how can I send notification to related user? (I cannot use User ID as Laravel Scheduler is running by server itself and not users!)
I also attached my codes to show you what I have up to this point and I know the method I used in Laravel Channels Broadcast is somehow wrong but I just tried it!
If anyone knows the answer please help me. Thank you
Laravel Scheduler - Command - Handle Function

public function handle()
    {
        $reminders = Note::whereTime('task_date', '<=', Carbon::now()->addMinutes(30))
        ->WhereTime('task_date', '>', Carbon::now()->subMinutes(30))->get();

        foreach ($reminders as $reminder) {
            if (!$reminder->notified) {
                Note::find($reminder->id)
                ->update(['notified' => 1]);
                event(new RemindUsers($reminder));
                dd($reminder->title);
            }
        }
    }

Laravel Event

public $reminder;
    
public function __construct($reminder)
    {
        $this->reminder = $reminder;
    }
    
    public function broadcastOn()
    {
        return new PrivateChannel('remind.'.$this->reminder->user_id);
    }

    public function broadcastWith () {
        return [
            'reminders' => $this->reminder
        ];
    }

Laravel Channels

Broadcast::channel('remind.{id}', function ($user, $id) {
    $notes = Note::where('user_id', $id)->get();
    foreach ($notes as $note) {
        return $user->id === $note->user_id;
    }
});

Vue JS Code - Echo Method

data () {
      return {
        user: 1
      }
    },
CatchReminders () {
        Echo.private(`remind.${this.user}`)
          .listen('RemindUsers', (response) => {
            this.$toast.info(`${response.reminders.title}`)
          })
      }
0

There are 0 best solutions below