push-sw.html (1573B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Service worker push test</title> 6 </head> 7 <body> 8 <script type="text/javascript"> 9 10 "use strict"; 11 12 let registration; 13 let subscription; 14 15 const registerServiceWorker = async function() { 16 const perm = { type: "desktop-notification", allow: true, context: document }; 17 await SpecialPowers.pushPermissions([perm]); 18 19 try { 20 registration = await navigator.serviceWorker.register("push-sw.worker.js"); 21 dump("Push service worker registered\n"); 22 } catch (e) { 23 dump("Push service worker not registered: " + e + "\n"); 24 } 25 }; 26 27 // Helper called from helper-serviceworker.js to unregister the service worker. 28 window.getRegistration = function() { 29 return registration; 30 }; 31 32 // Helper called from browser_aboutdebugging_serviceworker_pushservice_url.js 33 window.subscribeToPush = async function() { 34 try { 35 subscription = await registration.pushManager.subscribe(); 36 dump("SW subscribed to push: " + subscription.endpoint + "\n"); 37 } catch (e) { 38 dump("SW not subscribed to push: " + e + "\n"); 39 } 40 }; 41 42 // Helper called from browser_aboutdebugging_serviceworker_pushservice_url.js 43 window.unsubscribeToPush = async function() { 44 subscription.unsubscribe(); 45 }; 46 47 // Expose a promise to wait until the service worker is claimed. 48 window.onSwClaimed = new Promise(resolve => { 49 navigator.serviceWorker.addEventListener("message", function(event) { 50 if (event.data == "sw-claimed") { 51 resolve(); 52 } 53 }); 54 }); 55 56 // Register the service worker. 57 registerServiceWorker(); 58 59 </script> 60 </body> 61 </html>