0

I'm trying to enable web push notification but after call reg.pushManager.subscribe the promise is not resoved.

async enablePushNotification() { // CALLED ON USER CLICK
  try {
    const status = await Notification.requestPermission();
    if (status === 'granted') {
      const reg = await navigator.serviceWorker.ready;
      this.updateSubscriptionOnServer(reg);
    }
  } catch (error) {
    console.log('Error enabling push notification', error);
  }
}

async updateSubscriptionOnServer(reg) {
  try {
    const newSubscription = await reg.pushManager.subscribe({
      userVisibleOnly: true,
      applicationServerKey: urlB64ToUint8Array(VAPID_PUBLIC),
    });

    // BELLOW IS NEVER CALLED
    console.log('Subscription', newSubscription);
    const hasSubscription = !(newSubscription === null);
    this.setState(setHasSubscribed(hasSubscription));
    subscribeToPushNotification(newSubscription); // CALL API
  } catch (e) {
    console.error('Ops', e);
  }
}

What I'm doing wrong?

Cœur
  • 37,241
  • 25
  • 195
  • 267
ridermansb
  • 10,779
  • 24
  • 115
  • 226
  • Did you get any error? I think this [SO post](https://stackoverflow.com/questions/35775485/pushmanager-subscription-promise-never-fulfill-nor-reject) is related to your problem if I am not mistaken. And one of the answer give an idea that [Chromium team is working on the fix for this issue](https://bugs.chromium.org/p/chromium/issues/detail?id=623062). – MαπμQμαπkγVπ.0 Oct 09 '17 at 14:04
  • Any error, just didn't resolve the promise – ridermansb Oct 09 '17 at 14:07
  • I just tried this code in Chrome 65.0.3325.18 and it works. Only thing I had to correct was the missing? function keyword: async function enablePushNotification and async function updateSubscriptionOnServer – Stef Chäser Apr 09 '18 at 23:24

0 Answers0