Page MenuHomePhabricator

[web] Handle notifications in service worker
ClosedPublic

Authored by michal on Feb 22 2023, 2:39 AM.
Tags
None
Referenced Files
Unknown Object (File)
Mon, Mar 11, 5:44 AM
Unknown Object (File)
Feb 21 2024, 1:53 PM
Unknown Object (File)
Feb 21 2024, 1:52 PM
Unknown Object (File)
Feb 21 2024, 1:52 PM
Unknown Object (File)
Feb 21 2024, 12:56 PM
Unknown Object (File)
Feb 21 2024, 12:56 PM
Unknown Object (File)
Feb 18 2024, 2:20 AM
Unknown Object (File)
Feb 18 2024, 1:22 AM
Subscribers

Details

Summary

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

Repository
rCOMM Comm
Lint
No Lint Coverage
Unit
No Test Coverage

Event Timeline

web/push-notif/service-worker.js
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.
web/push-notif/service-worker.js
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.