Skip to content

Instantly share code, notes, and snippets.

@lholmquist
Last active August 29, 2015 14:26
Show Gist options
  • Save lholmquist/43bde013f7782809831c to your computer and use it in GitHub Desktop.
Save lholmquist/43bde013f7782809831c to your computer and use it in GitHub Desktop.
curl -X PUT SOME_REALLY_LONG_ENDPOINT
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<link rel="stylesheet" href="">
</head>
<body>
<script src="index.js"></script>
</body>
</html>
// index.js
navigator.serviceWorker.register('./service-worker.js').then(function (serivceWorkerRegistration) {
console.log('registered service worker, YAY');
console.log(serivceWorkerRegistration);
serivceWorkerRegistration.pushManager.subscribe().then(function (pushSubscription) {
// you want to use pushSubscription.endpoint for the sending
console.log(pushSubscription);
}).catch(function (err) {
console.log(err);
});
}).catch(function (err) {
console.log(err);
});
self.addEventListener('push', function (evt) {
console.log(evt);
console.log('Received a push message', event);
// THis didn't really work
// var title = 'Yay a message.';
// var body = 'We have received a push message.';
// var icon = '/images/icon-192x192.png';
// var tag = 'simple-push-demo-notification-tag';
// event.waitUntil(
// self.registration.showNotification(title, {
// body: body,
// icon: icon,
// tag: tag
// })
// );
});
// The SW will be shutdown when not in use to save memory,
// be aware that any global state is likely to disappear
console.log("SW startup");
self.addEventListener('install', function(event) {
console.log("SW installed");
alert('SW Instaled');
});
self.addEventListener('activate', function(event) {
console.log("SW activated");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment