Docs / Core / Notifications
ven.js
Notifications (Frontend)
Register a service worker, request push permission, and display local notifications.
Backend counterpart: The PHP side (
ven_notify.php, sw.js) is documented under Backend → Notification Server.notification.registerServiceWorker(swPath)
Registers the service worker (defaults to /sw.js). Must be served over HTTP(S) and the file must live at the site root.
await venjs.notification.registerServiceWorker("/sw.js");
notification.ask(phpUrl)
Requests the browser's notification permission. On "granted", it generates a device id and (optionally) POSTs it to phpUrl so your server can target this device later.
const deviceId = await venjs.notification.ask("/ven_notify.php");
Returns the deviceId string, or false if permission was denied or notifications are unsupported.
const id = await venjs.notification.ask("/ven_notify.php");
if (id) console.log("device registered:", id);
notification.push(options)
Shows a local notification immediately (requires permission).
| Option | Description |
|---|---|
Content | Body text of the notification. |
icon | Icon URL (defaults to ""). |
url | URL opened when the notification is clicked. |
venjs.notification.push({
Content: "New message from Ada",
icon: "/icon/logo.png",
url: "/messages/42"
});
Full flow
// logic/notifications.js
export const initNotifications = async () => {
await venjs.notification.registerServiceWorker("/sw.js");
const deviceId = await venjs.notification.ask("/ven_notify.php");
if (deviceId) {
venjs.notification.push({
Content: "VenJS notifications are ready!",
url: location.href
});
}
};
Permissions:
ask() must be called from a user gesture (e.g. a button click) in most browsers, otherwise the permission prompt may be blocked.