| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 this.onpush = function(event) { | 5 this.onpush = function(event) { |
| 6 // TODO(peter): Remove this check once Blink supports PushMessageData.json(). | 6 var data = event.data.text(); |
| 7 var data = event.data; | |
| 8 if (typeof data !== 'string') | |
| 9 data = data.text(); | |
| 10 | |
| 11 if (data !== 'shownotification') { | 7 if (data !== 'shownotification') { |
| 12 sendMessageToClients('push', data); | 8 sendMessageToClients('push', data); |
| 13 return; | 9 return; |
| 14 } | 10 } |
| 15 | 11 |
| 16 // TODO(peter): Switch to self.registration.showNotification once implemented. | 12 event.waitUntil(registration.showNotification('Push test title', { |
| 17 event.waitUntil(showLegacyNonPersistentNotification('Push test title', { | |
| 18 body: 'Push test body', | 13 body: 'Push test body', |
| 19 tag: 'push_test_tag' | 14 tag: 'push_test_tag' |
| 20 }).then(function(notification) { | 15 }).then(function(notification) { |
| 21 sendMessageToClients('push', data); | 16 sendMessageToClients('push', data); |
| 22 }, function(ex) { | 17 }, function(ex) { |
| 23 sendMessageToClients('push', String(ex)); | 18 sendMessageToClients('push', String(ex)); |
| 24 })); | 19 })); |
| 25 }; | 20 }; |
| 26 | 21 |
| 27 function sendMessageToClients(type, data) { | 22 function sendMessageToClients(type, data) { |
| 28 var message = JSON.stringify({ | 23 var message = JSON.stringify({ |
| 29 'type': type, | 24 'type': type, |
| 30 'data': data | 25 'data': data |
| 31 }); | 26 }); |
| 32 clients.getAll().then(function(clients) { | 27 clients.getAll().then(function(clients) { |
| 33 clients.forEach(function(client) { | 28 clients.forEach(function(client) { |
| 34 client.postMessage(message); | 29 client.postMessage(message); |
| 35 }); | 30 }); |
| 36 }, function(error) { | 31 }, function(error) { |
| 37 console.log(error); | 32 console.log(error); |
| 38 }); | 33 }); |
| 39 } | 34 } |
| 40 | |
| 41 function showLegacyNonPersistentNotification(title, options) { | |
| 42 return new Promise(function(resolve, reject) { | |
| 43 var notification = new Notification(title, options); | |
| 44 notification.onshow = function() { resolve(notification); }; | |
| 45 notification.onerror = function() { reject(new Error('Failed to show')); }; | |
| 46 }); | |
| 47 } | |
| OLD | NEW |