I want to send a mail to Notify, it works but when I try to put the variables, It returns that they are undefined. I don't understand how to pass a variable to Notify, I tried to do ->withResult($result) but it didn't work out.
Here is the controller:
$result = Review::where('user_hash', '=', $data['lname_c'])->where('email', $data['email_c'])->orderBy('created_at', 'desc')->first();
$result->notify(new SendReview());
And my SendReview.php notifications:
public function toMail($notifiable)
{
return $result['invitation_id']; // test to check if variable is passed
return (new MailMessage)
->line('Test.')
->action('Nani', url($url))
->line('Thank you');
}
There is user_hash and invitation_id in my Review table, I want to pass them to the notify. When I do return $result['invitation_id']; it works. Hope I am understandable, I checked for duplicate questions and couldn't find one.
This is how they do it in docs.
$arr = [ 'foo' => "bar" ];
$result->notify(new SendReview($arr));
And in your SendReview.php
...
protected $arr;
public function __construct(array $arr) {
$this->arr = $arr;
}
public function toMail($notifiable) {
// Access your array in here
dd($this->arr);
}
You must use $notifiable variable in your Notification class.
It is an instance of the class to which the notification is being sent. So here your review object is passed as $notifiable variable.
You can try to log it as logger($notifiable) in toMail() method and check its properties.
For people interested in passing an object e.g $msg_recipient and sending an email to a specific user from a controller.
Controller:
Remember to add use Illuminate\Support\Facades\Notification;
Notification::route('mail',$msg_recipient->email)
->notify(new MsgReceived($msg_recipient));
In your notification __construct
public function __construct($msg_recipient)
{
$this->username = $msg_recipient->username;
}
toMail function
public function toMail($notifiable)
{
$username = $this->username;
return (new MailMessage)
->greeting('Hello, '.$username)
->line('You received a brand new msg!')
->action('View msg', url('/'.$username));
}
Related
I'm using Laravel 5.8 and I have created an Event called UserWalletNewTransaction that goes like this:
public $transaction;
public $added_type;
public function __construct($transaction, $added_type)
{
$this->transaction = $transaction;
$this->added_type = $added_type;
}
As you can see I have specified two parameters here and these parameters are getting their value from the Controller:
event(new UserWalletNewTransaction($newTransaction, $value_added_type));
And then the Listener which is named UserWalletNotification goes like this:
public function handle(UserWalletNewTransaction $event, $added_type) {
But there is something wrong here, since I'm getting this error:
Too few arguments to function App\Listeners\UserWalletNotification::handle(), 1 passed and exactly 2 expected
So how to fix this issue? How can I pass two parameters to Event & Listener properly?
Update 1
Error Screenshot:
UserWalletNewTransaction event:
public $transaction;
public $added_type;
public function __construct($transaction, $added_type)
{
$this->transaction = $transaction;
$this->added_type = $added_type;
}
which you can call like this event(new UserWalletNewTransaction($newTransaction, $value_added_type));
and UserWalletNotification listener:
public function handle(UserWalletNewTransaction $event)
{
$transaction = $event->transaction;
$added_type = $event->added_type;
}
You can use array.
event(new UserWalletNewTransaction([ 'transaction' => $newTransaction, 'added_type' => $value_added_type));
public $transaction;
public $added_type;
public function __construct($data)
{
$this->transaction = $data['transaction'];
$this->added_type = $data['added_type'];
}
UPDATE #1:
You should remove 2nd argument of handle function. You can access added_type data in $event variable.
I made my code where an administrator can send a message to a group, and soon all users within that group receive notification on the mobile app:
public function create(Request $request)
{
if(!($error = $this->isNotValidate($request))){
//Log::error(print_r("validado", true));
$message = Message::create($request->toArray());
$message->recives()->attach($request->recive_ids);
foreach($message->recives as $group){
foreach($group->users as $user){
$user->notify(new NotificationMessage($message));
}
}
$response = ['message' => $message];
return response($response, 200);
}
return response($error, 422);
}
I used the library: https://github.com/laravel-notification-channels/fcm it is working he send the notification to the mobile through the firebase.
my difficulty is that in the toFcm function I want to retrieve the message to build the notification I'm sending:
public function toFcm($notifiable)
{
return FcmMessage::create()
->setData(['message_id' => $this->invoice->id, 'message_created' => $this->invoice->created_at])
->setNotification(\NotificationChannels\Fcm\Resources\Notification::create()
->setTitle($this->invoice->title)
->setBody($this->invoice->content)
//->setImage('http://example.com/url-to-image-here.png')
)->setAndroid(
AndroidConfig::create()
->setFcmOptions(AndroidFcmOptions::create()->setAnalyticsLabel('analytics'))
->setNotification(AndroidNotification::create()->setColor('#0A0A0A'))
)->setApns(
ApnsConfig::create()
->setFcmOptions(ApnsFcmOptions::create()->setAnalyticsLabel('analytics_ios')));
}
I thought that in the variable $this->invoice or in the $notifiable would come the $message that I am passing as a parameter in the creation, but it does not pass, both are returning the user.
does anyone know how i can make this dynamic notification with my message data?
UPDATE
class NotificationMessage extends Notification
{
/**
* Specifies the user's FCM token
*
* #return string|array
*/
public function routeNotificationForFcm()
{
return $this->fcm_token;
}
public function via($notifiable)
{
return [FcmChannel::class];
}
public function toFcm($notifiable)
{
return FcmMessage::create()
->setData(['message_id' => $notifiable->id, 'message_created' => $notifiable->created_at])
->setNotification(\NotificationChannels\Fcm\Resources\Notification::create()
->setTitle($notifiable->title)
->setBody($notifiable->content)
//->setImage('http://example.com/url-to-image-here.png')
)->setAndroid(
AndroidConfig::create()
->setFcmOptions(AndroidFcmOptions::create()->setAnalyticsLabel('analytics'))
->setNotification(AndroidNotification::create()->setColor('#0A0A0A'))
)->setApns(
ApnsConfig::create()
->setFcmOptions(ApnsFcmOptions::create()->setAnalyticsLabel('analytics_ios')));
}
// optional method when using kreait/laravel-firebase:^3.0, this method can be omitted, defaults to the default project
public function fcmProject($notifiable, $message)
{
// $message is what is returned by `toFcm`
return 'app'; // name of the firebase project to use
}
}
I decided to invert the logic of who was my notify, now instead of the user being the notify, my message became
In my control I'm even simpler:
public function create(Request $request)
{
if(!($error = $this->isNotValidate($request))){
//Log::error(print_r("validado", true));
$message = Message::create($request->toArray());
$message->recives()->attach($request->recive_ids);
$message->notify(new NotificationMessage);
$response = ['message' => $message];
return response($response, 200);
}
return response($error, 422);
}
in my message template I made the following changes:
use Illuminate\Notifications\Notifiable;
...
class Message extends BaseModel
{
use Notifiable;
...
public function routeNotificationForFcm()
{
$tokens = [];
foreach($this->recives as $group){
foreach($group->users as $user){
$tokens = array_merge($tokens, $user->notifications()->pluck('token')->toArray());
}
}
return $tokens;
}
}
with that in my variable $notifiable started to receive the information of the message and then it was easy to pass them as parameter.
I have customer data in my database with their emails. In my case, i want to get user email from customer table and send a custom email to customer. I'm new to laravel and this is the very first time i'm working with laravel notification. Help me to do this!
I have created this SendMailController:
public function sendNotifications(Request $request, $id)
{
$id = $request->id;
$customer_id = DB::table('jobs')->select('customers_id')->where('id', '=', $id)->get();
$customer_email = DB::table('customer')->select('email')->where('id', '=', $customer_id)->get();
$customer_firstname = DB::table('customer')->select('firstname')->where('id', '=', $customer_id)->get();
sendEmail($customer_firstname, $customer_email);
return view('admin.sendNotifications');
}
I have created a sendEmail Notification class:
class sendEmail extends Notification
{
use Queueable;
public function __construct($customer_firstname, $customer_email)
{
$this->customer_email = $customer_email;
$this->customer_firstname = $customer_firstname;
}
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
return (new MailMessage)
->error()
->subject('Thank you!')
->from('hashankannangara#gmail.com', 'Sender')
->to($this->customer_email)
->line($this->customer_firstname.''.'Thanks for got services from us!');
}
public function toArray($notifiable)
{
return [
//
];
}
}
How can i pass the customer email to to() function? I want to display customer first name in email with Thanking them. How can i use a custom template and add to this email? I appreciate if you can help me.
https://laravel.com/docs/6.x/notifications#mail-notifications
You can use something like this in toMail method:
return (new CustomMail($this->customer))->to($this->customer->email);
and when you call notify you must pass a customer
use Mail;
$data = 'set data variable pass in view'
$email= 'receiver' email'
Mail::send('here set your file for view (ex. email.access(blade))', $data, function ($message) use ($email) {
$message->from('sender mail','Title')->subject('Register');
$message->to($email);
});
I am writing tests to validate that a locale is set for a notification and asserting that the correct translation is used in the notification's content. I have been unable to get the locale set on notifications when using Twilio: https://github.com/laravel-notification-channels/twilio
I have a notification test for a mail channel that uses a Mailable which is working as expected:
public function test_mail_notifications_use_localisation()
{
$user = factory(User::class)->create();
$user->notify(
(new WelcomeMailNotificationStub)
->locale('fr')
);
$this->assertContains('Bonjour Le Monde',
app('swift.transport')->messages()[0]->getBody()
);
}
class WelcomeMailStub extends \Illuminate\Mail\Mailable
{
public function build()
{
return $this->view('welcome-notification');
}
}
class WelcomeMailNotificationStub extends \Illuminate\Notifications\Notification
{
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
return (new WelcomeMailStub);
}
}
I would like to write a similar test for SMS notifications sent via Twilio but in my attempts so far the locale appears to be ignored when sending the notification and the default locale is instead used. Here's what I've come up with:
public function test_sms_notifications_use_localisation()
{
Notification::fake();
$user = factory(User::class)->create();
$user->notify(
(new WelcomeSmsNotificationStub)
->locale('fr')
);
Notification::assertSentTo(
$user,
WelcomeSmsNotificationStub::class,
function ($notification, $channels) use ($user)
{
return $notification->toTwilio($user)->content === 'Bonjour Le Monde'; // test fails here
}
);
}
class WelcomeSmsNotificationStub extends \Illuminate\Notifications\Notification
{
public function via($notifiable)
{
return [TwilioChannel::class];
}
public function toTwilio($notifiable)
{
return (new TwilioSmsMessage())
->content(__('welcome_notification.opening_text'));
}
}
If I dd() inside the callback in assertSentTo like this:
Notification::assertSentTo(
$user,
WelcomeSmsNotificationStub::class,
function ($notification, $channels) use ($user)
{
dd(
$notification,
$notification->toTwilio($user)
);
return $notification->toTwilio($user)->content === 'Bonjour Le Monde'; // test fails here
}
);
I get the following:
Tests\Unit\Notifications\WelcomeSmsNotificationStub {#116
+id: "ae164ce6-fa47-4730-b401-e6cc15b27e16"
+locale: "fr"
}
NotificationChannels\Twilio\TwilioSmsMessage {#2414
+alphaNumSender: null
+applicationSid: null
+maxPrice: null
+provideFeedback: null
+validityPeriod: null
+content: "Default welcome" <== using the default welcome instead of 'fr'
+from: null
+statusCallback: null
+statusCallbackMethod: null
}
Any advice on getting this working would be appreciated, thanks!
User exohjosh has solved the issue. I need to explicitly set the translated string's locale in the SMS Notification's toTwilio method like this:
__('welcome_notification.opening_text', [], $this->locale)
whereas when using Laravel's Mailable this was done automatically.
Edit
Event better solution if you're using views is to use Illuminate\Support\Traits\Localizable then you can do this:
public function toTwilio($notifiable)
{
return $this->withLocale($this->locale, function() {
$content = View::make('welcome-notification')->render();
return (new TwilioSmsMessage())->content($content);
});
}
When I am trying to send mail, everytime a new member is added to the user table, so that they can get a setup password link. I have been trying to get this to work but seem not to be.
public function store(AddUser $request)
{
$user = $request->all();
$user['activate'] = $this->active();
$user['guid'] = $this->guid();
$user['accountno'] = $this->generateAndValidateAccountno();
$check = User::find($user['phone']);
if(!$check) {
$id = User::create($user);
$this->sendEmail($user['accountno']);
}
return redirect('employee');
}
public function sendEmail(Request $request, $id)
{
$user = User::find($id);
Beautymail::send('emails.welcome', [], function($message)
{
$message
->to('$id->email', '$id->fname')
->subject('Welcome!');
});
}
}
Not sure what am doing wrong
Just use the same request class in the controller and the model. In your user model, add use Illuminate\Http\Request at the top of the class to tell it which Request class to use.
Just change:
public function sendEmail(Request $request, $id){...}
to
public function sendEmail($id){...}