From 21e05c2706f959d4709818b3d670d5109a72e422 Mon Sep 17 00:00:00 2001 From: Greg Fodor <gfodor@gmail.com> Date: Thu, 18 Oct 2018 05:58:34 +0000 Subject: [PATCH] Set up push notification --- src/hub.js | 7 +++++-- src/hub.service.js | 37 +++++++++++++++++++++++++++++---- src/react-components/ui-root.js | 2 +- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/hub.js b/src/hub.js index 50dcd8aa9..8470ce891 100644 --- a/src/hub.js +++ b/src/hub.js @@ -311,8 +311,11 @@ document.addEventListener("DOMContentLoaded", async () => { console.log(`Hub ID: ${hubId}`); const subscriptions = new Subscriptions(hubId); - navigator.serviceWorker.register("/hub.service.js"); - navigator.serviceWorker.ready.then(registration => subscriptions.setRegistration(registration)); + + if (navigator.serviceWorker) { + navigator.serviceWorker.register("/hub.service.js"); + navigator.serviceWorker.ready.then(registration => subscriptions.setRegistration(registration)); + } const scene = document.querySelector("a-scene"); const hubChannel = new HubChannel(store); diff --git a/src/hub.service.js b/src/hub.service.js index d039f7290..6208f8620 100644 --- a/src/hub.service.js +++ b/src/hub.service.js @@ -1,10 +1,39 @@ self.addEventListener("install", function(e) { - e.waitUntil(self.skipWaiting()); + return e.waitUntil(self.skipWaiting()); }); + self.addEventListener("activate", function(e) { - e.waitUntil(self.clients.claim()); + return e.waitUntil(self.clients.claim()); +}); + +self.addEventListener("push", function(e) { + const payload = JSON.parse(e.data.text()); + + return e.waitUntil( + self.registration.showNotification("Hubs by Mozilla", { + body: "Someone has joined " + payload.hub_name, + image: payload.image, + icon: "/favicon.ico", + badge: "/favicon.ico", + tag: payload.hub_id, + data: { hub_url: payload.hub_url } + }) + ); }); -self.addEventListener("push", function() {}); +self.addEventListener("notificationclick", function(e) { + e.notification.close(); + + e.waitUntil( + self.clients.matchAll({ type: "window" }).then(function(clientList) { + for (let i = 0; i < clientList.length; i++) { + const client = clientList[i]; + if (client.url.indexOf(e.notification.data.hub_url) >= 0 && "focus" in client) return client.focus(); + } -self.addEventListener("notificationclick", function() {}); + if (self.clients.openWindow) { + return self.clients.openWindow(e.notification.data.hub_url); + } + }) + ); +}); diff --git a/src/react-components/ui-root.js b/src/react-components/ui-root.js index 0c85be53d..57de5be3f 100644 --- a/src/react-components/ui-root.js +++ b/src/react-components/ui-root.js @@ -687,7 +687,7 @@ class UIRoot extends Component { }; renderEntryStartPanel = () => { - const hasPush = "PushManager" in window; + const hasPush = navigator.serviceWorker && "PushManager" in window; return ( <div className={entryStyles.entryPanel}> -- GitLab