GoogleChrome / GoogleChrome/chrome-extensions-samples
Complete web push example with non-interactive subscription
- Dominant language
- JavaScript
- Stars
- 17.8k
- Forks
- 9k
- Avg merge
- 21h
- Merged PRs (30d)
- 28
Description
Currently, the cookbook.push example only features manually subscribing to the pushmanager in the service worker's devtools. Obviously this is not a real solution for live extensions. Getting the subscription created requires this additional code:
```js
addEventListener('activate', (e) => {
e.waitUntil(subscribeUserVisibleOnlyFalse());
})
```
This should be noted somewhere in the documentation or the example, and currently there's no information about this in either source or any other source I could find. The [extension docs](https://developer.chrome.com/docs/extensions/how-to/integrate/web-push) imply that you can use a top-level await to block until the subscription is created:
```js
const SERVER_PUBLIC_KEY = '_INSERT_VALUE_HERE_';
const applicationServerKey = urlB64ToUint8Array(SERVER_PUBLIC_KEY);
async function subscribe() {
try {
let subscription = await self.registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey
});
console.log(`Subscribed: ${JSON.stringify(subscription,0,2)}`);
return subscription
} catch (error) {
console.error('Subscribe error: ', error);
}
}
const subscription = await subscribe();
```
But of course top-level await isn't allowed in BSWs.
Contributor guide
Research direction
Start with the cookbook.push example and its accompanying documentation, then trace how the service worker currently creates its push subscription. Document or demonstrate the activate-time subscription flow, and make clear that top-level await is not available in background service workers; done means a live extension can create the subscription without DevTools interaction.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- documentation, web-dev
- Issue type
- Documentation
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100