Created
January 28, 2015 20:24
-
-
Save balazs/7ef961e8fac54afc2ccc to your computer and use it in GitHub Desktop.
This file contains 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
<!DOCTYPE html> | |
<!-- | |
Copyright 2014 Google Inc. All Rights Reserved. | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
--> | |
<!-- <link rel="manifest" href="Manifest.json" /> --> | |
<script> | |
var SCOPE = { | |
scope: '/' | |
}; | |
function setCheckbox(id, checked) { | |
document.getElementById(id).checked = checked; | |
} | |
function appendLog(id, text) { | |
document.getElementById(id).innerHTML += "<br/>" + text; | |
} | |
function registerSW() { | |
navigator.serviceWorker.register('service-worker.js', SCOPE).then( | |
function(serviceWorkerRegistration) { | |
console.log('SW registered'); | |
setCheckbox("swDisplay", true); | |
setTimeout(registerPush, 0); | |
} | |
).catch( | |
function(whut) { | |
console.error('uh oh... '); | |
console.error(whut); | |
} | |
); | |
} | |
function unregisterSW() { | |
navigator.serviceWorker.unregister(SCOPE).then( | |
function() { | |
console.log('SW unregistered'); | |
setCheckbox("swDisplay", false); | |
} | |
); | |
} | |
function registerPush() { | |
navigator.serviceWorker.ready.then( | |
function(serviceWorkerRegistration) { | |
serviceWorkerRegistration.pushManager.register().then( | |
function(pushRegistration) { | |
console.log("push registration: success"); | |
console.log("regId: " + pushRegistration.registrationId); | |
console.log("endPoint:" + pushRegistration.endpoint); | |
setCheckbox("pushRegDisplay", true); | |
}, | |
function(error) { | |
console.log("push registration: failed"); | |
console.log(error.message); | |
} | |
); | |
} | |
); | |
} | |
this.onmessage = function(event) { | |
console.log("msg from SW: " + event.data); | |
appendLog('swmsg', event.data); | |
} | |
this.onload = function() { | |
registerSW(); | |
} | |
</script> | |
<link rel="prefetch" href="service-worker.js" /> | |
<body> | |
<p> | |
<span>Service worker active: </span><input type="checkbox" id="swDisplay" /> | |
</p> | |
<button onclick="registerSW()">Register SW</button> | |
<button onclick="unregisterSW()">Unregister SW</button> | |
<p> | |
<span>Push registered: </span><input type="checkbox" id="pushRegDisplay" /> | |
<button onclick="registerPush()">PushRegister</button> | |
</p> | |
<div id="swmsg">--SW says--</div> | |
</body> |
This file contains 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
function sendMsg(msg) { | |
this.clients.getAll().then( | |
function (clients) { | |
clients[0].postMessage(msg); | |
} | |
); | |
} | |
this.onpush = function(event) { | |
console.log("push message received"); | |
console.log(event.data); | |
sendMsg('Sir, you have push message: "' + event.data.text() + '"'); | |
} | |
this.onmessage = function() { | |
console.log("msg"); | |
sendMsg(); | |
} | |
this.oninstall = function(event) { | |
console.log("dude, I'm installed"); | |
} | |
this.onactivate = function(event) { | |
console.log("dude, I'm activated"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment