Service Worker - Push notification via VAPID prv / pub buttons

Several years ago I implemented a service worker push notification on a project I was working on by registering the app with Firebase and using the registration number as part of the manifest.json file in the server side app. In this case, I asked the user to allow notifications, get the browser logged once, save on the server side and everything works fine.

Now I am trying to implement a similar solution but using VAPID ( https://developers.google.com/web/ilt/pwa/introduction-to-push-notifications#using_vapid). The browser registers correctly, sends the registration to the server side app, and the app can send push notifications. The problem I got is that for at least 24 hours when I try to send a push notification to an already registered subscription, I get an InvalidSubscription (410 NotRegistered) response. Using VAPID does browser registration expire after a few hours? do I need to get a new registration every certain number of hours? If so, how? For example, if a user doesn't revisit the site for a day or so, how can I keep sending them notifications? I cannot find a clear link to this problem I am experiencing.

Here is the JS code I use in SW to get the registration in the browser:

function postPushReg(sub){
  var rawKey = sub.getKey ? sub.getKey('p256dh') : '';
  var key = rawKey ?
        btoa(String.fromCharCode.apply(null, new Uint8Array(rawKey))) :
        '';
  var rawAuthSecret = sub.getKey ? sub.getKey('auth') : '';
  var authSecret = rawAuthSecret ?
                   btoa(String.fromCharCode.apply(null, new Uint8Array(rawAuthSecret))) :
                   '';
  fetch('https://XXXXX', {
    method: 'post',
    headers: {'Content-type': 'application/json'},
    body: JSON.stringify({endpoint: sub.endpoint, key: key, authSecret: authSecret}),
  });
}

self.addEventListener('install', function(event){
  self.registration.pushManager.getSubscription()
  .then(function(sub){
    if (sub) return postPushReg(sub);
    return self.registration.pushManager.subscribe({userVisibleOnly: true,
                                    applicationServerKey: urlB64ToUint8Array('XXX')})
                                    .then(function(sub){
                                      postPushReg(sub);
                                    });
  });
});

self.addEventListener('push', function(e){
  ...
});

      

This is the Rails / Ruby server side (webpush) I am using to send the notification:

Webpush.payload_send(
        message: "msg",
        endpoint: j['endpoint'],
        p256dh: j['key'],
        auth: j['authSecret'],
        vapid: {
          subject: "mailto:XXXX",
          public_key: "XXX",
          private_key: "XXX",
        }
      )

      

Again, for the first few hours everything works, then I get a 410 NotRegistered response.

0


source to share


1 answer


Trying to offer the same suggestion: Web Push with VAPID: 400/401 Unauthorized registration , now it works fine. I only get browser registration once and after 2 days it still works fine



0


source







All Articles