How To Check If Notification Is Sent Successfully In Laravel - laravel

I am unable to find in the documentation of how to handle the notification (what to do if notification is sent successfully or not)
NotificationController
public function AskForLevelUp()
{
$admin = User::where('is_admin', 1)->first();
$sendNotification = Notification::send($admin, new LevelUpNotification(Auth::user()->id));
if ($sendNotification->success()) // **HOW TO DO THIS CORRECTLY**
{
return back()->with('success', 'Your request has been submitted, please wait for admin confirmation.');
}
else
{
return back()->with('failure', 'Something went wrong, your request is not submitted. Please try again later.');
}
}
I've also read that send() has void return value here. Does anyone know how to handle this?

Related

Laravel Console Command Failing to Complete Execution

I have the script below which is used to send messages (sms, email & whatsapp) to some 300k customers. Things where working fine before but for some days now the script is bugging (it freezes at some point in the loop. I can see this with the progress bar which doesn't move further). I have checked laravel.log and there is no particular error message. The script itself does not output any error message.
Below is the code:
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
AccountsCstm::where('authentification_c', 1)->chunk(10000, function ($customer_details) {
$bar = $this->output->createProgressBar(count($customer_details));
$bar->start();
foreach ($customer_details as $customer_detail) {
// Get customer bills and send through the appropriate channel. He could have multiple bills
if (isset($customer_detail->account->sic_code)) {
$bills = TbBillsInfos::where('contract_number', $customer_detail->account->sic_code)->where('bill_status', 'UNPAID')->get();
if (isset($bills)){
foreach ($bills as $bill) {
// Send messages.
if ($customer_detail->is_whatsapp_c) {
$message = $this->bill_message($customer_detail, $bill, 'WhatsApp');
// Send the message
$this->send_whatsapp_message($customer_detail->phone_4_c, $message, $customer_detail->contract_number_c);
// Record the message in database
$this->save_message('WhatsApp', $customer_detail->phone_4_c, null, $message, $customer_detail->account->id);
} elseif ($customer_detail->is_sms_c) {
$message = $this->bill_message($customer_detail, $bill, 'SMS');
// Send the message
$this->send_sms($customer_detail->phone_4_c, $message);
// Record the message in database
$this->save_message('SMS', $customer_detail->phone_4_c, null, $message, $customer_detail->account->id);
} elseif ($customer_detail->is_mail_c) {
$message = $this->bill_message($customer_detail, $bill, 'Email');
// Send the message
$this->send_email($customer_detail, $bill, $message);
// Record the message in database
$this->save_message('Email', null, $customer_detail->mail_c, $message, $customer_detail->account->id);
}
}
}
}
$bar->advance();
}
$bar->finish();
});
// Delete all record from the tb_bills_infos table
// Set the auto-increment value to 0
TbBillsInfos::truncate();
Log::info('send:unpaid-bill-messages: Command executed successfully');
}
Please can someone point out what can be the issue with the above script? Why is it bugging and not completing execution ? Why is it freezing at some point in the loop?
See for example the command since April 21, where as before it will complete execution in about 15 minutes or less
I have equally checked RAM usage on the server and only 1GB out of 8GB is being used. So the server is stable

How can I fix the error from Backpack: view(<alert>) showing error?

I am trying to implement a method in Backpack that will show an alert when a user other than the admin role tries to log in to the admin panel, but I cannot implement the part in checkifadmin()!
private function respondToUnauthorizedRequest($request)
{
if ($request->ajax() || $request->wantsJson()) {
return response(trans('backpack::base.unauthorized'), 401);
} else {
\Alert::add('success', 'You have successfully logged in')->flash();
return backpack_view('success');
}
}
However, when I execute the code, it throws an error.
Attempt to read property "headers" on string

Laravel notify user with results when batched jobs have finished

I want to email a potentially large number of clients, so I am using Batches and pushing each email send as a batched job, like this:
public function __construct(RunReport $runReport, User $run_by) {
$this->runReport = $runReport;
$this->run_by = $run_by;
}
public function handle()
{
$company_detail = CompanyDetail::first();
$jobs = $this->runReport->runReportReportees
->map(fn(RunReportReportee $runReportReportee) => new EmailStatementJob($runReportReportee, $company_detail))
->toArray();
$batch = Bus
::batch($jobs)
->then(function(Batch $batch) {
// All completed
$completed = ($batch->totalJobs - $batch->failedJobs);
$message = "foo";
$type = "bar";
$this->run_by->notify(new GenericNotification($message, $type, 'envelope'));
})
->allowFailures()
->name('Batch name here trust me')
->dispatch();
return $batch->id;
}
However the notify line causes an error Serialization of 'Doctrine\DBAL\Driver\PDOConnection' is not allowed.
How can I notify the user that initiated the batch when the batch is finished, with the results of the included email send attempts? Alternatively, how else should I email a few hundred clients and notify the user of the results?
I figured it out eventually with help - I was using $this in the ->then() callback which was my first mistake. Then instead of the notification in the job, I have instead stored the user ID and passed it to the then() callback like this
->then(function(Batch $batch) use ($run_by_id) { ... }
In the callback instead of notifying the user, I call an event
event(new HcpStatementsBatchFinishedEvent($batch, $id));
The event simply stores the information
public function __construct(Batch $batch, int $run_by_sysid)
{
$this->run_by = User::find($run_by_id);
$this->batch = $batch;
}
And the listener for the event builds the message and notifies the user.

Get websocket data in laravel controller

I have an external WebSocket link where im getting data. My requirement is to receive those data in laravel controller and handle them.
How to listen in controller.
Finally i found the solution.
this has helped me.
$clientWebSoket = new \WebSocket\Client(
'wss://somelink'
);
$clientWebSoket->send(
'{"method":"test","symbols":"some data"}'
);
while ($i) {
try {
$message = $clientWebSoket->receive();
dump($message);
} catch (\WebSocket\ConnectionException $e) {
dd($e);
}
}
$clientWebSoket->close();

CodeIgniter - UserAgent

I have the below code to check if the user is coming from a mobile, if they are I want to load a different page but it doesn't seem to be working...
Can anyone shed any light on the below:
if($xmlRefresh==1) {
$viewData['content'] = $this->load->view('newquote/policy_xml', $viewData);
$this->load->view('layout', $viewData);
} elseif ($this->agent->is_mobile()) {
$this->_showPage("newquote/policy_mobile", 'Customise', $viewData);
} else {
$this->_showPage("newquote/policy", 'Customise', $viewData);
}
I keep getting an internal server error, can anyone spot the problem...
Look at your system/core/Loader.php, function view() that will be $this->load->view returns void unless you add third parameter. try with:
$viewData2['content'] = $this->load->view('newquote/policy_xml', $viewData1, TRUE);
and
'''
$this->_showPage("newquote/policy_mobile", 'Customise', $viewData2);
Note: $viewData2 is different with $viewData1
are you calling this in controller?
What is the 500 (internal server) error??
Did you load the library user_agent??
$this->load->library('user_agent');
if($xmlRefresh==1) {
$viewData['content'] = $this->load->view('newquote/policy_xml', $viewData);
$this->load->view('layout', $viewData);
} elseif ($this->agent->is_mobile()) {
$this->_showPage("newquote/policy_mobile", 'Customise', $viewData);
} else {
$this->_showPage("newquote/policy", 'Customise', $viewData);
}
Finally if you want serve the mobile pages, use post post or pre hooks to determine the user agent before view is executed (if you want to play around)
i have one idea, may be help u
you can replace redirect()/$this->load->view() to $this->_showPage("newquote/policy_mobile", 'Customise', $viewData);

Resources