Skip to content

Instantly share code, notes, and snippets.

@lorenzhs
Created January 6, 2016 13:26
Show Gist options
  • Save lorenzhs/5c9611650cd9e36778c6 to your computer and use it in GitHub Desktop.
Save lorenzhs/5c9611650cd9e36778c6 to your computer and use it in GitHub Desktop.
// File needs to be stored in the root of the app.
this.addEventListener('install', function(event) {
event.waitUntil(
caches.open('v1').then(function(cache) {
return cache.addAll([
'/assets/img/favicon.png',
'/assets/img/glowing_bear_128x128.png',
'/assets/img/glowing-bear.svg',
'/assets/img/badge_playstore.png',
'/assets/img/badge_firefoxos.png',
// '/assets/audio/sonar.ogg',
// '/assets/audio/sonar.mp3',
]);
})
);
});
// use cache for requests, or fallback to fetching
this.addEventListener('fetch', function(event) {
event.respondWith(
caches.match(event.request).catch(function() {
return fetch(event.request);
})
);
});
this.addEventListener('push', function(event) {
// TODO, support GCM here
var title = 'Push message';
event.waitUntil(
self.registration.showNotification(title, {
body: 'The Message',
icon: 'assets/img/favicon.png',
tag: 'my-tag'
}));
});
this.onnotificationclick = function(event) {
// Android doesn't close the notification when you click on it
// See: http://crbug.com/463146
event.notification.close();
// This looks to see if the current is already open and
// focuses if it is
event.waitUntil(clients.matchAll({
type: "window"
}).then(function(clientList) {
for (var i = 0; i < clientList.length; i++) {
var client = clientList[i];
if ('focus' in client) {
return client.focus();
}
}
/*
if (clients.openWindow) {
return clients.openWindow('/glowing-bear/');
}
*/
}));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment