首页 > 解决方案 > 未捕获的 TypeError:self.showLocalNotificationWrap 不是函数

问题描述

所以我正在尝试使用网络推送发送通知,但它不起作用。我看到该事件已收到,但未显示任何通知。

这是我的服务人员中“推送”事件的代码:

self.addEventListener('push', async (event) => {
    console.log("Push received data - ", event.data.text());
    const pushData = await event.data.text();
    let data, title, body;
    try{
      data = await JSON.parse(pushData);
      title = data.title;
      body = data.body;
    }catch(e){
      title = "untitled";
      body = pushData;
    }
    
    const options = {
      title : title,
      body : body, 
      icon : '/favicon.ico',
    }

    self.showLocalNotificationSW(title, body, self.registration);

})

并且showLocalNotificationSw推送事件调用:

const showLocalNotificationSW = async (title, body, swRegistration) => {
    swRegistration.showNotification(title, options);
}

我在控制台中看到它说self.showNotificationWrap不是控制台中的功能。这是什么意思?

我没有任何名为 showNotificationWrap 的函数。

错误:

sw.js:48 Uncaught TypeError: self.showLocalNotificationWrap is not a function
    at sw.js:48

我该如何解决?

我该如何解决?

标签: javascriptjqueryasync-awaitweb-push

解决方案


原来我只需要取消注册并再次注册服务人员即可解决问题。

这是修改后的推送事件代码:

self.addEventListener('push', async function received(event) {
  let data, title, body;  
  data = event.data.text();
  console.log("Push received event - " + data);

    try{
      data = JSON.parse(data);
      console.log(JSON.stringify(data));
      title = data.title;
      body = data.body;
    }catch(error){
      console.error(error);
      title = "Undefined";
      body = data;
    }

    event.waitUntil(showNotificationSW(title, body, self.registration));

}) 

推荐阅读