You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Service Worker无法触发可见通知问题求助

Service Worker无法触发可见通知问题求助

大家好,我正在测试一个带推送通知的新集成,目前控制台的日志都能正常输出,但最后浏览器就是不显示通知,实在摸不着头绪,来向各位求助!

具体情况是这样的:我已经配置了VAPID密钥,后端用NodeJS+Express搭建,REST POST接口能正确完成订阅、存储和获取订阅信息的流程,也能把推送消息发送给客户端。Service Worker的install和activate事件都能正常触发,push事件里的日志(包括"server contacted me for notification")也都打印出来了,但就是看不到实际的浏览器通知。

这是我的Service Worker代码:

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);
};

有没有大佬能帮我看看哪里出问题了呀?

备注:内容来源于stack exchange,提问作者miko25

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 15:38:02