On the client-side, to subscribe to web push notifications, you need to call subscribe
with the applicationServerKey
option, like this:
var serviceWorkerRegistration = ...
serviceWorkerRegistration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: urlB64ToUint8Array("......")
}).then(function(subscription) {
...
})
You can send the subscription
object to the server, where it can be saved.
To send a push message to the subscriber, the server needs to post to the URL indicated by the key endpoint
in the object subscription
. The server needs to identify itself to the provider (Mozilla or Google or whoever) using VAPID. For instance, using the Python library pywebpush
, this call would be made:
import pywebpush
pywebpush.webpush(
subscription,
data,
vapid_private_key="path_to_private_key.pem",
vapid_claims={"sub": "mailto:example@example.com"},
)
Here's my question:
Is the private VAPID key used to send the push message the one that corresponds to the public key passed to serviceWorkerRegistration.pushManager.subscribe
on the client-side? Or does it belong to a separate keypair? My intuition tells me it should belong to the same keypair, but the term VAPID is only ever mentioned when talking about sending push messages, and not when subscribing, so I'm not confident that that assumption is correct.