本文介绍了Service Worker - 使用 VAPID prv/pub 密钥推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几年前,我通过在 Firebase 上注册一个应用程序,并使用注册号作为服务器端应用程序上 manifest.json 文件的一部分,在我正在从事的项目中使用 Service Worker 实现了推送通知.在这种情况下,我请求用户允许通知,浏览器注册一次,保存在服务器端,一切正常.

A couple of years ago I implemented push notification with service worker on a project I was working on, by registering an app on Firebase, and using the registration number as part of the manifest.json file on the server side app. In that case I requested the user to allow notifications, got the browser registration once, saved on server side, and all works fine.

我现在正在尝试实施类似的解决方案,但使用 VAPID (https://developers.google.com/web/ilt/pwa/introduction-to-push-notifications#using_vapid).浏览器正确注册,将注册发送到服务器端应用程序,应用程序能够发送推送通知.我遇到的问题是,至少 24 小时后,当我尝试向已注册的订阅发送推送通知时,我收到 InvalidSubscription 响应 (410 NotRegistered).使用 VAPID,浏览器注册是否会在几个小时后过期?我是否需要每隔一定时间进行一次新的注册?如果是,如何?例如,如果用户在一天左右的时间内从未重新访问该站点,我如何才能继续向他们发送通知?对于我遇到的这个问题,我找不到任何明确的参考.

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

这是我在 SW 中用来获取浏览器注册的 JS 代码:

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

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){
  ...
});

这是我用来发送通知的 Rails/Ruby 服务器端 gem (webpush):

This is the Rails/Ruby server side gem (webpush) I use 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",
        }
      )

同样,在最初的几个小时内一切正常,然后我收到 410 NotRegistered 响应.

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

推荐答案

尝试此处发布的相同建议:带有 VAPID 的网络推送:400/401 未经授权的注册 ,现在工作正常.我只注册了一次浏览器,两天后它仍然工作正常

Trying the same suggestion posted here: Web Push with VAPID: 400/401 Unauthorized Registration , it is now working fine. I get the browser registration only once, and after 2 days it is still working fine

这篇关于Service Worker - 使用 VAPID prv/pub 密钥推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 12:37