How to access notification into channel laravel - laravel

I try to access my notification into channel notification but I don't get it.
$notify_user = new UserNotification($user) ;
I need get $notify_user into notification channel laravel
class MailChannel
{
public function send($notifiable, Notification $notification)
{
//I try to get $notify_user
** $notify_user ....**
....
}
I can get information into toMail by
class MailChannel
{
public function send($notifiable, Notification $notification)
{
//such as this
$info = $notification ->toMail(notifiable) ;
....
}
but I don't want to get my notification by passing extra parameter info toMail.

Related

Laravel Bulk Send Mail with Notification

I read in laravel documentation :
Once the ShouldQueue interface has been added to your notification, you may send the notification like normal. Laravel will detect the ShouldQueue interface on the class and automatically queue the delivery of the notification:
Because of in notification do automatically queue,
if I use in my controller:
public function store(Request $request)
{
$users = User::all()
Notification::send($users, new MyFirstNotification());
}
and in my notification:
public function toMail($notifiable)
{
return new custome-emailTo("emails.welcome",$notifiable);
}
and in custom-mailTo, (it is a mailable class) :
public function __construct($view2,User $user)
{
$this->user = $user;
$this->view = $view2;
}
public function build()
{
$this->to($this->user->email);
return $this->view('emails.welcome');
}
for me, it work and send to many users,
but my questions are:
As stated in the documentation of Laravel,
1. Do it really do queuing for send notification?
2. Do I need queue in mailable laravel class for send bulk email?

Laravel Notifications - delay email sending and cancel if condition met

I have an app where I am sending a push notification which is fine if the user is logged into the application - however, if they're not / if they have not read the notification within X minutes I'd like to send them an email.
The way I am going about this is to use Laravel Notifications to create a mail, broadcast & database notification. On the toMail() method I'm returning a mailable with a delay -
public function toMail($notifiable)
{
return (new \App\Mail\Order\NewOrder($this->order))
->delay(now()->addMinutes(10));
}
After the minutes are up, the email will send but, before the send goes ahead I'd like to perform a check to see if the push/database notification has already been marked as read and if it has cancel the email send. The only way I can think to do this is to bind to the MessageSending event that is baked into Laravel -
// listen for emails being sent
'Illuminate\Mail\Events\MessageSending' => [
'App\Listeners\Notification\SendingEmail'
],
The only problem is this listener receives a Swift mail event and not the original mailable I was dispatching so I don't know how to cancel it. Any ideas and thanks in advance?
Class extends Notification
public function via($notifiable)
{
if($this->dontSend($notifiable)) {
return [];
}
return ['mail'];
}
public function dontSend($notifiable)
{
return $this->appointment->status === 'cancelled';
}
Class EventServiceProvider
protected $listen = [
NotificationSending::class => [
NotificationSendingListener::class,
],
];
Class NotificationSendingListener
public function handle(NotificationSending $event)
{
if (method_exists($event->notification, 'dontSend')) {
return !$event->notification->dontSend($event->notifiable);
}
return true;
}
For more details look article Handling delayed notifications in Laravel

Send message to multiple users in Protected Channel

I have the following code that contains info about one user to send read time message.
Question: Is there any way to send message to more 10 users my current code sends message to one user like this
return new PrivateChannel('SendMessageChannel.1');
Event Class
class SendMessageEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $Message;
public function __construct($message)
{
$this->Message = $message;
}
public function broadcastOn()
{
return new PrivateChannel('SendMessageChannel.1');
}
}
If I'm not wrong you are trying to create a group chat where multiple users can chat with each other in a single chat room.
What you want is not just up to the Echo implementation. It also requires database structure accordingly.
So I can give you the brief idea about how I did previously.
I have a chatrooms table which contains ids(in comma separated form) of all the users which are added to that room. In channel route, this how I'm checking that a specific user should allow to read a message or not:
Broadcast::channel('private-chat-room-{chatRoom}', function ($user, $chatRoom) {
$chatRoom = App\Models\ChatRoom::find($chatRoom);
if(in_array(auth()->user()->id, explode(',', $chatRoom->user_ids))) {
return true;
} else {
return false;
}
});

Laravel notification facade check user before sending notification

I have a situation that I am sending a notification to multiple users and in past i have used this code:
foreach ($users as $user) {
$user->notify(new StaffNotify($dirtyAttributes, $user));
}
and I would check inside that notification if a user has a player_id
public function via($notifiable)
{
if ($this->user->player_id) {
return [OneSignalChannel::class, 'mail'];
} else {
return ['mail'];
}
}
(for OneSignal) and if he has I would send a push notification also on their mobile phone.
But with this new code:
\Notification::send($users, new StaffNotify($dirtyAttributes));
It is much better because i have only 1 request on my server instead of 250. I don't know how to check if a user has player_id because this works differently.
Does anyone know how to check the user before sending the notification?
You don't have to pass the user as an argument, you already have it in $notifiable and you can check what ever you want.
public function via($notifiable)
{
if ($notifiable->player_id) {
return [OneSignalChannel::class, 'mail'];
} else {
return ['mail'];
}
}

Send email in background : Laravel 5.4

I am using an inbuilt code in Laravel to send Email Notification. Code is below. I am using smtp to send email
class RegisterNotification extends Notification
{
use Queueable;
public function __construct($token)
{
$this->token = $token;
}
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
return (new MailMessage)
->line('hi');
}
public function toArray($notifiable)
{
return [
//
];
}
}
Here the problem is, it takes around 5 seconds to complete the process and control does not come back. I am assuming that if it come back and do the email sending work in background...it would save a lot of time.
Is there any inbuilt work to do the same? I meant, control should come back and should say email sent...and then it should do the work in background.
Email Sending code in Controller
class apiRegisterController extends Controller
{
public function Register(RegisterRequest $request) {
$RegisterNotification = new RegisterNotification($Token);
$User->notify($RegisterNotification);
}
}
Code for Queue
Controller Code
$job = (new SendForgotPasswordEmail($Data))->onConnection('database');
dispatch($job);
Job
class SendForgotPasswordEmail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $Data;
public $tries = 5;
public function __construct($Data)
{
$this->Data = $Data;
}
public function handle()
{
$Token = $this->Data["Token"];
$User = $this->Data["User"];
$RegisterNotification = new RegisterNotification($Token);
$User->notify($RegisterNotification);
}
}
Step 1: Change class RegisterNotification extends Notification to class RegisterNotification extends Notification implements ShouldQueue
Step 2: Implement a queue driver. In your config/queue.php make sure your driver is not set to sync like so: 'default' => env('QUEUE_DRIVER', 'sync'), and make sure your .env doesnt have QUEUE_DRIVER=sync. You can look at the Laravel documentation for queues to choose an appropriate queue driver
You can use the build-in API.
$user = User::findOrFail($id);
Mail::queue('emails.welcome', $data, function ($message) use ($user){
$message->from('hello#app.com', 'Your Application');
$message->to($user->email, $user->name)->subject('Your Reminder!');
});
But first you have to configure the queues.
Add in your .env file the line QUEUE_DRIVER=sync and then write on the terminal php artisan queue:listen.
If you want the queue to run forever on the server use Supervisor. Queues documentation explains how you can use it.
you can use laravel job queue https://laravel.com/docs/5.4/queues
Mail::queue(

Resources