-
-
Notifications
You must be signed in to change notification settings - Fork 645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement service worker for offline notifications #480
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
var gotifyKey = undefined | ||
|
||
self.addEventListener("install", event => { | ||
event.waitUntil(new Promise((resolve, reject) => { | ||
try { | ||
// resolves install promise only if search param 'key' was provided | ||
gotifyKey = new URL(location).searchParams.get('key'); | ||
} catch (e) { | ||
reject(e) | ||
} | ||
if (!gotifyKey) { | ||
reject("gotify-login-key not provided") | ||
} | ||
console.log("Worker recieved gotify-login-key, successfully installed") | ||
resolve() | ||
})) | ||
}) | ||
|
||
self.addEventListener("activate", () => { | ||
|
||
console.log("Notification worker activated") | ||
|
||
const host = location.host | ||
const wsProto = location.protocol === "https:" ? "wss:" : "ws:" | ||
const ws = new WebSocket(`${wsProto}//${host}/stream?token=${gotifyKey}`) | ||
console.log("Notification worker connected to websocket, waiting for messages") | ||
|
||
ws.onmessage = (event) => { | ||
|
||
// check if any client is currently visible | ||
// if so, skip sending notification from worker | ||
self.clients.matchAll({ | ||
type: "window", | ||
includeUncontrolled: true | ||
}) | ||
.then((windowClients) => { | ||
var clientVisible = false | ||
for (var i = 0; i < windowClients.length; i++) { | ||
const windowClient = windowClients[i] | ||
// check if a client is visible, then break | ||
if (windowClient.visibilityState === "visible") { | ||
clientVisible = true | ||
break | ||
} | ||
} | ||
return clientVisible | ||
}) // use the bool to evaluate whether to send a notification | ||
.then((clientVisible) => { | ||
if (!clientVisible) { | ||
var msgObj = JSON.parse(event.data) // parse event data, only if not visible | ||
self.registration.showNotification("WORKER: " + msgObj.title, { | ||
body: msgObj.message | ||
}) | ||
} else { | ||
console.log("not sending worker notification, as gotify window is visible") | ||
} | ||
}) | ||
|
||
} | ||
|
||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ import 'typeface-roboto'; | |
import {initAxios} from './apiAuth'; | ||
import * as config from './config'; | ||
import Layout from './layout/Layout'; | ||
import {unregister} from './registerServiceWorker'; | ||
import {registerNotificationWorker} from './registerServiceWorker'; | ||
import {CurrentUser} from './CurrentUser'; | ||
import {AppStore} from './application/AppStore'; | ||
import {WebSocketStore} from './message/WebSocketStore'; | ||
|
@@ -61,7 +61,14 @@ const initStores = (): StoreMapping => { | |
|
||
registerReactions(stores); | ||
|
||
stores.currentUser.tryAuthenticate().catch(() => {}); | ||
stores.currentUser.tryAuthenticate().then(() => { | ||
// always request notification permission when logged in | ||
Notification.requestPermission() | ||
.then(perm => console.log("Notification permissions " + perm)) | ||
.catch(console.error) | ||
// reregister worker | ||
registerNotificationWorker(stores.currentUser.token()); | ||
}).catch(() => {}); | ||
Comment on lines
+64
to
+71
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add another button under the enable notifications button, something like "enable background notifications". Should probably only visible if the notification permission was given. Gotify must not request this permission without user interaction, see #264 The button should be a toggle, enabling and disabling the notifications. |
||
|
||
window.onbeforeunload = () => { | ||
stores.wsStore.close(); | ||
|
@@ -73,5 +80,5 @@ const initStores = (): StoreMapping => { | |
</InjectProvider>, | ||
document.getElementById('root') | ||
); | ||
unregister(); | ||
//unregister(); | ||
})(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably need some reconnect logic here, otherwise the connection can break in the background and then the user doesn't get notifications anymore.