Skip to content

Instantly share code, notes, and snippets.

@beverloo
Created December 2, 2015 14:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beverloo/8bda511f92806ebdd5f2 to your computer and use it in GitHub Desktop.
Save beverloo/8bda511f92806ebdd5f2 to your computer and use it in GitHub Desktop.
PushSubscription - transmitting information to your server
navigator.serviceWorker.ready.then(registration => {
// Note that userVisibleOnly is not required for payloads - it's (still)
// required for Chrome's implementation though.
registration.pushManager.subscribe({ userVisibleOnly: true }).then(subscription => {
// Option 1: Using JSON.stringify() (recommended).
//
// The created JSON will have the following structure:
// { "endpoint": "https://...",
// "keys": {
// "p256dh": "...", // base64url encoded uncompressed P-256 EC point
// "auth": "...", // base64url encoded 16-byte authentication secret
// }
// }
sendToServer(JSON.stringify(subscription));
// Option 2: Manually get the data.
let endpoint = subscription.endpoint,
p256dh = subscription.getKey('p256dh'),
auth = subscription.getKey('auth');
// |p256dh| and |auth| are ArrayBuffers containing the raw bytes. These will
// probably have to be converted to another representation before being sent
// to the server, unless you're happy to transmit raw bytes (i.e. using a
// WebSocket.)
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment