PHP code example of glueful / notiva

1. Go to this page and download the library: Download glueful/notiva 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/ */

    

glueful / notiva example snippets



use Glueful\Notifications\Services\ChannelManager;
use App\Models\User; // implements Notifiable and routeNotificationFor('push')

// Resolve the push channel
$channels = container()->get(ChannelManager::class);
$push = $channels->getChannel('push');

// Target notifiable
$user = User::findByUuid('u_ABC123');

// Build payload (common + per-channel options as needed)
$payload = [
    'title' => 'Hello',
    'body'  => 'World',
    'data'  => ['foo' => 'bar'],

    // Android (FCM)
    'android_priority' => 'HIGH',
    'click_action'     => 'OPEN_APP',

    // iOS (APNs)
    'apns_push_type' => 'alert',
    'apns_priority'  => 10,

    // Web Push (Browser)
    'actions' => [
        ['action' => 'open', 'title' => 'Open']
    ],
];

// Send
$ok = $push->send($user, $payload);


use Glueful\Notifications\Contracts\Notifiable;

class User implements Notifiable
{
    public function getNotifiableId(): string { return $this->uuid; }

    public function shouldReceiveNotification(string $type, string $channel): bool { return true; }

    public function getNotificationPreferences(): array { return []; }

    public function routeNotificationFor(string $channel)
    {
        if ($channel !== 'push') return null;
        return [
            'fcm' => ['fcm-token-1'],
            'apns' => ['apns-token-1'],
            // 'webpush' => [[ 'endpoint' => '...', 'keys' => ['p256dh' => '...', 'auth' => '...'] ]]
        ];
    }
}


use Glueful\\Notifications\\Services\\NotificationDispatcher;
use Glueful\\Notifications\\Models\\Notification;
use App\\Models\\User; // your Notifiable implementation

$dispatcher = container()->get(NotificationDispatcher::class);

// Build a Notification model; type is arbitrary for your domain
$notification = new Notification([
    'type' => 'system_alert',
    'data' => [
        'title' => 'Hello',
        'body'  => 'World',
        'data'  => ['foo' => 'bar'],
        // optional per-channel fields supported as in the direct example
    ],
]);

$user = User::findByUuid('u_ABC123');

// Restrict to the push channel explicitly
$result = $dispatcher->send($notification, $user, ['push']);
bash
php glueful extensions:list
php glueful extensions:info notiva
php glueful extensions:diagnose
bash
php glueful migrate run

return [
  'webpush' => [
    [
      'endpoint' => 'https://fcm.googleapis.com/fcm/send/XXX',
      'keys' => [
        'p256dh' => 'BASE64_PUBLIC_KEY',
        'auth' => 'BASE64_AUTH'
      ]
    ]
  ]
];