【问题标题】:Javascript Web Push: where can I get "auth" key?Javascript Web Push:我在哪里可以获得“auth”密钥?
【发布时间】:2016-11-10 07:24:17
【问题描述】:

我开始使用网络推送通知;我不知道在哪里可以找到auth 键:

var pushSubscription = {
  endpoint: '< Push Subscription URL >',
  keys: {
    p256dh: '< User Public Encryption Key >',
    auth: '< ???? User Auth Secret ???? >'
  }
};

我可以从ServiceWorker&gt;registeration.pushManager.getSubscription() 获得endpointp256dh,但不能从auth 密钥中获得。

谢谢

【问题讨论】:

    标签: javascript web-push web-notifications


    【解决方案1】:

    您可以使用getKey 方法获取p256dhauth(请参阅the specsthe example from the specs)。

    getSubscription 承诺返回的 PushSubscription 对象上调用 JSON.stringify 更加简单。

    【讨论】:

    • 如果你想在 JS 中使用对象(获取密钥),不要摆弄 ab2string 转换或 pushSubscription.keyKeys()...(剧透:不会工作。)只要做这个:var subJSObject = JSON.parse(JSON.stringify(pushSubscription)); var endpoint = subJSObject.endpoint; var auth = subJSObject.keys.auth; var p256dh = subJSObject.keys.p256dh;
    • @mondjunge 你是完全正确的。我选择了艰难的方法,而我从所有这些方法中获得的最大收益都是胡言乱语。下次我将查看手头的具体问题,而不是尝试一般解决方案,例如尝试查找转换方法。
    【解决方案2】:

    使用 Typescript,PushSubscription 对象应该有一个名为 toJSON 的方法。就用那个吧。

    const sub: PushSubscription = YOUR_RAW_PUSH_SUBSCRIPTION;
    const pushSubscription = {
      endpoint: sub.endpoint,
      expirationTime: sub.expirationTime,
      keys: {
        p256dh: sub.toJSON().keys.p256dh,
        auth: sub.toJSON().keys.auth
      }
    };
    

    【讨论】:

      猜你喜欢
      • 2021-07-24
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多