Page MenuHomePhabricator

[web] Handle notifications in service worker

Authored by michal on Feb 22 2023, 2:39 AM.
Referenced Files
Unknown Object (File)
Tue, Apr 2, 6:48 PM
Unknown Object (File)
Sun, Mar 31, 8:14 AM
Unknown Object (File)
Sun, Mar 31, 8:14 AM
Unknown Object (File)
Sun, Mar 31, 8:14 AM
Unknown Object (File)
Sun, Mar 31, 8:13 AM
Unknown Object (File)
Sun, Mar 31, 8:06 AM
Unknown Object (File)
Mar 11 2024, 5:44 AM
Unknown Object (File)
Feb 21 2024, 1:53 PM



This diff modifies the service worker to actually handle the push event and display a notification, additionaly it modifies the other events:

  • 'install' -> adds a skipWaiting so it will immediately take over the previous service worker
  • 'activate' -> clients.claim() so it will claim tabs that aren't controlled by a service worker (while this tab wouldn't have a event listener and wouldn't be able to navigate to a chat when user click a notification, we will still be able to focus on it)

event.waitUntil is needed because otherwise the browser could put the service worker to sleep before the promises are resolved.

Test Plan

Rest if the notification shows up after getting a push event

Diff Detail

rCOMM Comm
Lint Not Applicable
Tests Not Applicable

Event Timeline

3–8 ↗(On Diff #22923)

Unfortunately, these types don't exist in flow so I had to type them myself based on MDN.

10–11 ↗(On Diff #22923)

While these exist in flow, eslint complains that they aren't defined. Instead of this, we could add them in .eslintrc in globals but then they would be available in the whole web which isn't great.

tomek added inline comments.
4 ↗(On Diff #22923)

If this is specific to push notifications, maybe we can have a more concrete type (with body, prefix, etc.)? Otherwise we should keep Object here and cast a result of calling json() immediately in push event handler.

22 ↗(On Diff #22923)

Can we cast data so that all the fields have types assigned?

This revision is now accepted and ready to land.Feb 28 2023, 3:04 AM

Type the json from the web push API. The PushMessageData is taken from the official web api types so I think it's better to keep them in sync and only later type them as our own type.