我开始使用网络推送通知;我不知道在哪里可以找到
auth
键:
var pushSubscription = {
endpoint: '< Push Subscription URL >',
keys: {
p256dh: '< User Public Encryption Key >',
auth: '< ???? User Auth Secret ???? >'
}
};
我可以从
endpoint
获取 p256dh
和 ServiceWorker>registeration.pushManager.getSubscription()
,但不能获取 auth
键。
谢谢
使用 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
}
};
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));