簡體   English   中英

Create-react-app registerServiceWorker在getRegistration()承諾中提供未定義

[英]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”或我在此問題中提供的代碼(無論如何我都想手動執行),我將不勝感激。

我在計算機上測試了它:Chrome + Windows,它可以正常工作。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM