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
3 // found in the LICENSE file.
5 this.onpush = function(event) {
6 // TODO(peter): Remove this check once Blink supports PushMessageData.json().
8 if (typeof data !== 'string')
11 if (data !== 'shownotification') {
12 sendMessageToClients('push', data);
16 // TODO(peter): Switch to self.registration.showNotification once implemented.
17 event.waitUntil(showLegacyNonPersistentNotification('Push test title', {
18 body: 'Push test body',
20 }).then(function(notification) {
21 sendMessageToClients('push', data);
23 sendMessageToClients('push', String(ex));
27 function sendMessageToClients(type, data) {
28 var message = JSON.stringify({
32 clients.getAll().then(function(clients) {
33 clients.forEach(function(client) {
34 client.postMessage(message);
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')); };