This repository has been archived by the owner on Oct 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
92 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,91 @@ | ||
/* | ||
* Push notificiations ServiceWorker | ||
* | ||
* NOTE(@mxstbr): This only gets updated if you change the filename, not if you | ||
* update the contents. Make sure to change the version number in the filename | ||
* after updating the code! | ||
*/ | ||
|
||
// A new notification is coming in, yay! | ||
self.addEventListener('push', function(event) { | ||
var notificationData = {}; | ||
|
||
try { | ||
notificationData = event.data.json(); | ||
} catch (e) { | ||
console.log('event.data.json() failed', e); | ||
// 🚨 We either got no data or it's malformatted, ABORT ABORT ABORT | ||
return; | ||
} | ||
|
||
// Check if the user is looking at Spectrum right now and don't show a notification | ||
event.waitUntil( | ||
clients | ||
.matchAll({ | ||
type: 'window', | ||
includeUncontrolled: true, | ||
}) | ||
.then(windowClients => { | ||
for (let i = 0; i < windowClients.length; i++) { | ||
const windowClient = windowClients[i]; | ||
// The user is looking at Spectrum right now abort showing the notification! | ||
// (except for if we're on localhost, i.e. in development) | ||
if ( | ||
windowClient.focused && | ||
!(self.registration.scope.indexOf('http://localhost:3000') === 0) | ||
) { | ||
return; | ||
} | ||
} | ||
return self.registration.showNotification(notificationData.title, { | ||
vibrate: [200], | ||
icon: '/img/apple-icon-144x144-precomposed.png', | ||
badge: '/img/badge.png', | ||
body: notificationData.body, | ||
title: notificationData.title, | ||
timestamp: notificationData.timestamp, | ||
image: notificationData.image, | ||
tag: notificationData.tag, | ||
data: notificationData.data, | ||
// If we don't set a tag and set renotify to true this'll throw an error | ||
// renotify: notificationData.renotify || !!notificationData.tag, | ||
}); | ||
}) | ||
); | ||
}); | ||
|
||
// On notification click | ||
self.addEventListener('notificationclick', function(event) { | ||
event.notification.close(); | ||
|
||
const urlToOpen = | ||
event.notification.data && event.notification.data.href | ||
? new URL(event.notification.data.href, self.location.origin).href | ||
: '/'; | ||
|
||
// see if the current is open and if it is focus it | ||
event.waitUntil( | ||
self.clients | ||
.matchAll({ | ||
type: 'window', | ||
includeUncontrolled: true, | ||
}) | ||
.then(function(clientList) { | ||
// If there is an open Spectrum.chat window navigate to the notification href | ||
if (clientList.length > 0) { | ||
return clientList[0] | ||
.focus() | ||
.then(client => client.navigate(urlToOpen)); | ||
} | ||
// If there's no open Spectrum.chat window open a new one | ||
return self.clients.openWindow(urlToOpen); | ||
}) | ||
); | ||
}); | ||
/* | ||
* Push notificiations ServiceWorker | ||
* | ||
* NOTE(@mxstbr): This only gets updated if you change the filename, not if you | ||
* update the contents. Make sure to change the version number in the filename | ||
* after updating the code! | ||
*/ | ||
|
||
// A new notification is coming in, yay! | ||
// eslint-disable-next-line | ||
self.addEventListener('push', function(event) { | ||
var notificationData = {}; | ||
|
||
try { | ||
notificationData = event.data.json(); | ||
} catch (e) { | ||
console.log('event.data.json() failed', e); | ||
// 🚨 We either got no data or it's malformatted, ABORT ABORT ABORT | ||
return; | ||
} | ||
|
||
// Check if the user is looking at Spectrum right now and don't show a notification | ||
event.waitUntil( | ||
clients | ||
.matchAll({ | ||
type: 'window', | ||
includeUncontrolled: true, | ||
}) | ||
.then(windowClients => { | ||
for (let i = 0; i < windowClients.length; i++) { | ||
const windowClient = windowClients[i]; | ||
// The user is looking at Spectrum right now abort showing the notification! | ||
// (except for if we're on localhost, i.e. in development) | ||
if ( | ||
windowClient.focused && | ||
// eslint-disable-next-line | ||
!(self.registration.scope.indexOf('http://localhost:3000') === 0) | ||
) { | ||
return; | ||
} | ||
} | ||
// eslint-disable-next-line | ||
return self.registration.showNotification(notificationData.title, { | ||
vibrate: [200], | ||
icon: '/img/apple-icon-144x144-precomposed.png', | ||
badge: '/img/badge.png', | ||
body: notificationData.body, | ||
title: notificationData.title, | ||
timestamp: notificationData.timestamp, | ||
image: notificationData.image, | ||
tag: notificationData.tag, | ||
data: notificationData.data, | ||
// If we don't set a tag and set renotify to true this'll throw an error | ||
// renotify: notificationData.renotify || !!notificationData.tag, | ||
}); | ||
}) | ||
); | ||
}); | ||
|
||
// On notification click | ||
// eslint-disable-next-line | ||
self.addEventListener('notificationclick', function(event) { | ||
event.notification.close(); | ||
|
||
const urlToOpen = | ||
event.notification.data && event.notification.data.href | ||
? // eslint-disable-next-line | ||
new URL(event.notification.data.href, self.location.origin).href | ||
: '/'; | ||
|
||
// see if the current is open and if it is focus it | ||
event.waitUntil( | ||
// eslint-disable-next-line | ||
self.clients | ||
.matchAll({ | ||
type: 'window', | ||
includeUncontrolled: true, | ||
}) | ||
.then(function(clientList) { | ||
// If there is an open Spectrum.chat window navigate to the notification href | ||
if (clientList.length > 0) { | ||
return clientList[0] | ||
.focus() | ||
.then(client => client.navigate(urlToOpen)); | ||
} | ||
// If there's no open Spectrum.chat window open a new one | ||
// eslint-disable-next-line | ||
return self.clients.openWindow(urlToOpen); | ||
}) | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters