w3c / push-api

Push API
https://w3c.github.io/push-api/
Other
144 stars 40 forks source link

Best practices to dismiss web push notifications cross devices #359

Open Maxou44 opened 1 year ago

Maxou44 commented 1 year ago

Hello,

I'm working on a web application that allows receiving push notifications on different devices. I would like to ensure that when a user reads a notification on one device, it gets removed from the others.

I wanted to use subscribe with userVisibleOnly: false, but it seems that Chrome doesn't currently support it...

It seems that if no notification is sent for a "push" event at the service worker level, the browser may display a warning notification to the user indicating that it's working in the background.

I was thinking of including the IDs of my unread notifications in each push and removing any notifications that don't match before adding the new one. However, I'm still facing an issue if I have only one notification and it becomes read.

What are the current best practices to achieve this?

Have a good day. Maxime

RealAlphabet commented 2 weeks ago

According to the section 5.4 of the WebPush Protocol.
There is only one way to replace push messages, but not to delete them.

Given that browsers require a notification to be displayed when the push event handler is called, I don't think there's any standard way of overcoming this problem.

A feature request would have to be made on the Web Push protocol standard to propose a mechanism for deleting messages in addition to replacement. If replacement is standardised, I don't see what would prevent the addition of a means of deleting the message from the queue.

UPDATE: In practical terms, you cannot expect to use Web Push to deliver a "dismiss notifications" message, as receiving a message must result in a notification being displayed. You also can't expect messages to be delivered in the right order, even if the priority of two notifications is the same.

I think suppressing notifications across devices is an anti-pattern. If two subscribed devices are currently reachable, they will receive the notification and will need to display the notification as this is required by most browsers.

A solution to mitigate the receipt of old notifications would be to set a low TTL and validate the receipt of notifications with your backend in the service worker push event. As long as the notification has not been acknowledged by at least one device, you can try to resend it. This approach allows you to dismiss notifications from devices that have been offline for a long time, which avoids flooding them with obsolete messages.