Last active
August 26, 2019 14:35
-
-
Save Spyna/28140b8ba94039d0c5813eae9c2a9f0e to your computer and use it in GitHub Desktop.
How to create a web push notification subscription
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
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