commit e9eeea48e5ea76214971daa1a879b0ae2ba1a196
parent 36ec13d4285b1d8633cc3bddd26df8fddbb8e59e
Author: RobinLinus <robinlinus@users.noreply.github.com>
Date: Fri, 21 Sep 2018 18:54:52 +0200
Fix notifications on android
Diffstat:
2 files changed, 20 insertions(+), 6 deletions(-)
diff --git a/client/scripts/network.js b/client/scripts/network.js
@@ -3,9 +3,11 @@ class ServerConnection {
constructor() {
this._connect();
Events.on('beforeunload', e => this._disconnect(), false);
+ Events.on('pageshow', e => this._connect(), false);
}
_connect() {
+ if (this._isConnected()) return
const ws = new WebSocket(this._endpoint());
ws.binaryType = 'arraybuffer';
ws.onopen = e => console.log('WS: server connection opened');
@@ -16,6 +18,10 @@ class ServerConnection {
clearTimeout(this._reconnectTimer);
}
+ _isConnected(){
+ return this._socket && this._socket.readyState === this._socket.OPEN;
+ }
+
_onMessage(msg) {
msg = JSON.parse(msg);
console.log('WS:', msg);
diff --git a/client/scripts/ui.js b/client/scripts/ui.js
@@ -358,12 +358,16 @@ class Notifications {
}
_notify(message, body) {
- var img = '/images/logo_transparent_128x128.png';
- return new Notification(message, {
+ const config = {
body: body,
- icon: img,
+ icon: '/images/logo_transparent_128x128.png',
vibrate: [200, 100, 200, 100, 200, 100, 400],
- });
+ }
+ if (serviceWorker && serviceWorker.showNotification) {
+ return serviceWorker.showNotification(message, config);
+ } else {
+ return new Notification(message, config);
+ }
}
_messageNotification(message) {
@@ -434,10 +438,14 @@ document.copy = text => {
return success;
}
-if ('serviceWorker' in navigator && isProductionEnvironment) {
+
+if ('serviceWorker' in navigator) {
navigator.serviceWorker
.register('/service-worker.js')
- .then(e => console.log("Service Worker Registered"));
+ .then(serviceWorker => {
+ console.log('Service Worker registered');
+ window.serviceWorker = serviceWorker
+ });
}
// Background Animation