Javascript Web Push:在哪里可以获得“auth”密钥?

问题描述 投票:0回答:3

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

auth
键:

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

我可以从

endpoint
获取
p256dh
ServiceWorker>registeration.pushManager.getSubscription()
,但不能获取
auth
键。

谢谢

javascript web-push web-notifications
3个回答
11
投票

您可以使用

getKey
方法获取
p256dh
auth
(请参阅 规格规格中的示例)。

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


9
投票

使用 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
  }
};

0
投票
this.swPush.requestSubscription({serverPublicKey: 'BPffxa1Lf3WJuqc-OLSRCYdYteLAzXHZhAxHUXcEgiBUpHzeJUBq9M6k44f8YeSs4ckVLWCYyvbviLBnknXnTKU'})
  .then(subscription => {        
    this.p256dhKey = this.getBase64Url(subscription.getKey('p256dh')!);
    this.authKey = this.getBase64Url(subscription.getKey('auth')!);
    this.endpoint = subscription.endpoint;
  })
  .catch(error => console.error('Error requesting subscription:', error));
© www.soinside.com 2019 - 2024. All rights reserved.