OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // The MessagePort to communicate with the client. | |
5 var messagePort = null; | 6 var messagePort = null; |
6 | 7 |
8 // If true this service worker will show a notification when a push message is | |
9 // received. | |
10 var notifyOnPush = true; | |
11 | |
12 // The number of notifications shown. | |
13 var notificationCounter = 0; | |
14 | |
15 // Sends a message to the test, via the page. | |
16 function sendToTest(message) { | |
17 messagePort.postMessage(JSON.stringify({ | |
18 'type': 'sendToTest', | |
19 'data': message | |
20 })); | |
21 } | |
22 | |
7 onmessage = event => { | 23 onmessage = event => { |
Bernhard Bauer
2015/12/11 12:28:42
Nit: Use window.onmessage to make the scope of the
Michael van Ouwerkerk
2015/12/11 16:05:56
This is service worker code, there is no window ob
Bernhard Bauer
2015/12/11 16:35:57
Oh, right. See, all the more reason to make the sc
Michael van Ouwerkerk
2015/12/11 17:57:02
Acknowledged.
| |
8 messagePort = event.data; | 24 if (event.data instanceof MessagePort) { |
9 messagePort.postMessage('ready'); | 25 messagePort = event.data; |
26 messagePort.postMessage('ready'); | |
27 return; | |
28 } | |
29 | |
30 var message = JSON.parse(event.data); | |
31 if (message.type == 'setNotifyOnPush') { | |
32 notifyOnPush = message.data; | |
33 sendToTest('setNotifyOnPush ' + message.data + ' ok'); | |
34 return; | |
35 } | |
36 | |
37 sendToTest('Unknown message type.'); | |
10 }; | 38 }; |
11 | 39 |
12 onpush = event => | 40 onpush = event => { |
13 event.waitUntil(registration.showNotification('push notification')); | 41 if (notifyOnPush) { |
42 notificationCounter++; | |
43 event.waitUntil(registration.showNotification( | |
44 'push notification ' + notificationCounter)); | |
45 } | |
46 }; | |
OLD | NEW |