The only snippet you will need to deal with push notifications in a service worker

A Web Developer based in Belgium, specialized in PHP & JS development
Search for a command to run...

A Web Developer based in Belgium, specialized in PHP & JS development
Great post! But how would you integrate this into your Workbox setup? I'm having troubles since I'm using its auto-generated sw.js file based on my workbox config, are you adding the push code there too or somewhere else..?
I recently needed to collect all changes in one file for reporting purposes. Found this article: https://ratfactor.com/cards/git-diff-with-new-files In case you need something similar on Windows, here

Some projects start with a grand plan. Others start because you just need to read a file without thinking too much about it. That was the case for spread-compat. I needed a common interface for CSV, X

With modern css, you can almost get rid of sass. But there is one last pain point : media queries. Not being able to use variables or an easy, common syntax for typical breakpoints is really annoying. That is, until you meet Media Queries Level 5. I’...

This is a quick article to share this demo I just made. https://codepen.io/lekoalabe/pen/JoPNWpX

Modals are typical UI elements for many web apps. There are countless packages dedicated to that specific features, each framework creating its own variation. Popular frameworks like Bootstrap also have custom code to deal with it. But all this feels...

I'm working on a PWA at the moment. What a great time it is to be able to develop near native looking mobile applications without having to deal with flutter, native code, etc.
One part in particular is great: push notifications. Now that Safari 16 has added support for them, it's not realistic to actually use this in production.
I know, i know... let's get to the point of this article: the snippet.
// @link https://flaviocopes.com/push-api/
// @link https://web.dev/push-notifications-handling-messages/
self.addEventListener('push', function (event) {
if (!event.data) {
console.log('This push event has no data.');
return;
}
if (!self.registration) {
console.log('Service worker does not control the page');
return;
}
if (!self.registration || !self.registration.pushManager) {
console.log('Push is not supported');
return;
}
const eventText = event.data.text();
// Specify default options
let options = {};
let title = '';
// Support both plain text notification and json
if (eventText.substr(0, 1) === '{') {
const eventData = JSON.parse(eventText);
title = eventData.title;
// Set specific options
// @link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/showNotification#parameters
if (eventData.options) {
options = Object.assign(options, eventData.options);
}
// Check expiration if specified
if (eventData.expires && Date.now() > eventData.expires) {
console.log('Push notification has expired');
return;
}
} else {
title = eventText;
}
// Warning: this can fail silently if notifications are disabled at system level
// The promise itself resolve to undefined and is not helpful to see if it has been displayed properly
const promiseChain = self.registration.showNotification(title, options);
// With this, the browser will keep the service worker running until the promise you passed in has settled.
event.waitUntil(promiseChain);
});
So what does this do ?
Happy coding :-)