Push Notifications on mobile

I might need a bit of help here.

I want to add support for Push Notifications for mobile devices, so that you can get a bleep on your phone if someone tries to message you in game, or something else that you wish to know. We have local notifications, but they mainly just work on desktop.

I want to add notifications using push services.

I know the gist of how it is done, reading articles like:

But. One thing I still can’t answer is;

How do I prevent notifications when you have the PWA app open?

  1. Does the browser ignore push by itself, if the app is in focus?
  2. If not 1) , then do I need to tell the server every time you focus/unfocus the app, so I know only to send notifications when not focused?

And also - anyone else here with experience making using push services, in case I end up with questions?

Based on the linked documentation you don’t need to take it back to the server in either case. You’d get a push event on the PWA’s service worker, and in that event handler you can check local state to see if the tab is focused or not, I think. Though I’m not sure if that catches all corner cases like backgrounding the app or going into lockscreen.

But in that push-handler, you should be able to do all kinds of logic to decide whether or not to actually show the notification to the user. (Or even when to show the notification.)

Yes, that seems to be correct.
The issue I saw was that Chrome (and other browsers?) requires you to show a notification on a push event, to avoid silent pushs. But if I understood it correctly, this does not apply if the app is in focus already.
So, I will not inform the server of focus state, but let the client silently discard push events if already in focus.

But I am currently battling with another issue; multi-account users.

Since PushSubscription are per device, I will most likely end up just having subscriptions for one account/user at a time. If anyone has a good solution how to solve this issue, I am happy to hear.

And another issue.
While notification being turned on/off is device specific, the preference is not. So the following settings:
image

are per account, and not per device. This is how it is currently.
I think I will also keep it that way for now, as it simplifies my own implementation of the feature.