Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keys property doesn't exist on my PushRegistration object

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?

like image 382
owencm Avatar asked Jan 07 '23 09:01

owencm


1 Answers

The keys are available, but as you note aren't available directly in that way.

You have the option to either:

  1. Call JSON.stringify(subscription) which will create a string that includes the serialized keys (which you can then JSON.parse if you really want), or
  2. Get a particular key with subscription.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.

like image 131
owencm Avatar answered Jan 29 '23 10:01

owencm