Created
December 2, 2015 14:22
-
-
Save beverloo/8bda511f92806ebdd5f2 to your computer and use it in GitHub Desktop.
PushSubscription - transmitting information to your server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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