gpt4 book ai didi

javascript - 是否可以使用 Gatsby 进行推送通知?

转载 作者:搜寻专家 更新时间:2023-11-01 05:09:18 25 4
gpt4 key购买 nike

我对 Gatsby 很感兴趣,我对它的初步体验非常积极。

尚不清楚静态 CDN 托管模型如何与推送通知功能相吻合,我将不胜感激任何指导。在网上搜索无果。

最佳答案

我按照 Mozilla 指南成功添加了推送通知:https://developer.mozilla.org/es/docs/Web/API/ServiceWorkerRegistration/showNotification#Examples

在你的 gatsby-browser.js 文件中,你可以使用 onServiceWorkerUpdateFound 来监听更新并触发推送通知,见下面的代码

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()
}

关于javascript - 是否可以使用 Gatsby 进行推送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47971439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com