Skip to content

Instantly share code, notes, and snippets.

@Spyna
Last active August 26, 2019 14:35
Show Gist options
  • Save Spyna/28140b8ba94039d0c5813eae9c2a9f0e to your computer and use it in GitHub Desktop.
Save Spyna/28140b8ba94039d0c5813eae9c2a9f0e to your computer and use it in GitHub Desktop.
How to create a web push notification subscription
const pushServerPublicKey = "<A PUSH SERVER PUBLIC KEY GOES HERE>";
/**
*
* using the registered service worker creates a push notification subscription and returns it
*
*/
function createNotificationSubscription() {
//wait for service worker installation to be ready, and then
return navigator.serviceWorker.ready.then(function(serviceWorker) {
// subscribe and return the subscription
return serviceWorker.pushManager
.subscribe({
userVisibleOnly: true,
applicationServerKey: pushServerPublicKey
})
.then(function(subscription) {
console.log("User is subscribed.", subscription);
return subscription;
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment