最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript Web Push: where can I get "auth" key? - Stack Overflow

programmeradmin2浏览0评论

I'm starting to use Web Push Notification; I don't know where I can find the auth key:

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

I can get endpoint and p256dh from ServiceWorker>registeration.pushManager.getSubscription() but not the auth key.

Thanks

I'm starting to use Web Push Notification; I don't know where I can find the auth key:

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

I can get endpoint and p256dh from ServiceWorker>registeration.pushManager.getSubscription() but not the auth key.

Thanks

Share Improve this question edited Mar 13, 2020 at 1:08 moeinghasemi 1093 silver badges12 bronze badges asked Nov 10, 2016 at 7:24 Ali AminiAli Amini 1923 silver badges15 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 13

You can use the getKey method to get both p256dh and auth (see the specs or the example from the specs).

It's even simpler to just call JSON.stringify on the PushSubscription object returned by the getSubscription promise.

Using Typescript, the PushSubscription object should have a method on it called toJSON. Just use that.

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
  }
};
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));
发布评论

评论列表(0)

  1. 暂无评论