【问题标题】:Uncaught (in promise): TypeError: Cannot read property 'showNotification' of undefined未捕获(承诺):TypeError:无法读取未定义的属性“showNotification”
【发布时间】:2019-05-29 14:52:44
【问题描述】:

我想在我的应用中添加浏览器通知,所以我创建了这个功能:

showNotification() {
    navigator.serviceWorker.getRegistration()
    .then( function(reg){

      reg.showNotification('finish button clicked');
    });
  }

我从这里打电话:

stop() {
this.showNotification();
}

但我收到此错误:

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'showNotification' of undefined

我做错了什么?有没有更好的方法来利用浏览器通知而无需来自后端的任何消息?

【问题讨论】:

    标签: angular google-chrome push-notification


    【解决方案1】:

    要获得注册的服务工作者,您必须在 getRegistraion 中传递范围 URL

    例如:

     if ('serviceWorker' in navigator) {
       
               navigator.serviceWorker.getRegistration('./firebase-cloud-messaging-push-scope').then(function(registration)  {                         
                        setTimeout(() => {
                            registration.showNotification(notificationTitle,  notificationOptions);
                            registration.update();
                        }, 100);
                    }).catch(function (err) {
                        console.log("No Service Worker Registered", err);
                    })
    
     }   
    

    【讨论】:

      猜你喜欢
      • 2019-05-16
      • 2018-05-28
      • 1970-01-01
      • 1970-01-01
      • 2018-06-03
      • 2020-09-24
      • 1970-01-01
      • 2020-03-24
      • 2021-05-14
      相关资源
      最近更新 更多