[英]Create-react-app registerServiceWorker giving undefined at getRegistration() promise
首先,它可以在PC上运行,但不能在移动设备上运行,这是我的主要问题。
我使用create-react-app创建了PWA,并且尝试实现推送通知,但是在我的promise的'reg'参数中似乎不确定。 有人知道如何使它起作用吗? 我已经尝试退出create-react-app,但是出现了同样的问题。
我将此代码添加到registerServiceWorker.js中 :
export function displayNotification() {
if (Notification.permission === 'granted') {
console.log('this is showing at console when i click the button')
navigator.serviceWorker.getRegistration().then(function(reg) {
reg.showNotification('Hello world!');
});
}
}
错误:
未处理的拒绝(TypeError):无法读取未定义的属性“ showNotification”
此问题谈论此问题,但我已经尝试过这些解决方案,并且错误相同。
https://github.com/facebook/create-react-app/issues/2253#issuecomment-312359460
下面的是旧版本,仅在浏览器PC上有效,在PWA中,单击按钮时会显示空白页。
我的PWA: https : //danilosilvadev.github.io/pwa-test/
我的代码: https : //github.com/danilosilvadev/pwa-test/tree/master
如果能帮助我使用一个使用名为“ react-web-notification”的npm软件包的旧版本:“ ^ 0.3.1”或我在此问题中提供的代码(无论如何我都想手动执行),我将不胜感激。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.