The Notifications API lets your app send notifications to its users, such as a new message, a finished export or a reminder. Every open page of your app gets each notification right away. When the user isn't looking at your app, it also goes out as a push notification to their phone or computer, and a tap opens your app.
await puter.notifications.notify({
title: 'New message from Ana',
text: 'See you at 6?',
target: '/chats/42',
});
A notification goes to your app, and the Puter desktop's notifications panel doesn't show it. It reaches the user in two ways:
onNotification() or onPush(). A page can also read back the ones sent while it was closed.on('click') handler.The user turns pushes on once in each browser, from a click. On a website, pushes arrive on your own domain through a one-line service worker, and enable() turns them on. Inside the Puter desktop, pushes go through Puter, and enable() asks the user to allow your app. Push notifications for your app has the setup and the browsers and devices that can receive pushes.
A notification has a title, and optionally text and an icon. A target path inside your app, such as /chats/42, and a small data value come back to your app when the user clicks it. A group replaces your earlier notification in the same group instead of adding another one, and sensitive: true keeps the title and text out of pushes.
notify() lists every field. Rate Limits and Quotas lists the size limits and how many notifications and pushes your app can send each user.
To send a notification at a set time instead of now, pass sendAt to notify(). It goes out then, even when your app is closed.
When the user reads a notification in your app, dismiss() closes it on their other devices too.
A notification goes to the user your app runs for. To notify another user, such as a chat member, share data with them in a key-value share made with notify: true, and send through the share's handle. Each side of the share can notify the other.
You can send from a page, from a Puter Worker, or from an events handler that runs while your app is closed.
Your app's notifications are events on the notif:app-user subject. onNotification() catches up on them and listens for new ones for you. To page through them yourself, use puter.events.fetch().
<html>
<body>
<script src="https://js.puter.com/v2/"></script>
<script>
(async () => {
// (1) Listen for this app's notifications while it's open
const listener = await puter.notifications.onNotification((n) => {
puter.print(`Received: ${n.title}<br>`);
}, { catchUp: false });
// (2) Send one. This page is open, so it arrives here live.
await puter.notifications.notify({
title: 'Your export is ready',
text: 'report.pdf finished exporting.',
target: '/exports/report',
data: { exportId: 'exp_123' },
});
setTimeout(() => listener.off(), 5000);
})();
</script>
</body>
</html>
<html>
<body>
<script src="https://js.puter.com/v2/"></script>
<script>
// Also gets the click that opened the app.
puter.notifications.on('click', ({ uid, target, data }) => {
puter.print(`Clicked ${uid}, open ${target}<br>`);
});
</script>
</body>
</html>
<html>
<body>
<script src="https://js.puter.com/v2/"></script>
<button id="enable">Turn on notifications</button>
<script>
// Needs /puter-sw.js on your site. See Push notifications for your app.
puter.notifications.onPush('chat', (n) => ({
title: n.title,
body: n.text ?? '',
url: n.target ?? '/',
}));
document.getElementById('enable').addEventListener('click', async () => {
try {
const on = await puter.notifications.enable();
puter.print(on ? 'Notifications are on<br>' : 'Notifications are off<br>');
} catch (error) {
puter.print(`Could not turn on notifications: ${error.code}<br>`);
}
});
</script>
</body>
</html>
puter.notifications.notify() - Send a notification to the user, or to the other side of a key-value shareputer.notifications.onNotification() - Receive your app's notifications, including the ones sent while it was closedputer.notifications.on() - React when one of your notifications is clicked or dismissedputer.notifications.dismiss() - Dismiss a notification and close it on the user's other devicesputer.notifications.markShown() - Mark a notification as shown, so the next one in its group is newputer.notifications.cancel() - Cancel a notification stored to send laterputer.notifications.enable() - Turn on pushes for your app in this browserputer.notifications.onPush() - Say what a push shows for one channel, and handle its notifications while the page is openputer.notifications.status() - Check whether pushes reach this browser, without askingputer.notifications.disable() - Turn off pushes for your appputer.notifications.requestPermission() - Ask the user to allow pushes from your appputer.notifications.hasPermission() - Check whether the user allows pushes from your app, without askingputer.notifications.enablePush() - Turn on pushes through Puter in this browserFor Puter's own apps, which manage where pushes go:
puter.notifications.listDevices() - List the browsers that receive the user's pushesputer.notifications.removeDevice() - Stop pushes to one of them