mirror of
https://github.com/GoogleChrome/chrome-extensions-samples.git
synced 2026-03-26 13:19:49 +07:00
* Remove notification permission from push cookbook sample As of [M132] this requirement is no longer necessary (unless the extension chooses to show a notification). [M132]: https://chromiumdash.appspot.com/commit/ed68ac786e9a276ef2b627b7e21cf0d20cc6d798 * address review comments * Add note about minimum Chrome version --------- Co-authored-by: Oliver Dunk <oliverdunk@google.com>
Service worker with push notification
This sample demonstrates using the Push API with self.registration.pushManager.subscribe() and specifically how to use userVisibleOnly to silence required notifications when receiving a push message in a service worker based extension.
Overview
By calling a method in the sample and using an external push server website we can simulate an extension receiving a push message where it is not required to emit a notification by setting (userVisibleOnly = false) on registration.
Running this extension
Note: This sample requires Chrome 132+. Before Chrome 132, the same code works with the additional requirement of the notification extension permission.
- Clone this repository.
- Go to the web push test server and copy the “Public Key” to the
APPLICATION_SERVER_PUBLIC_KEYvariable in background.js. - Load this directory in Chrome as an unpacked extension.
- Click “service worker (Inactive)” on the extension to load DevTools for background.js
- In DevTools call:
await subscribeUserVisibleOnlyFalse(); - Copy the output after “Subscription data to be sent to the push notification server:” and paste it into the web push test server inside “Subscription to Send To” text box
- Enter some text into “Text to Send”
- Click “SEND PUSH MESSAGE”
- Observe that there is no notification with the text you sent in the browser and no generic notification notification being shown by the browser (that is usually enforced). You’ll see the text you sent in the DevTools log proving the extension service worker received the push data.