Edit File: NotifyUser.php
<?php namespace App\Jobs; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use App\Traits\Firebase; use App\Notifications\NotifyUser as Notify ; class NotifyUser implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Firebase; /** * Create a new job instance. * * @return void */ protected $users , $data , $type , $count; public function __construct($users , $request , $type , $count='all' ) { $data = [] ; $this->data = [ 'sender' => auth('admin')->id(), 'sender_name' => auth('admin')->user()->name, 'sender_avatar' => auth('admin')->user()->avatar, 'title_ar' => $request->title_ar , 'title_en' => $request->title_en , 'message_ar' => $request->message_ar, 'message_en' => $request->message_en, 'type' => 'admin_notify' , ]; $this->type = $type; $this->users = $users; $this->count = $count; } /** * Execute the job. * * @return void */ public function handle() { $users = $this->users; $tokens = []; $android_tokens = []; $ios_tokens = []; if ($this->type !== 'email') { if ($this->count == 'all') { foreach ($users as $user) { if ($user->devices->count() > 0) { foreach ($user->devices()->where('device_type' , 'android')->get() as $device) { if($device->user->new_orders_notify=='true') { $android_tokens[] = $device->device_id; } } foreach ($user->devices()->where('device_type' , 'ios')->get() as $device) { if($device->user->new_orders_notify=='true') { $ios_tokens[] = $device->device_id; } } } $user->notify(new Notify($this->data)); } $this->sendNotification($ios_tokens, $android_tokens , $this->data); } else { if ($users->devices->count() > 0) { foreach ($users->devices()->where('device_type' , 'android')->get() as $device) { if($device->user->new_orders_notify=='true') { $android_tokens[] = $device->device_id; } } foreach ($users->devices()->where('device_type' , 'ios')->get() as $device) { if($device->user->new_orders_notify=='true') { $ios_tokens[] = $device->device_id; } } } $users->notify(new Notify($this->data)); $this->sendNotification($ios_tokens, $android_tokens , $this->data); } } if ($this->users instanceof \Illuminate\Database\Eloquent\Collection){ foreach ($this->users as $user){ if ($this->type == 'email') { \Mail::to($user->email)->send(new \App\Mail\SendMail($this->data)); } } }else{ if ($this->type == 'email') { \Mail::to($this->users->email)->send(new \App\Mail\SendMail($this->data)); } } } }
Back to File Manager