Last active
March 16, 2016 15:03
-
-
Save deanhume/2f04d6b1b6316fc97c52 to your computer and use it in GitHub Desktop.
Register Service Worker
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
if ('serviceWorker' in navigator) { | |
navigator.serviceWorker.register('service-worker.js').then(function(registration) { | |
// Registration was successful | |
console.log('ServiceWorker registration successful with scope: ', registration.scope); | |
registration.pushManager.subscribe({userVisibleOnly: true}).then(function(subscription){ | |
isPushEnabled = true; | |
console.log("subscription.subscriptionId: ", subscription.subscriptionId); | |
console.log("subscription.endpoint: ", subscription.endpoint); | |
// TODO: Send the subscription subscription.endpoint | |
// to your server and save it to send a push message | |
// at a later date | |
return sendSubscriptionToServer(subscription); | |
}); | |
}).catch(function(err) { | |
// registration failed :( | |
console.log('ServiceWorker registration failed: ', err); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment