5

PushManager.getSubscription()arasındaki fark nelerdir `pushManager.subscribe` ve` pushManager.getSubscription` Servisi İşçinin

Retrieves an existing push subscription. It returns a Promise that resolves to a PushSubscription object containing details of an existing subscription. If no existing subscription exists, this resolves to a null value.

[...]

PushManager.subscribe()

Subscribes to a push service. It returns a Promise that resolves to a PushSubscription object containing details of a push subscription. A new push subscription is created if the current service worker does not have an existing subscription.

MDN en pushManager belgelerine göre. Yöntemler hemen hemen aynıdır, ancak getSubcription() durumunda boş değerle çözülebilir.

Temel olarak, subscribe()'u kullanabileceğimi ve Servis Ekibinin kullanılabilir olması durumunda aboneliği almaya çalışacağını ve mevcut olmadığında yeni bir tane oluşturmayı anladığımı anlıyorum.

=> Ama başka bir şey yapmaya çalışıyordum. Önce abone olmak istiyorum, eğer null ile çözüldüyse, onu abone olmaya çalışacağım. Sonra hata ile sona erdi am

navigator.serviceWorker.register('./worker.js') 
    .then(function(reg) { 

     // Subscribe push manager 
     reg.pushManager.getSubscription() 
     .then(function(subscription) { 

      if(subscription){ 
       // TODO... get the enpoint here 
      } else { 
       reg.pushManager.subscribe() 
       .then(function(sub){ 
        // TODO... get the endpoint here 
       }); 
      } 

     }, function(error) { 
      console.error(error); 
     }) 
    }); 

Ama:

Uncaught (in promise) DOMException: Subscription failed - no active Service Worker

kafa karıştırıcı olduğunu ve can muhtemelen bir hata bu Servis İşçisi'nin itin API Chrome bir sınırlamadır şüphe veya ediyorum. Bu garip davranış hakkında herhangi bir bilgisi var mı?

cevap