javascript - 通过 Service Worker 收到的 Chrome 通知是批量的还是实时的?

标签 javascript google-chrome push-notification notifications service-worker

我正在尝试为我的网站实现浏览器推送通知。我注意到即使浏览器收到通知,有时也不会显示通知。

var showNotification = function (event, data) {
    var notificationData = data['data'];
    var title = notificationData['title'];
    var body = notificationData['body'];
    var icon = notificationData['icon'];
    var notificationActionsData = notificationData["actions"];
    var actions = [];

    if(notificationActionsData) {
        for(var i=0; i < notificationActionsData.length; i++) {
            var action = {
                action: "action" + i,
                title: notificationActionsData[i].title,
            };
            actions.push(action);
        }
    }

    var campaignId = notificationData["id"];
    self.registration.showNotification(title, {
        body: body,
        icon: icon,
        data: notificationData,
        tag: notificationData.id,
        actions: actions
    });

    pushowlReporting.tickle(campaignId, "delivery");
};

function processNotification(event) {
    if (event.data) {
        var data = event.data.json();
        showNotification(event, data);
    }
    else {
        fetch("https://" + hostname + "/api/v1/subdomain/" + subdomain + "/campaign/", {'mode': 'no-cors'}).then(
            function (response) {
                if (response.status !== 200) {
                    console.log('Looks like there was a problem. Status Code: ', response.status);
                    return;
                }

                // Examine the text in the response
                response.text().then(function (responseText) {
                    var data = JSON.parse(responseText);
                    showNotification(event, data);
                });
            }
        ).catch(function (err) {
                console.log('Fetch Error :', err);
            }
        );
    }
}

self.addEventListener('push', function (event) {
    event.waitUntil(processNotification(event));
});

我的报告 API 显示通知已送达,但浏览器间歇性地显示通知。

通知显示非常不稳定。有时通知会立即显示,有时则一段时间不显示,突然所有过去的通知都批量出现。有时某些通知根本不会显示。

如果我在这里做错了什么,请告诉我?

最佳答案

传递给 event.waituntil 的函数应该返回一个 promise 。如果不是,范围就会困惑,因为事件的生命周期不会延长。 https://developer.mozilla.org/en-US/docs/Web/API/ExtendableEvent/waitUntil

var showNotification = function (event, data) {
    var notificationData = data['data'];
    var title = notificationData['title'];
    var body = notificationData['body'];
    var icon = notificationData['icon'];
    var notificationActionsData = notificationData["actions"];
    var actions = [];

    if(notificationActionsData) {
        for(var i=0; i < notificationActionsData.length; i++) {
            var action = {
                action: "action" + i,
                title: notificationActionsData[i].title,
            };
            actions.push(action);
        }
    }

    var campaignId = notificationData["id"];
    return self.registration.showNotification(title, {
        body: body,
        icon: icon,
        data: notificationData,
        tag: notificationData.id,
        actions: actions
    }).then(function (succ) {
        pushowlReporting.tickle(campaignId, "delivery");
    });
};
function processNotification(event) {
    if (event.data) {
        var data = event.data.json();
        return showNotification(event, data);
    }
    else {
        return fetch("https://" + hostname + "/api/v1/subdomain/" + subdomain + "/campaign/", {'mode': 'no-cors'}).then(
            function (response) {
                if (response.status !== 200) {
                    console.log('Looks like there was a problem. Status Code: ', response.status);
                    return;
                }

                // Examine the text in the response
                response.text().then(function (responseText) {
                    var data = JSON.parse(responseText);
                    showNotification(event, data);
                });
            }
        ).catch(function (err) {
                console.log('Fetch Error :', err);
            }
        );
    }
}

我刚刚在您的代码中添加了 return 语句。试试这个。

关于javascript - 通过 Service Worker 收到的 Chrome 通知是批量的还是实时的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39572255/

相关文章:

javascript - 为什么我的 Tampermonkey 脚本会抛出 "Selenium is not defined"?

android - 点击通知未启动应用程序

iphone - 当收到 RemoteNotification 时跳转到其他页面并取消位置

javascript - 打开自定义协议(protocol)后关闭浏览器窗口

javascript - 在 Javascript 的 if 语句中更改变量的值

javascript - InsertBefore 对表中的行进行排序

php - Laravel 队列反序列化问题

javascript - Socket.IO 和 IE8 - jsonp 轮询连接总是失败

javascript - 如何从 chrome 扩展程序调用网页上的 javascript 函数?

google-chrome - 修改自托管Owin "require SSL"