Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best practices to dismiss web push notifications cross devices #359

Open
Maxou44 opened this issue May 19, 2023 · 1 comment
Open

Best practices to dismiss web push notifications cross devices #359

Maxou44 opened this issue May 19, 2023 · 1 comment

Comments

@Maxou44
Copy link

Maxou44 commented May 19, 2023

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
Copy link

RealAlphabet commented Jul 2, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants