Skip to content

Push Channel ​

The push channel delivers browser notifications (WebPush) and mobile push notifications via Firebase Cloud Messaging (FCM v1).


The PushMessage Builder ​

Define a toPush() method returning a Jengo\Notifications\Messages\PushMessage:

php
namespace App\Notifications;

use Jengo\Notifications\Messages\PushMessage;
use Jengo\Notifications\Notification;

class NewMessageReceived extends Notification
{
    public function __construct(public array $chatMessage)
    {
        parent::__construct();
    }

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

    public function toPush(object $notifiable): PushMessage
    {
        return (new PushMessage())
            ->title("Message from {$this->chatMessage['sender']}")
            ->body($this->chatMessage['text'])
            ->icon('/icons/chat-icon-192.png')
            ->badge('/icons/badge-72.png')
            ->image($this->chatMessage['preview_image'] ?? null)
            ->link(site_url("chat/{$this->chatMessage['thread_id']}"))
            ->sound('default')
            ->data([
                'thread_id' => $this->chatMessage['thread_id'],
            ]);
    }
}

Supported Drivers ​

In app/Config/Notifications.php:

php
public array $push = [
    'driver' => 'fcm', // 'fcm', 'webpush', 'log', 'null'
    // ...
];

1. Firebase Cloud Messaging (FCM v1) ​

Delivers to Android, iOS, and Web clients via Google's modern FCM v1 HTTP API:

php
public array $push = [
    'driver' => 'fcm',
    'fcm'    => [
        'projectId'          => 'your-firebase-project-id',
        'serviceAccountJson' => WRITEPATH . 'keys/firebase-service-account.json',
        'accessToken'        => null, // Optional pre-fetched bearer token
    ],
];

On your recipient entity, implement routeNotificationForPush():

php
public function routeNotificationForPush(): ?string
{
    return $this->fcm_device_token;
}

2. WebPush (Standard Browser Push) ​

Direct push notification delivery to Chrome, Firefox, Safari, and Edge via VAPID authentication:

php
public array $push = [
    'driver'  => 'webpush',
    'webpush' => [
        'publicKey'  => 'BLB...your-vapid-public-key...',
        'privateKey' => 'K3X...your-vapid-private-key...',
        'subject'    => 'mailto:admin@example.com',
    ],
];

The recipient's routeNotificationForPush() should return the browser's subscription JSON string or array containing endpoint and keys (p256dh, auth).

Released under the MIT License.