- From: Jake Archibald <notifications@github.com>
- Date: Mon, 15 Dec 2014 04:10:55 -0800
- To: slightlyoff/ServiceWorker <ServiceWorker@noreply.github.com>
- Message-ID: <slightlyoff/ServiceWorker/issues/588/66985639@github.com>
Before I respond to specific points, I really should have provided example usage code:
Responding to a push message:
```js
self.addEventListener('push', function(event) {
if (event.message.data == 'new-chat-message') {
fetch('/latest-messages').then(r => r.json()).then(function(data) {
clients.getAll().then(function(clients) {
// filter clients to relevent urls
clients = clients.filter(c => new URL(c.url).pathname.indexof('/chat/') === 0);
// tell them about the new messages
clients.forEach(c => c.postMessage(data));
// if we don't have a focused & visible window, show notification
if (!clients.find(c => !c.hidden && c.focused)) {
registration.showNotification(data[0].username, {
body: data[0].message,
icon: data[0].avatarURL,
tag: 'chat-' + data[0].username
});
}
});
});
}
});
```
Responding to a notification activation:
```js
self.addEventListener('notificationclick', function(event) {
// chat notification clicked
if (event.notification.tag.indexOf('chat-') === 0) {
clients.getAll().then(function(clients) {
// look for relevent window, or make a new one
var client = clients.find(c => new URL(c.url).pathname.indexof('/chat/') === 0)
|| new Client('/chat/');
// focus it
client.focus();
});
}
};
```
---
Reply to this email directly or view it on GitHub:
https://github.com/slightlyoff/ServiceWorker/issues/588#issuecomment-66985639
Received on Monday, 15 December 2014 12:11:21 UTC