【问题标题】:Firestore realtime listeners when your web app is in the background当您的 Web 应用程序在后台时,Firestore 实时侦听器
【发布时间】:2018-07-31 19:29:31
【问题描述】:

以下代码将监听器添加到 Firestore 集合,并在 Web 应用程序处于前台时发送推送通知。是否可以通过服务人员在后台执行相同的操作? 我尝试将 firebase 模块导入 service worker,但没有成功

db.collection("favorites").onSnapshot(function(snapshot) {
        var response =snapshot.docChanges();
        response.forEach(function(change) {
                if (change.type === "added") {
                    var bodyPaint="New favorites: ";
                }
                if (change.type === "modified") {
                    var bodyPaint="Modified favorites: "; 
                }
                if (change.type === "removed") {
                    var bodyPaint="Removed favorites: ";
                }

            navigator.serviceWorker.ready.then(function(registration) {
                const title = 'LA BUENOS AIRES';
                const options = {
                  body: bodyPaint,
                  icon: 'images/icon.png',
                  badge: 'images/badge.png'
                };
                registration.showNotification(title, options);
              });
        });
});

【问题讨论】:

    标签: javascript firebase google-cloud-firestore service-worker progressive-web-apps


    【解决方案1】:

    Service Worker 无法像这样维护与服务器的持久连接。

    您可以运行Cloud Function trigger 来监听更改,当需要通知用户时,您可以发送push notification

    【讨论】:

    • 感谢亚伯拉罕的链接,我想象着火力基地功能
    猜你喜欢
    • 2017-05-31
    • 2020-12-09
    • 2018-08-08
    • 2021-01-19
    • 2021-07-14
    • 2017-06-27
    • 1970-01-01
    • 2020-11-11
    相关资源
    最近更新 更多