
For a while, I was using a service that sent notifications via WhatsApp whenever an offer or message came in from my site. One day it silently stopped working. When I noticed a request that came in during a night shift hours later, I made a decision: notifications should come directly to my phone without depending on a third-party service.
The solution was Web Push, which browsers already support. Once from Chrome on my phone I say "allow notifications" to my admin panel, new offers, appointments, and messages drop as notifications—even when the site is closed. No SMS fees, no intermediary apps.
How does Web Push work?
There are three parties: your server, the browser, and the browser's notification service (Google's server for Chrome, Mozilla's for Firefox).
- Subscription: When the user grants permission, the browser gives you an address (endpoint) and two keys. You save these in your database.
- Sending: When you want to send a notification, you encrypt the message with those keys and send it to the notification service's address.
- Delivery: The service delivers the message to the device, and a small script (service worker) in the browser decrypts it and displays the notification.
The hard part is the second step. The message must be encrypted according to the RFC 8291 standard, and your server's identity must be proven with a signed identity called VAPID. If a single byte is wrong, the service silently rejects the message.
Why I didn't use a ready-made library
The common library for this in PHP is solid, but it brings along nine separate packages—JWT, HTTP client, base64, and more—and requires Composer. My site is intentionally kept simple: a PHP + SQLite system running on shared hosting. I didn't want to build a huge dependency chain for a single feature.
I realized: everything needed is already in PHP's own OpenSSL functions. openssl_pkey_derive for elliptic curve key agreement, hash_hkdf for key derivation, aes-128-gcm for encryption, openssl_sign for signing. All that was left was to carefully read the standard and assemble the pieces in the right order.
Result: one file, roughly 200 lines
The code that emerged is a single PHP file. It only uses PHP's standard openssl and curl extensions; these are open on almost every hosting. Here's how to use it:
require 'WebPush.php';
use AsilTechSoft\WebPush\WebPush;
// Generate keys once and store them
$keys = WebPush::generateVapidKeys();
$push = new WebPush($publicKey, $privatePem, 'mailto:you@example.com');
$result = $push->send($subscription, json_encode([
'title' => 'New offer',
'body' => 'An offer request came in for your website.',
'url' => '/admin/messages.php',
]));
if ($result->expired()) {
// User unsubscribed, delete the record
}
To ensure correctness, I tested the code with an independent decryption library: 50 notifications with different content all decrypted correctly, and 50 signatures all validated.
Important considerations
- HTTPS is required. Browsers only allow notifications on secure sites.
- On iPhone, you need to open the site from Safari using "Add to Home Screen" to use it like an app (iOS 16.4 and later).
- Messages should be small: around 4 KB maximum. A title, brief text, and a link are enough.
- Clean up dead subscriptions: If the service returns 404 or 410, that device is no longer receiving notifications; delete the record.
I shared the code as open source
I released this code that works for my needs under the MIT license so others can use it too. It includes a sample service worker and subscription page:
- GitHub: github.com/asiltechsoft/webpush-lite-php
- To install with Composer:
composer require asiltechsoft/webpush-lite-php
If you want to set up a notification system for your site or app, or if you're stuck on something similar, you can reach out to me.
ASILTECHSOFT