PHP code example of notify-eu / notify

1. Go to this page and download the library: Download notify-eu/notify library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

notify-eu / notify example snippets


// config/services.php
...
''notify' => [
         'clientID' => env('NOTIFY_CLIENT_ID'),
         'secret' => env('NOTIFY_SECRET'),
         'transport' => env('NOTIFY_TRANSPORT'),
         'url' => env('NOTIFY_URL')
],
...

// .env
...
NOTIFY_CLIENT_ID=
NOTIFY_SECRET=
NOTIFY_TRANSPORT=
NOTIFY_URL=
],
...


namespace App\Listeners;
	
use Illuminate\Notifications\Events\NotificationFailed;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Log;
use NotifyEu\Notify\NotifyChannel;
	
class NotificationFailedListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Notification failed event handler
     *
     * @param  NotificationFailed  $event
     * @return void
     */
    public function handle(NotificationFailed $event)
    {
        // Handle fail event for Notify
        //
        if($event->channel == NotifyChannel::class) {
	            
            $logData = [
            	'notifiable'    => $event->notifiable->id,
            	'notification'  => get_class($event->notification),
            	'channel'       => $event->channel,
            	'data'      => $event->data
            	];
            	
            Log::error('Notification Failed', $logData);
         }
    }
}

...
protected $listen = [

	'Illuminate\Notifications\Events\NotificationFailed' => [
		'App\Listeners\NotificationFailedListener',
	],

	'Illuminate\Notifications\Events\NotificationSent' => [
		'App\Listeners\NotificationSentListener',
	],
];
...
 php
use App\User;
use Illuminate\Notifications\Notification;
use NotifyEu\Notify\NotifyChannel;
use NotifyEu\Notify\NotifyMessage;

class InvoicePaid extends Notification
{
    const TYPE = 'buyerContractApproval';
    protected $user;
    private $cc = [];
    private $bcc = [];


    /**
     * InvoicePaid constructor.
     * @param User $user
     */
    public function __construct(User $user)
    {
        $this->user = $user;
    }

    /**
     * @param $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return [NotifyChannel::class];
    }

    /**
     * @param $notifiable
     * @return NotifyMessage
     */
    public function toNotify($notifiable)
    {
        return NotifyMessage::create()
            ->setNotificationType(self::TYPE)
            ->setTransport('mail')
            ->setLanguage('en')
            ->setParams($this->getParams())
            ->setCc($this->cc)
            ->setBcc($this->bcc);
    }

    /**
     * @return array
     */
    private function getParams()
    {
        return array('userToken' => $this->user->getRememberToken());
    }

    /**
     * @param array $cc
     * format: array(array('name' => 'John Doe', 'recipient' => '[email protected]')
     */
    public function addCc(array $cc)
    {
        $this->cc = $cc;
    }

    /**
     * @param array $bcc
     * format: array(array('name' => 'John Doe', 'recipient' => '[email protected]')
     */
    public function addBcc(array $bcc)
    {
        $this->bcc = $bcc;
    }
 php
/**
 * Route notifications for the notify channel.
 *
 * @return string
 */
public function routeNotificationForNotify()
{
    return [
        'name' => $this->name,
        'recipient' => $this->email,
    ];
}