Created
April 24, 2018 07:37
-
-
Save BackEndTea/9645ee770772f8c7456c8cf6361b0f00 to your computer and use it in GitHub Desktop.
Quick service worker registration
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) { | |
| 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