Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 837
capouch Avatar asked Dec 06 '22 12:12

capouch


1 Answers

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()
}
like image 153
Smakosh Avatar answered Dec 22 '22 05:12

Smakosh