PHP code example of eonx-com / easy-notification

1. Go to this page and download the library: Download eonx-com/easy-notification 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/ */

    

eonx-com / easy-notification example snippets


// src/Listener/UserCreatedListener.php

namespace App\Listener;

use App\Entity\User;
use EonX\EasyNotification\Client\NotificationClientInterface;
use EonX\EasyNotification\Message\RealTimeMessage;
use EonX\EasyNotification\Provider\ConfigProviderInterface;

final class UserCreatedListener
{
    /**
     * @var \EonX\EasyNotification\Provider\ConfigProviderInterface
     */
    private $configFinder;

    /**
     * @var \EonX\EasyNotification\Client\NotificationClientInterface
     */
    private $client;

    public function __construct(ConfigProviderInterface $configFinder, NotificationClientInterface $client)
    {
        $this->configFinder = $configFinder;
        $this->client = $client;
    }

    public function created(User $user): void
    {
        /**
         * In real use case, those values should come from configuration.
         *
         * For multi-tenancies application, it is a best practice to store those values against each tenancy.
         */
        $config = $this->configFinder->provide('my-api-key', 'my-provider-external-id');

        /**
         * Topics are "channels" to send notifications to.
         * Every clients subscribing to any of the topics will then receive the notification.
         */
        $topics = [$user->getExternalId()]; // Good practice is to have 1 topic per user

        /**
         * Body is the notification contents each client will receive.
         * Its structure is completely up to the application but should negotiated with subscribers.
         */
        $body = [
            'title' => 'Welcome!',
            'body' => \sprintf('We are to have you onboard %s', $user->getUsername()),
        ];

        $this->client
             ->withConfig($config) // Set config for next send
             ->send(RealTimeMessage::create($body, $topics)); // Send real time message
    }
}