And*_*man 6 javascript push-notification web web-notifications service-worker
我注册了一个服务人员,并尝试在浏览器中测试网络通知。Chrome(和 Firefox)声称 Service Worker 已成功注册。
在加载 React 应用程序时,我已授予接收通知的权限。
在我的 中sw.js,我正在侦听一个push事件,并尝试从 Chrome 应用程序选项卡发送示例推送消息,如上面的屏幕截图所示。
self.addEventListener("push", receivePushNotification);
Run Code Online (Sandbox Code Playgroud)
Push单击Chrome 应用程序选项卡时,push会触发该事件,并调用receivePushNotification. 但是,当我尝试在浏览器中显示通知时,没有任何反应,也没有报告错误。
function receivePushNotification(event) {
// This prints "Test push message from DevTools."
console.log("[Service Worker] Push Received.", event.data.text());
var options = {
body: "This notification was generated from a push!"
};
/*****
I would expect the following line to display a notification, since I've already
granted permission to allow for notifications. Yet, nothing happens and there is
no error in the console.
*****/
event.waitUntil(self.registration.showNotification("Hello world!", options));
}
Run Code Online (Sandbox Code Playgroud)
mik*_*nik -1
您似乎在单独定义函数/不将其定义为异步函数方面走得太远了。您也没有将事件传递给函数调用。
如果你这样重写会发生什么?
self.addEventListener("push", function(event) {
console.log("[Service Worker] Push Received.", event.data.text());
var options = {
body: "This notification was generated from a push!"
};
event.waitUntil(self.registration.showNotification("Hello world!", options));
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6697 次 |
| 最近记录: |