我正在测试与推送通知的新集成,并且我得到了正确的console.logs,但最终没有显示浏览器通知。
这是我的服务人员:
self.addEventListener("install", (event) => {
console.log("Service Worker installing");
// Skip waiting to immediately activate the service worker
self.skipWaiting();
});
self.addEventListener("activate", (event) => {
console.log("Service Worker activated");
});
self.addEventListener("push", function (event) {
console.log("here");
if (event.data) {
console.log("Push event!! ", JSON.parse(event.data.text()));
showLocalNotification(
"Notification ",
JSON.parse(event.data.text()),
self.registration
);
} else {
console.log("Push event but no data");
}
});
const showLocalNotification = (title, data, swRegistration) => {
console.log("server contacted me for notification");
swRegistration.showNotification(title, data);
};
我有乏味的密钥,我将 NodeJS 与 Express 一起使用,并且 REST POST 端点正确订阅、存储和检索订阅以供使用,并将其发送到客户端。所以我正确安装、激活但没有实际显示通知。它记录“服务器联系我以获取通知”
对于那些像我一样愚蠢的人来说,我今天实际上关闭了 Chrome 的通知。没关系,一切正常。