How to change Laravel 5.4 notifications Via Manual - laravel

How to set via in controller
If one user want notification on mail and second user not want to notification in mail
So how to setup that one user notification send on mail and second user notification not send on mail
$newinvoice = New NewInvoice('create a new invoice',$invoice->id);
$newinvoice->via(['database','broadcast','mail']);
Notification::send($sendToUser, $newinvoice);
when I run this code give me error
InvalidArgumentException in Manager.php line 90: Driver [1] not supported.
Thank's in advance

You could provide the channel as another argument to your notifications constructor
$newinvoice = New NewInvoice('create a new invoice',$invoice->id, 'mail');
You can also provide an array of channels
$newinvoice = New NewInvoice('create a new invoice',$invoice->id, ['mail', 'broadcast']);
Create a property in your notifiable class
protected $_channel = null;
Your constructor can save the channels to this property
public function __construct($description, $invoiceId, $notificationChannel)
{
$this->_channel = $notificationChannel;
}
Your via() method could do things like this
public function via($notifiable)
{
if (!$this->_channel)
{
throw new \Exception('Sending a message failed. No channel provided.');
}
return is_array($this->_channel) ? $this->_channel : [$this->_channel];
}

Related

How To Check If Notification Is Sent Successfully In 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?

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.

Getting error using Laravel PayPal billing agreement

I'm getting an error using Laravel PayPal billing agreement. I received HTTP response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/billing-agreements/. I'm
getting this error once I try to complete the agreement process.
$id = my_created_plan_id
protected function agreement($id)
{
$agreement = new Agreement();
$agreement->setName('Base Agreement')->setDescription('Basic Agreement')
// ->setStartDate(date("Y-m-d").'T9:45:04Z'); ->setStartDate('2021-07-05T9:45:04Z');
$agreement->setPlan($this->Plan($id));
$agreement->setPayer($this->payer());
$agreement->setShippingAddress($this->shippingAddress());
$agreement = $agreement->create($this->apiContext);
return $agreement->getApprovalLink();
}
protected function Plan($id)
{
$plan = new Plan();
$plan->setId($id);
return $plan;
}
protected function payer()
{
$payer = new Payer();
$payer->setPaymentMethod('paypal');
return $payer;
}
protected function shippingAddress()
{
$shippingAddress = new ShippingAddress();
$shippingAddress->setLine1('111 First Street')
->setCity('Saratoga')
->setState('CA')
->setPostalCode('95070')
->setCountryCode('US');
return $shippingAddress;
}
You are using a deprecated SDK that does not support the current version of PayPal Subscriptions, for which there is no SDK.
Change your integration to not use that old SDK. The new Subscribe button itself is JavaScript. Use direct HTTPS calls with curl or similar when you need to call an API to create Products and Plans or administer Subscriptions.
(You can also do so in your account's web interface, rather than via API)
Sandbox: https://www.sandbox.paypal.com/billing/
Live: https://www.paypal.com/billing/
protected function agreement($id)
{
$agreement = new Agreement();
$agreement->setName('Base Agreement')->setDescription('Basic Agreement')
// ->setStartDate(date("Y-m-d").'T9:45:04Z'); ->setStartDate('2021-07-05T9:45:04Z');
//Replace above line with below line
//->setStartDate(gmdate("Y-m-d\TH:i:s\Z", time()+60));
$agreement->setPlan($this->Plan($id));
$agreement->setPayer($this->payer());
$agreement->setShippingAddress($this->shippingAddress());
$agreement = $agreement->create($this->apiContext);
return $agreement->getApprovalLink();
}
You cannot comment on the start date of the agreement.
you must provide the start date of agreement and the error occurred due to this issue.

spring websocket notification

Ok here i'm , i'm right now following the guides on spring site but i'm having problem on how to deliver a notification to only one user using WebSocket, i'm following this guide https://spring.io/guides/gs/messaging-stomp-websocket/ .
What I want is: i have 2 users, both of them subscribe to process1... User1 need to let the server process his pass...now i want that the server will deliver the notification only to User1...
#Controller
public class ProcessController {
#MessageMapping("/ProcessOwner/approve/{pass}")
#SendTo("")
public String notifica(#DestinationVariable String pass)throws Exception{
return "ok"+pass;
}
}
Now what should i write in the #SendTo field to deliver the answer only to user1 ? if ill write /Process/process1 both user1 and user2 will receive the message....
You ca use the sufix. It's send to every unique client.
When you subscribe in the js code:
client.connect('guest', 'guest', function(frame) {
var suffix = frame.headers['queue-suffix'];
client.subscribe("/queue/error" + suffix, function(msg) {
// handle error
});
client.subscribe("/queue/position-updates" + suffix, function(msg) {
// handle position update
});
});
On the server side you can use #ReplyToUser or the message template
String user = "fabrice";
String queue = "/queue/position-updates";
this.messagingTemplate.convertAndSendToUser(user, queue, position);
See more here: http://assets.spring.io/wp/WebSocketBlogPost.html (section: Sending Messages To a Single User)

WCF Data Service - Add Object With Related Object

I have the following situation, I have a WCF Data Service with User objects and message objects and the message object has two relations to user, a sender and a receiver.
When I try to add a new Message object the related users are left null
Message message = new Message();
message.text = InputText; // string
message.Sender = Sender; // User object
message.Receiver = Receiver; // User object
context.AddToMessages(message);
context.BeginSaveChanges(new AsyncCallback((result) =>
{
// Some code
}));
Now the Sender and Receiver will be null. When I try to set a link before the BeginSaceChanges like this I get the error "InvalidOperationException: The context is not currently tracking the entity."
context.AddToMessages(message);
context.AddLink(message, "Sender", message.Sender);
context.AddLink(message, "Receiver", message.Receiver);
context.BeginSaveChanges(new AsyncCallback((result) =>
{
// Some code
}));
How do I make sure the relations are created properly?
Thanks to Pratik I found the solution. I had to use attach the already existing users Sender and Receiver to the context first because they weren't tracked (and added a if if they are on the second call). Then I add the message and use SetLink to set the link to both users (instead of AddLink)
if(context.GetEntityDescriptor(message.Sender) == null)
context.AttachTo("Users", message.Sender);
if (context.GetEntityDescriptor(message.Receiver) == null)
context.AttachTo("Users", message.Receiver);
context.AddToMessages(message);
context.SetLink(message, "Sender", message.Sender);
context.SetLink(message, "Receiver", message.Receiver);
context.BeginSaveChanges(new AsyncCallback((result) =>
{
// Some code
}));
I believe you need to use the DbSet.Attach method instead. I assume you use Entity Framework on the back end here.

Resources