javascript - 未捕获的 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
我该如何解决?
我该如何解决?
解决方案
原来我只需要取消注册并再次注册服务人员即可解决问题。
这是修改后的推送事件代码:
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));
})
推荐阅读
- git - .gitconfig“错误的配置行”由于别名
- vue.js - 如何在 localhost 中使用 HTTPS 运行 NUXT(npm run dev)?
- c# - 如何通过文本文件在控制台上显示学生的姓名和平均成绩?
- python-3.x - 将张量分成两组的最快方法,对于每组,随机将一个分配给 0,另一个分配给 1
- swift - '不能用'UIImagePickerController.Infokey'类型的索引为'[NSObject:AnyObject]'类型的值下标
- sql - PostgreSQL查询检查是否从多个表中引用了一行
- botframework - QnA Maker 知识库返回“在 KB 中找不到好的匹配项”以获取后续提示
- python - 如何从python子目录中的多个文件中读取特定的文本文件
- azure-devops - 构建管道 - 获取触发管道的签入标题
- node.js - 如何解析来自 Slack 的交互消息?