From 85a7d986b0134976b929f186981089dfac681832 Mon Sep 17 00:00:00 2001 From: paulincai Date: Mon, 18 May 2020 01:14:18 +0400 Subject: [PATCH] Do not register a service worker for FB inapp browser. Also for web, refresh all tabs when a SW is updated --- lib/client/web.js | 15 ++++++++++++++- package.js | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/client/web.js b/lib/client/web.js index b3f4054..30e11d8 100644 --- a/lib/client/web.js +++ b/lib/client/web.js @@ -51,13 +51,26 @@ class WebPushHandle extends EventState { this.messaging = webApp.messaging() this.messaging.usePublicVapidKey(configuration.publicVapidKey) - if ('serviceWorker' in navigator) { + const isFacebookApp = () => { + const ua = navigator.userAgent || navigator.vendor || window.opera + return (ua.indexOf('FBAN') > -1) || (ua.indexOf('FBAV') > -1) + } + + if ('serviceWorker' in navigator && !isFacebookApp()) { window.addEventListener('load', async () => { const registration = await navigator.serviceWorker.register('/firebase-messaging-sw.js', { updateViaCache: 'none' }) this.messaging.useServiceWorker(registration) + // Refresh all tabs after a service worker update takes place + let refreshing + navigator.serviceWorker.addEventListener('controllerchange', () => { + if (refreshing) { return } + refreshing = true + window.location.reload() + }) + /** * Listen for the foreground messages. Background messages will be dealt with by the worker */ diff --git a/package.js b/package.js index 9ce2e5a..cf6ba60 100755 --- a/package.js +++ b/package.js @@ -1,7 +1,7 @@ /* globals Package, Npm, Cordova */ Package.describe({ name: 'activitree:push', - version: '2.0.4', + version: '2.0.5', summary: 'Push Notifications for Cordova and Web/PWA with Firebase (FCM).', git: 'https://github.com/activitree/meteor-push.git' })