opened
perrin4869
username_0
This is a question regarding all the push cookbooks. In all of them, we subscribe to push subscriptions from within the webpage, more or less as follows:

```js
navigator.serviceWorker.register('service-worker.js')
.then(function(registration) {
  // Use the PushManager to get the user's subscription to the push service.
  return registration.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 registration.pushManager.subscribe({ userVisibleOnly: true });
  });
}).then(function(subscription) {
  // send to server...
});
```

Couldn't the same code be included inside the service worker's activation or installation phase, and wouldn't it make more sense to confine it to it?

```js
self.addEventListener('activate', function(event) {
  event.waitUntil(
  registration.pushManager.getSubscription()
  .then(function(subscription) {
    // If a subscription was found, return it.
    if (subscription) {
      return subscription;
    }

    return registration.pushManager.subscribe({ userVisibleOnly: true });
  })
  .then(function(subscription) {
    // send subscription to server...
    return self.clients.claim();
  }));
});
```
Why not subscribe to push subscriptions within the service worker's onactivate event?
issue
created
delapuente
username_1
I can not imagine if there is a good reason to do this. Perhaps to reduce client/service worker messages but maybe @username_2 can answer better.
comment
created
marco-c
username_2
Honestly, I don't see any difference in the two approaches. @username_0 why do you think it would make more sense?
comment
created
perrin4869
username_0
Well, there are 2 reasons I think it makes more sense:

1) Purely semantic reason. Seems like all code related to push notifications belongs in service workers, which are the only ones that can intercept these notifications in any case. Placing the code outside of service workers just feels out of place.

2) If we subscribe outside of service workers, we are submitting a subscription to the server every time the page is loaded, even if the subscription hasn't changed. Not a big deal, but it's a small detail.
comment
created
nitsas
username_3
I agree with perrin4849 that all code about push notifications seems to belong in the service worker itself.

Unfortunately, subscriptions don't stay the same for ever. When we call `subscribe()`, the browser talks with a push service to get a push subscription. I don't think the push service guarantees that it'll always return the same subscription. So we have to `subscribe()` again every now and then, just to be safe.

The service worker's `activate` event fires only once after the worker updates, installs and takes the place of an old worker. So it doesn't seem like a good place to resubscribe. If the worker never gets a new update, we'll never resubscribe to push notifications.
comment
created
perrin4869
username_0
@username_3 that's why you'd subscribe to the 'pushsubscriptionchange` event

```
self.addEventListener('pushsubscriptionchange', () => (registerPushSubscription()));
```
comment
created
pazguille
username_4
Hi!

I am trying to subscribe inside a service worker but I got an error:

“ERROR DOMException: Registration failed - missing applicationServerKey, and gcm_sender_id not found in manifest”

When I subscribe to push outside service worker every works fine.

Why it happend?
comment
created
marco-c
username_2
@username_4 I guess you haven't set a `applicationServerKey` in your call to `subscribe`?
comment
created
pazguille
username_4
Hi @username_2. In this case I'm using a `manifest.json` with a valid `gcm_sender_id` value.  Is it necessary to set a `applicationServerKey`?
comment
created
marco-c
username_2
No, it shouldn't be. I would suggest it since it's the standard now and `gcm_sender_id` is probably deprecated.
It seems like a bug in Chrome, but I wouldn't expect them to fix it since the new way of doing things is VAPID.
comment
created
pazguille
username_4
@username_2 you are right! Unfortunately is a bug in Chrome: https://bugs.chromium.org/p/chromium/issues/detail?id=597317&can=1&q=gcm_sender_id&colspec=ID%20Pri%20M%20Stars%20ReleaseBlock%20Component%20Status%20Owner%20Summary%20OS%20Modified

Thank you!
comment
created
h43z
username_5
Subscribing within a service worker will not request the permission to show notification (like when done from regular javascript file) and instantly throws an exception.

`Registration failed - permission denied`
comment