PHP code example of deegitalbe / laravel-trustup-io-notifications

1. Go to this page and download the library: Download deegitalbe/laravel-trustup-io-notifications 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/ */

    

deegitalbe / laravel-trustup-io-notifications example snippets


use App\Models\Comment;
use Illuminate\Notifications\Notification;
use Deegitalbe\TrustupIoNotificationsClient\Channel\TrustupIoNotificationsChannel;
use Deegitalbe\TrustupIoNotificationsClient\Concerns\InteractsWithTrustupIoNotifications;
use Deegitalbe\TrustupIoNotificationsClient\Contracts\SendsTrustupIoNotification;
use Deegitalbe\TrustupIoNotificationsContracts\Contracts\NotificationData;
use Deegitalbe\TrustupIoNotificationsContracts\Data\ToolsCommentNotificationData;

class CommentNotification extends Notification implements SendsTrustupIoNotification
{
    use InteractsWithTrustupIoNotifications;

    public function __construct(private Comment $comment) {}

    public function via(object $notifiable): array
    {
        return [TrustupIoNotificationsChannel::class];
    }

    public function toTrustupIoNotificationsData(object $notifiable): NotificationData
    {
        return new ToolsCommentNotificationData(
            product_url: $this->comment->product->url,
            product_name: $this->comment->product->name,
            body: $this->comment->body,
            attachment_details: $this->comment->attachmentDetails(),
            commenter_name: $this->comment->author->name,
            timestamp: $this->comment->created_at->toIso8601String(),
            action_url: $this->comment->url,
            notifications_url: route('notifications.index'),
            company_name: $this->comment->product->company->name,
            company_address: $this->comment->product->company->address,
        );
    }
}

use Illuminate\Notifications\Notifiable;
use Deegitalbe\TrustupIoNotificationsClient\Concerns\RoutesTrustupIoNotifications;
use Deegitalbe\TrustupIoNotificationsClient\Contracts\NotifiableViaTrustupIoNotifications;
use Deegitalbe\TrustupIoNotificationsContracts\Enums\Source;
use Deegitalbe\TrustupIoNotificationsContracts\Request\Recipient;

class User extends Model implements NotifiableViaTrustupIoNotifications
{
    use Notifiable;
    use RoutesTrustupIoNotifications;

    public function toTrustupIoNotificationsRecipient(): Recipient
    {
        return Recipient::identified((string) $this->id);
    }
}

$user->notify(new CommentNotification($comment));

use Illuminate\Support\Facades\Event;
use Deegitalbe\TrustupIoNotificationsClient\Events\TrustupIoNotificationStatusReceived;
use Deegitalbe\TrustupIoNotificationsClient\Events\TrustupIoNotificationEngagementReceived;

Event::listen(TrustupIoNotificationStatusReceived::class, function ($event) {
    $event->payload->sendId;         // string
    $event->payload->channel;        // NotificationChannel: email|sms|push
    $event->payload->status;         // NotificationStatus: pending|sent|delivered|error
    $event->payload->type;           // NotificationType
    $event->payload->data;           // hydrated NotificationData
});

Event::listen(TrustupIoNotificationEngagementReceived::class, function ($event) {
    $event->payload->kind;           // ChannelEventKind: delivered|bounced|spam_complaint|opened|clicked|...
    $event->payload->clickedUrl;     // ?string (set for clicks)
    $event->payload->sendId;
    $event->payload->channel;
});

use Deegitalbe\TrustupIoNotificationsContracts\Enums\NotificationChannel;

public function restrictTrustupIoNotificationsChannels(object $notifiable): ?array
{
    return [NotificationChannel::Email]; // email only, even if the data class also supports sms/push
}

use Illuminate\Support\Facades\Notification;
use Deegitalbe\TrustupIoNotificationsContracts\Request\Recipient;

Notification::route('trustup-io-notifications',
    Recipient::anonymous(email: '[email protected]', phone: null, deviceTokens: [], locale: 'fr-BE')
)->notify(new CommentNotification($comment));
bash
php artisan vendor:publish --provider="Deegitalbe\TrustupIoNotificationsClient\TrustupIoNotificationsClientServiceProvider"