Web notification improvements

This commit is contained in:
10G Meow 2023-10-01 14:31:00 +03:00
parent 7ed3e80b9c
commit 13ef686e36
4 changed files with 61 additions and 46 deletions

View file

@ -0,0 +1,22 @@
// Notification click event listener
self.addEventListener("notificationclick", (e) => {
// Close the notification popout
e.notification.close();
// Get all the Window clients
e.waitUntil(
clients.matchAll({ type: "window" }).then((clientsArr) => {
// If a Window tab matching the targeted URL already exists, focus that;
const hadWindowToFocus = clientsArr.some((windowClient) =>
windowClient.url === e.notification.data.url
? (windowClient.focus(), true)
: false,
);
// Otherwise, open a new tab to the applicable URL and focus it.
if (!hadWindowToFocus)
clients
.openWindow(e.notification.data.url)
.then((windowClient) => (windowClient ? windowClient.focus() : null));
}),
);
});

View file

@ -31,10 +31,12 @@
<script> <script>
import stickers from "./plugins/stickers"; import stickers from "./plugins/stickers";
import { notificationCount } from "./plugins/notificationAndServiceWorker.js" import { registerServiceWorker, notificationCount, windowNotificationPermission } from "./plugins/notificationAndServiceWorker.js"
import logoMixin from "./components/logoMixin";
export default { export default {
name: "App", name: "App",
mixins: [logoMixin],
data() { data() {
return { return {
loading: true, loading: true,
@ -46,6 +48,7 @@ export default {
this.setDefaultLanguage(); this.setDefaultLanguage();
}, },
mounted() { mounted() {
registerServiceWorker();
/** /**
if ( if (
window.location.protocol == "http" && window.location.protocol == "http" &&
@ -81,6 +84,7 @@ export default {
this.$config.promise.then(this.onConfigLoaded); this.$config.promise.then(this.onConfigLoaded);
}, },
methods: { methods: {
windowNotificationPermission,
onConfigLoaded(config) { onConfigLoaded(config) {
if (config.shortCodeStickers) { if (config.shortCodeStickers) {
stickers.loadStickersFromConfig(config); stickers.loadStickersFromConfig(config);
@ -112,7 +116,20 @@ export default {
// Set language // Set language
this.$i18n.locale = this.$store.state.language || "en"; this.$i18n.locale = this.$store.state.language || "en";
} },
showNotification() {
if(document.visibilityState === "hidden") {
const title = this.$t('notification.title');
const self = this;
navigator.serviceWorker.ready.then(function(registration) {
registration.showNotification(title, {
icon: self.logotype,
data: { url: window.location.href }
});
});
}
},
}, },
computed: { computed: {
notificationCount, notificationCount,
@ -157,6 +174,15 @@ export default {
}, },
}, },
watch: { watch: {
notificationCount: {
handler(nCount) {
// windowNotificationPermission
// return value: 'granted', 'default', 'denied'
if (nCount > 0 && this.windowNotificationPermission() === "granted") {
this.showNotification()
}
}
},
"$i18n.locale": { "$i18n.locale": {
handler(val) { handler(val) {
// Locale changed, check file if RTL // Locale changed, check file if RTL

View file

@ -346,7 +346,7 @@
depressed depressed
block block
class="filled-button" class="filled-button"
@click.stop="onRequestNotificationAndServiceWorker" @click.stop="onNotifyRequest"
>{{ $t("notification.dialog.enable") }}</v-btn >{{ $t("notification.dialog.enable") }}</v-btn
> >
</v-col> </v-col>
@ -379,8 +379,7 @@ import chatMixin from "./chatMixin";
import sendAttachmentsMixin from "./sendAttachmentsMixin"; import sendAttachmentsMixin from "./sendAttachmentsMixin";
import AudioLayout from "./AudioLayout.vue"; import AudioLayout from "./AudioLayout.vue";
import FileDropLayout from "./file_mode/FileDropLayout"; import FileDropLayout from "./file_mode/FileDropLayout";
import { requestNotificationAndServiceWorker, windowNotificationPermission, notificationCount } from "../plugins/notificationAndServiceWorker.js" import { requestNotificationPermission, windowNotificationPermission } from "../plugins/notificationAndServiceWorker.js"
import logoMixin from "./logoMixin";
import roomTypeMixin from "./roomTypeMixin"; import roomTypeMixin from "./roomTypeMixin";
import roomMembersMixin from "./roomMembersMixin"; import roomMembersMixin from "./roomMembersMixin";
@ -418,7 +417,7 @@ ScrollPosition.prototype.prepareFor = function (direction) {
export default { export default {
name: "Chat", name: "Chat",
mixins: [chatMixin, logoMixin, roomTypeMixin, sendAttachmentsMixin, roomMembersMixin], mixins: [chatMixin, roomTypeMixin, sendAttachmentsMixin, roomMembersMixin],
components: { components: {
ChatHeader, ChatHeader,
ChatHeaderPrivate, ChatHeaderPrivate,
@ -554,7 +553,6 @@ export default {
}, },
computed: { computed: {
notificationCount,
nonImageFiles() { nonImageFiles() {
return this.isCurrentFileInputsAnArray && this.currentFileInputs.filter(file => !file?.type.includes("image/")) return this.isCurrentFileInputsAnArray && this.currentFileInputs.filter(file => !file?.type.includes("image/"))
}, },
@ -742,13 +740,6 @@ export default {
}, },
watch: { watch: {
notificationCount: {
handler(nCount) {
if (nCount > 0 && this.windowNotificationPermission() === "granted") {
this.showNotification()
}
}
},
initialLoadDone: { initialLoadDone: {
immediate: true, immediate: true,
handler(value, oldValue) { handler(value, oldValue) {
@ -841,22 +832,6 @@ export default {
methods: { methods: {
windowNotificationPermission, windowNotificationPermission,
showNotification() {
if(document.visibilityState === "visible") {
return;
}
const title = this.$t('notification.title');
const self = this;
navigator.serviceWorker.ready.then(function(registration) {
registration.showNotification(title, {
icon: self.logotype,
tag: "new-message-notification",
});
});
},
onNotificationDialog() { onNotificationDialog() {
if(this.windowNotificationPermission() === 'denied') { if(this.windowNotificationPermission() === 'denied') {
alert(this.$t("notification.blocked_message")); alert(this.$t("notification.blocked_message"));
@ -864,8 +839,8 @@ export default {
this.notificationDialog = true; this.notificationDialog = true;
} }
}, },
onRequestNotificationAndServiceWorker() { onNotifyRequest() {
requestNotificationAndServiceWorker() requestNotificationPermission()
this.notificationDialog = false; this.notificationDialog = false;
}, },
onRoomJoined(initialEventId) { onRoomJoined(initialEventId) {

View file

@ -1,23 +1,15 @@
const registerServiceWorker = async () => { export function registerServiceWorker() {
const swRegistration = await navigator.serviceWorker.register("/sw.js");
return swRegistration;
};
const requestNotificationPermission = async () => {
// return value: 'granted', 'default', 'denied'
return await window.Notification.requestPermission();
};
export async function requestNotificationAndServiceWorker() {
if (!("serviceWorker" in navigator)) { if (!("serviceWorker" in navigator)) {
throw new Error("No Service Worker support!"); throw new Error("No Service Worker support!");
} }
navigator.serviceWorker.register("/sw.js");
}
export function requestNotificationPermission() {
if (!("PushManager" in window)) { if (!("PushManager" in window)) {
throw new Error("No Push API Support!"); throw new Error("No Push API Support!");
} }
const permission = await requestNotificationPermission(); window.Notification.requestPermission();
if(permission==='granted') await registerServiceWorker();
return permission
} }
export function windowNotificationPermission() { export function windowNotificationPermission() {