Skip to content

Instantly share code, notes, and snippets.

@BackEndTea
Created April 24, 2018 07:37
Show Gist options
  • Select an option

  • Save BackEndTea/9645ee770772f8c7456c8cf6361b0f00 to your computer and use it in GitHub Desktop.

Select an option

Save BackEndTea/9645ee770772f8c7456c8cf6361b0f00 to your computer and use it in GitHub Desktop.
Quick service worker registration
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
navigator.serviceWorker.register('/sw.js').then(
function (serviceWorkerRegistration) {
console.log('service worker has been registered, attempting to register push manager');
if (!('PushManager' in window)) {
console.log('This browser does not support push notifications');
return;
}
serviceWorkerRegistration.pushManager.subscribe().then(
function(pushSubscription) {
console.log('Subscribed to push manager');
console.log(pushSubscription.endpoint);
// The push subscription details needed by the application
// server are now available, and can be sent to it using,
// for example, an XMLHttpRequest.
}, function (error) {
console.log('Unable to register push notifications', error);
}
);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment