6

I am new to service workers and GAE, I am able to register the service workers but not able to subscribe the PushManager, getting subscription null error. Find the below code for reference.

serviceWorkerRegistration.pushManager.getSubscription()  
  .then(function(subscription) {  
    var pushButton = document.querySelector('.js-push-button');  
    pushButton.disabled = false;

    if (!subscription) {  
      console.log('subscription error '); 
      return;  
    }
    console.log('subscriptioned ');

    // Keep your server in sync with the latest subscriptionId
    sendSubscriptionToServer(subscription);

    // Set your UI to show they have subscribed for  
    // push messages  
    pushButton.textContent = 'Disable Push Messages';  
    isPushEnabled = true;  
  })  
  .catch(function(err) {  
    console.warn('Error during getSubscription()', err);  
  });
});

In above code getting "subscription" value as "null" inside then, so that, control is coming to if block and simply returning.

k0pernikus
  • 60,309
  • 67
  • 216
  • 347
Srinivas
  • 81
  • 2
  • 3

1 Answers1

8

Initially, when the user isn't subscribed, the promise returned by getSubscription resolves to null. You need to call registration.pushManager.subscribe in order to subscribe the user (and then, the next time the user visits your site, getSubscription will resolve with a non-null subscription).

There are many examples on the ServiceWorker Cookbook.

A simple example here:

// Use the PushManager to get the user's subscription to the push service.
serviceWorkerRegistration.pushManager.getSubscription()
.then(function(subscription) {
  // If a subscription was found, return it.
  if (subscription) {
    return subscription;
  }

  // Otherwise, subscribe the user (userVisibleOnly allows to specify
  // that we don't plan to send notifications that don't have a
  // visible effect for the user).
  return serviceWorkerRegistration.pushManager.subscribe({
    userVisibleOnly: true
  });
})
.then(function(subscription) {
  // Here you can use the subscription.
Marco Castelluccio
  • 10,152
  • 2
  • 33
  • 48
  • 3
    You can simplify this to `registration.pushManager.subscribe(...).then(subscription => ...)`. `subscribe()` will return the existing subscription if there is one, so calling `getSubscription()` first isn't necessary. – Jeff Posnick Feb 11 '16 at 10:29
  • 1
    That's useful in this case, although sometimes you might need to act differently if a subscription already exists. – Marco Castelluccio Feb 11 '16 at 10:51
  • thanks Jeff Posnick, After making above changes, Every thing is working in local host now. But when deployed in GAE, I am getting the below error "Registration failed - no sender id provided" – Srinivas Feb 13 '16 at 09:09
  • You need to create a project on the Google Developer Console, get a few keys from there (https://developers.google.com/web/fundamentals/getting-started/push-notifications/step-04) and then you need to create a web app manifest containing the `gcm_sender_id` value, which is the Project Number from the Developer Console. See also https://developer.mozilla.org/en-US/docs/Web/API/Push_API/Using_the_Push_API#Extra_steps_for_Chrome_support. – Marco Castelluccio Feb 13 '16 at 11:07