Q:

Is it possible to do push notifications with Gatbsy?

I am intrigued by Gatsby and my initial experiences with it have been very positive.
It's unclear how the static CDN-hosted model would dovetail with push notification functionality, and I would be appreciative of any guidance. Searching the web was to no avail.

A:

Gatsby assumes a "decoupled" architecture. Gatsby wants to handle your frontend and the build process but how/where you store your data is up to you. So push notifications with Gatsby would be handled by a different service. You'd just need to add React code which handles the pushed data and presents it.

A:

I managed to add push notifications, following the Mozilla guide: https://developer.mozilla.org/es/docs/Web/API/ServiceWorkerRegistration/showNotification#Examples
In your gatsby-browser.js file, you can use onServiceWorkerUpdateFound to listen to updates and trigger a push notification, see code below
export const onServiceWorkerUpdateFound = () => {
  const showNotification = () => {
    Notification.requestPermission(result => {
        if (result === 'granted') {
            navigator.serviceWorker.ready.then(registration => {
                registration.showNotification('Update', {
                    body: 'New content is available!',
                    icon: 'link-to-your-icon',
                    vibrate: [200, 100, 200, 100, 200, 100, 400],
                    tag: 'request',
                    actions: [ // you can customize these actions as you like
                        {
                            action: doSomething(), // you should define this
                            title: 'update'
                        },
                        {
                            action: doSomethingElse(), // you should define this
                            title: 'ignore'
                        }
                    ]
                })
            })
        }
    })
  }

  showNotification()
}

