files/en-us/web/api/pushsubscription/index.md
{{ApiRef("Push API")}}{{SecureContext_Header}}{{AvailableInWorkers}}
The PushSubscription interface of the Push API provides a subscription's URL endpoint along with the public key and secrets that should be used for encrypting push messages to this subscription.
This information must be passed to the application server, using any desired application-specific method.
The interface also provides information about when the subscription will expire, and a method to unsubscribe from the subscription.
Each browser uses a particular push service.
A service worker can use {{domxref("PushManager.subscribe()")}} to subscribe to the supported service, and use the returned PushSubscription to discover the endpoint where push messages should be sent.
The PushSubscription is also used to get the public key and secret that the application server must use to encrypt the messages that it sends to the push service.
Note that the private keys used to decrypt push messages are not shared by the browser, and are used to decrypt messages before they are passed to the service worker.
This ensures that push messages remain private as they pass through the push server infrastructure.
The service worker doesn't need to know anything about the endpoints or encryption, other than to pass the relevant information onto the application server. Any mechanism may be used to share the information with the application server.
The p256dh public key and auth secret used for encrypting the message are provided to the service worker via its push subscription, using the {{domxref("PushSubscription.getKey()")}} method, along with the target endpoint for sending push messages in {{domxref("PushSubscription.endpoint")}}.
The coding that should be used for encryption is provided by the {{domxref("PushManager.supportedContentEncodings")}} static property.
This example shows how you might put the needed information from PushSubscription and supportedContentEncodings into a JSON object, serialize it using JSON.stringify(), and post the result to the application server.
// Get a PushSubscription object
const pushSubscription =
await serviceWorkerRegistration.pushManager.subscribe();
// Create an object containing the information needed by the app server
const subscriptionObject = {
endpoint: pushSubscription.endpoint,
keys: {
p256dh: pushSubscription.getKey("p256dh"),
auth: pushSubscription.getKey("auth"),
},
encoding: PushManager.supportedContentEncodings,
/* other app-specific data, such as user identity */
};
// Stringify the object and post to the app server
fetch("https://example.com/push/", {
method: "post",
body: JSON.stringify(subscriptionObject),
});
navigator.serviceWorker.ready
.then((reg) => reg.pushManager.getSubscription())
.then((subscription) => subscription.unsubscribe())
.then((successful) => {
// You've successfully unsubscribed
})
.catch((e) => {
// Unsubscribing failed
});
{{Specifications}}
{{Compat}}