{
"name": "MyApp",
"short_name": "MyApp",
"icons": [
{
"src": "/images/myapp-256.png",
"sizes": "256x256",
"type": "image/png"
},
...
],
"start_url": "/index.html",
"display": "standalone",
"background_color": "#BADA55",
"theme_color": "#BADA55"
}
importScripts('/path/to/workbox-sw.js');
const workboxSW = new WorkboxSW({
clientsClaim: true,
skipWaiting: true
});
// Insert caching rules here
workboxSW.precache([
{
url: '/index.html',
revision: '613e6c7332dd83e848a8b00c403827ed'
},
{
url: '/styles/main.css',
revision: '59a325f32baad11bd47a8c515ec44ae5'
}
]);
workboxSW.router.registerRoute('/resource/path/',
workboxSW.strategies.cacheFirst({
cacheName: 'my-resource-cache',
cacheExpiration: {
maxEntries: 10,
maxAgeSeconds: 7 * 24 * 60 * 60
}
})
);
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker
.register('/service-worker.js')
.then(function() {
console.log('service worker registered');
});
}
</script>
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker
.register('/service-worker.js')
.then(function(sw) {
window.sw = sw;
});
}
</script>
window.sw.pushManager.subscribe()
.then(function(s) {
// Subscription successful
updateSubscriptionUI(true);
registerSubscriptionOnServer(s.endpoint);
});
window.sw.pushManager.getSubscription()
.then(function(s) {
s.unsubscribe().then(function(){
// Unsubscribed
updateSubscriptionUI(false);
unRegisterSubscriptionOnServer(s.endpoint);
});
});
self.addEventListener('push', function(e) {
e.waitUntil(
self.registration
.showNotification('Yay a message!', {
body: 'We have received a push message.',
icon: '/images/icon-192x192.png',
tag: 'my-app'
})
);
});
self.addEventListener('notificationclick', function(e) {
e.notification.close();
e.waitUntil(
clients.matchAll({ type: "window" })
.then(function(clientList) {
clientList.forEach(function (client) {
if (client.url == '/') {
return client.focus();
}
});
return clients.openWindow('/');
})
);
});
curl --header "Authorization: key={YOUR_SERVER_KEY}"
--header "Content-Type: application/json"
https://android.googleapis.com/gcm/send
-d "{\"registration_ids\":[\"{YOUR_SUBSCRIPTION_IDS}\"]}"
{
...
"gcm_sender_id": "{YOUR_SENDER_ID}"
}