Web push bildirimi gösterilmiyor

Sep 16 2020

Bir servis çalışanı kaydettim ve tarayıcıda bir web bildirimini test etmeye çalışıyorum. Chrome (ve Firefox), hizmet çalışanının başarıyla kaydedildiğini iddia ediyor.

React Uygulaması yüklendiğinde, bildirim alma izni verdim.

Benim içinde sw.js, bir için dinliyorum pusholay ve yukarıdaki ekran görüntüsünde görüldüğü gibi, Chrome Uygulaması sekmesinden bir örnek itme mesaj göndermek için çalışırken.

self.addEventListener("push", receivePushNotification);

PushChrome Uygulaması Sekmesine tıklandığında , pusholay tetiklenir ve çağrılır receivePushNotification. Ancak, tarayıcıda bir bildirim göstermeye çalıştığımda hiçbir şey olmuyor ve herhangi bir hata bildirilmiyor.

function receivePushNotification(event) {
  // This prints "Test push message from DevTools."
  console.log("[Service Worker] Push Received.", event.data.text());

  var options = {
    body: "This notification was generated from a push!"
  };

/*****
  I would expect the following line to display a notification, since I've already 
  granted permission to allow for notifications. Yet, nothing happens and there is 
  no error in the console.
*****/

  event.waitUntil(self.registration.showNotification("Hello world!", options));
}

Yanıtlar

miknik Sep 20 2020 at 05:32

Görünüşe göre, işlevinizi ayrı ayrı tanımlamakta / asenkron işlev olarak tanımlamamakta bir adım fazla ileri gitmişsiniz. Ayrıca olayı işlev çağrısına iletmiyorsunuz.

Bunu böyle yeniden yazarsan ne olur?

self.addEventListener("push", function(event) {
  console.log("[Service Worker] Push Received.", event.data.text());
  var options = {
    body: "This notification was generated from a push!"
  };
  event.waitUntil(self.registration.showNotification("Hello world!", options));
});