从 Service Worker 发送通知

发布于 2025-01-14 04:39:53 字数 586 浏览 4 评论 0原文

我有一个 PWA 应用程序,基本上是一个简单的形式。如果网络离线,则保存到本地存储(indexedDB)并在连接在线时发送。到目前为止,一切都很好,但我想向用户显示一条消息“表单已发送到服务器”,但我无法让它工作。我使用属性“SYNC”来执行此功能。在我的服务工作者中,与此功能相关的代码部分是:

self.addEventListener('sync', e => {
    console.log('SW: Sync');
    if ( e.tag === 'new-form' ) {

        // send to DB when onLine
        const resp = sendForm(); //up to here all is OK.

        self.registration.showNotification(' Succesfully send to MongoDB');// this line is not working

        e.waitUntil( resp);
    }
}); 

可能出了什么问题?在javascript方面,已验证Notification.permission ===“已授予”。

I have a pwa app that basically is a simple form. If the network is offline, it is saved to local storage(indexedDB) and send afterwards when the connection is online. Up to here everything is ok, but I would like to show a message to the user 'the form was sent to the server' and I can't get it working. I use the property 'SYNC' for doing this feature. In my service worker the part of the code relative to this feature is:

self.addEventListener('sync', e => {
    console.log('SW: Sync');
    if ( e.tag === 'new-form' ) {

        // send to DB when onLine
        const resp = sendForm(); //up to here all is OK.

        self.registration.showNotification(' Succesfully send to MongoDB');// this line is not working

        e.waitUntil( resp);
    }
}); 

What could be wrong?, in javascript side, It is verified that Notification.permission === 'granted'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文