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 |
7 onmessage = event => { | 8 // If true this service worker will show a notification when a push message is |
8 messagePort = event.data; | 9 // received. |
9 messagePort.postMessage('ready'); | 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 |
| 23 self.onmessage = event => { |
| 24 if (event.data instanceof MessagePort) { |
| 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 self.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 |