4

Here is my code working to send notification email to multiple users

$users = User::whereIn('id', $userIds)->get();
\Notification::send($users, new DealPublished($deal));

It works but if I want to delay it like that

$users = User::whereIn('id', $userIds)->get();

$when = Carbon::now()->addSecond();

\Notification::send($users, new DealPublished($deal))->when($when);

Error is

FatalThrowableError in DealController.php line 226:
Call to a member function when() on null

How can I send notification email to multiple users using queue and Notification Facade ?

Thank's for help

user2916349
  • 412
  • 1
  • 6
  • 17
  • Try replacing`->when` with `delay` and try again. – EddyTheDove Jan 16 '17 at 23:34
  • FatalThrowableError in DealController.php line 226: Call to a member function delay() on null – user2916349 Jan 17 '17 at 00:05
  • If you just want to queue notifications, add `ShouldQueue` interface and `Queuable` trait as mentioned here https://laravel.com/docs/5.3/notifications#queueing-notifications. However it looks you are trying to schedule/delay the notification for a specific time (1 second). Referring to the doc, `delay` only applies to `notify`, which unfortunately, cannot be called with an array, but per user. Maybe a for loop ? Just suggesting. – EddyTheDove Jan 17 '17 at 00:26
  • Did you include the carbon library like so: use \Carbon\Carbon? – Paras Jan 17 '17 at 04:35
  • I'm already using ShouldQueue interface and Queuable trait with Amazon SQS and it's work fine for my others notifications like that : $when = Carbon::now()->addSecond(); $user->notify((new ForgotPassword($token))->delay($when)); but when I'm using Notification interface, it doesn't work, I don't know why and how to do it. – user2916349 Jan 17 '17 at 11:12

3 Answers3

11

Try it like this:

\Notification::send($users, (new DealPublished($deal))->delay($when));

Stubbies
  • 2,746
  • 1
  • 19
  • 30
1

I think you should try this:

$when = Carbon::now()->addSecond(10);

 \Notification::send($users, new DealPublished($deal))->later($when);

OR

\Notification::send($users, new DealPublished($deal))->when($when);

Hope this work for you!

AddWeb Solution Pvt Ltd
  • 18,949
  • 3
  • 21
  • 50
0

With a foreach loop

$when = Carbon::now()->addSecond();
 foreach($users as $user){
     $user->notify((new DealPublished($deal))->delay($when));
 }

It works, but if there is 1000+ users to notify, I'm not sure about the execution time :D

user2916349
  • 412
  • 1
  • 6
  • 17