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 badges3 Answers
Reset to default 13You 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));