I'm trying to implement push notifications in a progressive web apps and am using the new Payload support in Chrome.
I'm trying to send the endpoint and keys up to the server, but don't see the keys property on the PushSubscription object...
How do I access the keys so I can send them to the server?
The keys are available, but as you note aren't available directly in that way.
You have the option to either:
JSON.stringify(subscription)
which will create a string that includes the serialized keys (which you can then JSON.parse
if you really want), orsubscription.getKey('p256dh')
, which returns an ArrayBuffer, and then convert it to a string yourself, for example btoa(String.fromCharCode.apply(null, new Uint8Array(subscription.getKey('p256dh'))))
Note that the first approach creates "URL Safe Base 64" encoded keys while the second creates "Base 64 (non-URL Safe)". While the web-push Node library will accept either encoding for keys you may want to be careful with which encoding you choose for your library.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With