Unregister from GCM when the only GCM app is removed
[chromium-blink-merge.git] / chrome / test / data / push_messaging / service_worker.js
blob3633517343532b8355f70ac6a531a641c5cd041c
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().
7 var data = event.data;
8 if (typeof data !== 'string')
9 data = data.text();
11 if (data !== 'shownotification') {
12 sendMessageToClients('push', data);
13 return;
16 // TODO(peter): Switch to self.registration.showNotification once implemented.
17 event.waitUntil(showLegacyNonPersistentNotification('Push test title', {
18 body: 'Push test body',
19 tag: 'push_test_tag'
20 }).then(function(notification) {
21 sendMessageToClients('push', data);
22 }, function(ex) {
23 sendMessageToClients('push', String(ex));
24 }));
27 function sendMessageToClients(type, data) {
28 var message = JSON.stringify({
29 'type': type,
30 'data': data
31 });
32 clients.getAll().then(function(clients) {
33 clients.forEach(function(client) {
34 client.postMessage(message);
35 });
36 }, function(error) {
37 console.log(error);
38 });
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 });