Skip to content

Commit

Permalink
Do not register a service worker for FB inapp browser.
Browse files Browse the repository at this point in the history
Also for web, refresh all tabs when a SW is updated
  • Loading branch information
paulincai committed May 17, 2020
1 parent 3dd728d commit 85a7d98
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
15 changes: 14 additions & 1 deletion lib/client/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion package.js
Original file line number Diff line number Diff line change
@@ -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'
})
Expand Down

0 comments on commit 85a7d98

Please sign in to comment.