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 push messages received. | |
13 var pushCounter = 0; | |
14 | |
15 // The number of notifications shown. | |
16 var notificationCounter = 0; | |
17 | |
7 onmessage = event => { | 18 onmessage = event => { |
8 messagePort = event.data; | 19 if (event.data instanceof MessagePort) { |
9 messagePort.postMessage('ready'); | 20 messagePort = event.data; |
21 messagePort.postMessage('ready'); | |
22 return; | |
23 } | |
24 | |
25 var message = JSON.parse(event.data); | |
26 if (message.type == 'setNotifyOnPush') { | |
27 notifyOnPush = message.data; | |
28 sendToTest('setNotifyOnPush ok'); | |
29 return; | |
30 } | |
31 | |
32 sendToTest('Unknown message type.'); | |
10 }; | 33 }; |
11 | 34 |
12 onpush = event => | 35 onpush = event => { |
13 event.waitUntil(registration.showNotification('push notification')); | 36 pushCounter++; |
37 if (notifyOnPush) { | |
38 notificationCounter++; | |
39 event.waitUntil(registration.showNotification( | |
40 'push notification ' + notificationCounter)); | |
41 } | |
42 sendToTest('push ' + pushCounter + ' ok'); | |
johnme
2015/12/08 17:12:31
Nit: when notifyOnPush is true, consider waiting f
Michael van Ouwerkerk
2015/12/09 15:02:52
Obsolete as this no longer relies on setting the t
| |
43 }; | |
44 | |
45 function sendToTest(message) { | |
johnme
2015/12/08 17:12:31
Nit: maybe put this above onmessage?
Michael van Ouwerkerk
2015/12/09 15:02:52
Done.
| |
46 messagePort.postMessage(JSON.stringify({ | |
47 'type': 'sendToTest', | |
48 'data': message | |
49 })); | |
50 } | |
OLD | NEW |