Use service worker to display notifications if available
This commit is contained in:
parent
da5a5c7175
commit
8791a17fc4
@ -6,6 +6,7 @@ const render = require("../render");
|
|||||||
const utils = require("../utils");
|
const utils = require("../utils");
|
||||||
const options = require("../options");
|
const options = require("../options");
|
||||||
const helpers_roundBadgeNumber = require("../libs/handlebars/roundBadgeNumber");
|
const helpers_roundBadgeNumber = require("../libs/handlebars/roundBadgeNumber");
|
||||||
|
const webpush = require("../webpush");
|
||||||
const chat = $("#chat");
|
const chat = $("#chat");
|
||||||
const sidebar = $("#sidebar");
|
const sidebar = $("#sidebar");
|
||||||
|
|
||||||
@ -131,16 +132,30 @@ function notifyMessage(targetId, channel, msg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const notify = new Notification(title, {
|
if (webpush.hasServiceWorker) {
|
||||||
|
navigator.serviceWorker.ready.then((registration) => {
|
||||||
|
registration.active.postMessage({
|
||||||
|
type: "notification",
|
||||||
|
chanId: targetId,
|
||||||
|
timestamp: msg.time,
|
||||||
|
title: title,
|
||||||
body: body,
|
body: body,
|
||||||
icon: "img/logo-64.png",
|
});
|
||||||
tag: `lounge-${targetId}`,
|
});
|
||||||
|
} else {
|
||||||
|
const notify = new Notification(title, {
|
||||||
|
tag: `chan-${targetId}`,
|
||||||
|
badge: "img/logo-64.png",
|
||||||
|
icon: "img/touch-icon-192x192.png",
|
||||||
|
body: body,
|
||||||
|
timestamp: msg.time,
|
||||||
});
|
});
|
||||||
notify.addEventListener("click", function() {
|
notify.addEventListener("click", function() {
|
||||||
window.focus();
|
window.focus();
|
||||||
button.click();
|
button.click();
|
||||||
this.close();
|
this.close();
|
||||||
});
|
});
|
||||||
|
}
|
||||||
} catch (exception) {
|
} catch (exception) {
|
||||||
// `new Notification(...)` is not supported and should be silenced.
|
// `new Notification(...)` is not supported and should be silenced.
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,8 @@ const pushNotificationsButton = $("#pushNotifications");
|
|||||||
let clientSubscribed = null;
|
let clientSubscribed = null;
|
||||||
let applicationServerKey;
|
let applicationServerKey;
|
||||||
|
|
||||||
|
module.exports.hasServiceWorker = false;
|
||||||
|
|
||||||
module.exports.configurePushNotifications = (subscribedOnServer, key) => {
|
module.exports.configurePushNotifications = (subscribedOnServer, key) => {
|
||||||
applicationServerKey = key;
|
applicationServerKey = key;
|
||||||
|
|
||||||
@ -16,7 +18,7 @@ module.exports.configurePushNotifications = (subscribedOnServer, key) => {
|
|||||||
if (clientSubscribed === true && subscribedOnServer === false) {
|
if (clientSubscribed === true && subscribedOnServer === false) {
|
||||||
pushNotificationsButton.attr("disabled", true);
|
pushNotificationsButton.attr("disabled", true);
|
||||||
|
|
||||||
navigator.serviceWorker.register("service-worker.js")
|
navigator.serviceWorker.ready
|
||||||
.then((registration) => registration.pushManager.getSubscription())
|
.then((registration) => registration.pushManager.getSubscription())
|
||||||
.then((subscription) => subscription && subscription.unsubscribe())
|
.then((subscription) => subscription && subscription.unsubscribe())
|
||||||
.then((successful) => {
|
.then((successful) => {
|
||||||
@ -32,6 +34,8 @@ if (isAllowedServiceWorkersHost()) {
|
|||||||
|
|
||||||
if ("serviceWorker" in navigator) {
|
if ("serviceWorker" in navigator) {
|
||||||
navigator.serviceWorker.register("service-worker.js").then((registration) => {
|
navigator.serviceWorker.register("service-worker.js").then((registration) => {
|
||||||
|
module.exports.hasServiceWorker = true;
|
||||||
|
|
||||||
if (!registration.pushManager) {
|
if (!registration.pushManager) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -58,7 +62,7 @@ if (isAllowedServiceWorkersHost()) {
|
|||||||
function onPushButton() {
|
function onPushButton() {
|
||||||
pushNotificationsButton.attr("disabled", true);
|
pushNotificationsButton.attr("disabled", true);
|
||||||
|
|
||||||
navigator.serviceWorker.register("service-worker.js").then((registration) => {
|
navigator.serviceWorker.ready.then((registration) => {
|
||||||
registration.pushManager.getSubscription().then((existingSubscription) => {
|
registration.pushManager.getSubscription().then((existingSubscription) => {
|
||||||
if (existingSubscription) {
|
if (existingSubscription) {
|
||||||
socket.emit("push:unregister");
|
socket.emit("push:unregister");
|
||||||
|
@ -2,13 +2,19 @@
|
|||||||
/* global clients */
|
/* global clients */
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
|
self.addEventListener("message", function(event) {
|
||||||
|
showNotification(event, event.data);
|
||||||
|
});
|
||||||
|
|
||||||
self.addEventListener("push", function(event) {
|
self.addEventListener("push", function(event) {
|
||||||
if (!event.data) {
|
if (!event.data) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const payload = event.data.json();
|
showNotification(event, event.data.json());
|
||||||
|
});
|
||||||
|
|
||||||
|
function showNotification(event, payload) {
|
||||||
if (payload.type !== "notification") {
|
if (payload.type !== "notification") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -33,7 +39,7 @@ self.addEventListener("push", function(event) {
|
|||||||
});
|
});
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
});
|
}
|
||||||
|
|
||||||
self.addEventListener("notificationclick", function(event) {
|
self.addEventListener("notificationclick", function(event) {
|
||||||
event.notification.close();
|
event.notification.close();
|
||||||
|
Loading…
Reference in New Issue
Block a user