Sami
Sami

Reputation: 129

get variable when createing custom notification channel in laravel

In Laravel documentation, There is section explained how to make a custom notification class, and the URL is :

https://laravel.com/docs/5.4/notifications#custom-channels

So I created a notification class called `MessageReplied', and I want to define a custom SMS channel for it,

In MessageReplied Class, codes are like this:

<?php

namespace App\Notifications;

use App\Channels\SmsChannel;
use App\WorkCase;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class MessageReplied extends Notification
{
use Queueable;
public $workCase;

/**
 * Create a new notification instance.
 *
 * @param WorkCase $workCase
 */
public function __construct(WorkCase $workCase)
{
    $this->workCase = $workCase;
}

/**
 * Get the notification's delivery channels.
 *
 * @param  mixed  $notifiable
 * @return array
 */
public function via($notifiable)
{
    return ['database', 'mail', SmsChannel::class];
}

/**
 * Get the mail representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return \Illuminate\Notifications\Messages\MailMessage
 */
public function toMail($notifiable)
{
    return (new MailMessage)
                ->markdown('mail.message_replied', ['workCase' => $this->workCase])
                ->subject('new Message');
}

/**
 * Get the array representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return array
 */
public function toArray($notifiable)
{
    return [
        'workCase' => $this->workCase
    ];
}

/**
 * @return array
 */
public function toSms()
{
    return [
        'foo' => 'bar'
    ];
}


}

and my custom Channel is called SmsChannel and contains:

<?php

namespace App\Channels;
use App\WorkCase;
use GuzzleHttp\Client;
use Illuminate\Notifications\Notification;
use function MongoDB\BSON\toJSON;

class SmsChannel
{

/**
 * Send the given notification.
 *
 * @param  mixed  $notifiable
 * @param  \Illuminate\Notifications\Notification  $notification
 * @return void
 */
public function send($notifiable, Notification $notification)
{
    var_dump($notifiable);
    $text = !!!How Can I Get this variable from MessageReplied Class ???;

    $guzzle = new Client();
     $guzzle->post('https://api.exapme.com/v1/93253374C30696465434E325645513D3D/sms/send.json', [
        'form_params' => [
            'receptor' => $notification->workCase->client->cellphone,
            'message' => $text,
//              'sender'  => config('sms.sender')
        ],
        'verify' => false,
    ]);
}

}

As you see in SmsChannel class how can I get bar value which is set in MessageReplied Class?

Upvotes: 0

Views: 1205

Answers (2)

hossein emami
hossein emami

Reputation: 286

in the send method on the SmsChannel class :

public function send($notifiable, Notification $notification)
{
    $message = $notification->toSms(notifiable);
    $bar = $message['bar'];
}

Upvotes: 0

Denis G.
Denis G.

Reputation: 41

From documentation:

$message = $notification->toSms();

Upvotes: 1

Related Questions