puter.notifications.dismiss()

Websites Puter Apps Node.js Workers

Marks one of your app's notifications as dismissed, such as when the user reads the message it's about. Every open page of your app hears about it through on('dismiss'), on any of the user's devices, and the notification closes there.

After you dismiss the newest notification in a group, the next send with that group creates a new notification instead of replacing it. To get that without closing anything, use markShown().

Closing it on other devices

On a website, the notification closes on each device where a page of your app has a live notification listener, meaning an onNotification(), onPush() or on('dismiss') listener. The page that calls dismiss() closes its own copy when the call resolves, even when it resolves false.

On a device with no such page open, the notification stays on screen until a page of your app starts one of those listeners. The page then checks up to 50 notifications on screen and closes the ones already dismissed. It does the same check when its connection comes back after a drop. These checks don't call your on('dismiss') handlers.

Puter doesn't send a push only to close a notification, since browsers show a notification for every push. For a push Puter shows, such as one for an app inside the Puter desktop, an open Puter desktop on that device closes it.

Dismissals reach other pages through Events, so a server without events doesn't deliver them.

Syntax

puter.notifications.dismiss(uid)

Parameters

uid (String) (required)

The notification's uid. For an immediate notification to your app's own user, notify() resolves with it. Notifications delivered to onNotification() and on('click') also carry it.

Return value

A Promise that resolves to true when the notification is dismissed. It resolves to false when it was already dismissed, doesn't exist, or isn't one of your app's notifications for this user.

It rejects with an error carrying message and code:

code Meaning
invalid_argument uid isn't a non-empty string.
too_many_requests Too many notification calls in a short time. Try again later.
auth_canceled Nobody was signed in, and the user closed the sign-in without finishing it.
notifications_failed The server sent a response the SDK couldn't read.

Examples

Dismiss a notification and hear about it

<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        (async () => {
            // (1) Every open page of the app hears about dismissals, on any device
            puter.notifications.on('dismiss', ({ uid }) => {
                puter.print(`dismissed ${uid}<br>`);
            });

            // (2) Send one, then dismiss it
            const uid = await puter.notifications.notify({ title: 'New message from Ana' });
            const dismissed = await puter.notifications.dismiss(uid);
            puter.print(`dismiss() resolved ${dismissed}<br>`);
        })();
    </script>
</body>
</html>

Dismiss a notification once the user opens it

puter.notifications.on('click', async ({ uid, target }) => {
    await puter.notifications.dismiss(uid);
    location.assign(target ?? '/');
});