Skip to content

Commit

Permalink
Correct PushSubscription code example (#38031)
Browse files Browse the repository at this point in the history
* Correct PushSubscription code example

The example used `pushKeys`, the correct method name is `pushKey`

* Update index.md

* Update index.md

* Update index.md

---------

Co-authored-by: Joshua Chen <[email protected]>
  • Loading branch information
maxrothman and Josh-Cena authored Feb 9, 2025
1 parent 737125e commit a8fd735
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions files/en-us/web/api/pushsubscription/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,41 +55,39 @@ This example shows how you might put the needed information from `PushSubscripti

```js
// Get a PushSubscription object
const pushSubscription = await serviceWorkerRegistration.pushManager.subscribe();
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.getKeys('p256dh'),
auth: pushSubscription.getKeys('auth'),
p256dh: pushSubscription.getKey("p256dh"),
auth: pushSubscription.getKey("auth"),
},
encoding: PushManager.supportedContentEncodings,
/* other app-specific data, such as user identity */
};

// Stringify the object an post to the app server
fetch(`https://example.com/push/`, {
fetch("https://example.com/push/", {
method: "post",
body: JSON.stringify(subscriptionObject);
body: JSON.stringify(subscriptionObject),
});
```

### Unsubscribing from a push manager

```js
navigator.serviceWorker.ready.then((reg) => {
reg.pushManager.getSubscription().then((subscription) => {
subscription
.unsubscribe()
.then((successful) => {
// You've successfully unsubscribed
})
.catch((e) => {
// Unsubscribing failed
});
navigator.serviceWorker.ready
.then((reg) => reg.pushManager.getSubscription())
.then((subscription) => subscription.unsubscribe())
.then((successful) => {
// You've successfully unsubscribed
})
.catch((e) => {
// Unsubscribing failed
});
});
```

## Specifications
Expand Down

0 comments on commit a8fd735

Please sign in to comment.