mirror of
https://github.com/Crazyco-xyz/48hr.email.git
synced 2025-12-16 14:56:31 +01:00
Fix notifications
This commit is contained in:
parent
2902d0fcc5
commit
9538e7be7a
1 changed files with 40 additions and 40 deletions
|
|
@ -2,56 +2,56 @@
|
||||||
/* eslint no-undef: 0 */
|
/* eslint no-undef: 0 */
|
||||||
|
|
||||||
function showNewMailsNotification(address, reloadPage) {
|
function showNewMailsNotification(address, reloadPage) {
|
||||||
// We want the page to be reloaded. But then when clicking the notification, it can not find the tab and will open a new one.
|
// We want the page to be reloaded. But then when clicking the notification, it can not find the tab and will open a new one.
|
||||||
|
|
||||||
const notification = new Notification(address, {
|
const notification = new Notification(address, {
|
||||||
body: 'You have new messages',
|
body: 'You have new messages',
|
||||||
icon: '/images/logo.png',
|
icon: '/images/logo.png',
|
||||||
tag: '48hr-email-replace-notification',
|
tag: '48hr-email-replace-notification',
|
||||||
renotify: true
|
renotify: true
|
||||||
})
|
})
|
||||||
notification.addEventListener('click', event => {
|
notification.addEventListener('click', event => {
|
||||||
// TODO: does not work after reloading the page, see #1
|
event.preventDefault()
|
||||||
event.preventDefault()
|
window.focus()
|
||||||
})
|
})
|
||||||
|
|
||||||
if (reloadPage) {
|
if (reloadPage) {
|
||||||
location.reload()
|
location.reload()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function enableNewMessageNotifications(address, reloadPage) {
|
function enableNewMessageNotifications(address, reloadPage) {
|
||||||
enableNotifications()
|
enableNotifications()
|
||||||
const socket = io()
|
const socket = io()
|
||||||
socket.emit('sign in', address)
|
socket.emit('sign in', address)
|
||||||
|
|
||||||
socket.on('reconnect', () => {
|
socket.on('reconnect', () => {
|
||||||
socket.emit('sign in', address)
|
socket.emit('sign in', address)
|
||||||
})
|
})
|
||||||
socket.on('new emails', () => {
|
socket.on('new emails', () => {
|
||||||
showNewMailsNotification(address, reloadPage)
|
showNewMailsNotification(address, reloadPage)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function enableNotifications() {
|
function enableNotifications() {
|
||||||
// Let's check if the browser supports notifications
|
// Let's check if the browser supports notifications
|
||||||
if (!('Notification' in window)) {
|
if (!('Notification' in window)) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Let's check whether notification permissions have already been granted
|
// Let's check whether notification permissions have already been granted
|
||||||
if (Notification.permission === 'granted') {
|
if (Notification.permission === 'granted') {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, we need to ask the user for permission
|
// Otherwise, we need to ask the user for permission
|
||||||
if (Notification.permission !== 'denied') {
|
if (Notification.permission !== 'denied') {
|
||||||
Notification.requestPermission(permission => {
|
Notification.requestPermission(permission => {
|
||||||
// If the user accepts, let's create a notification
|
// If the user accepts, let's create a notification
|
||||||
return permission === 'granted'
|
return permission === 'granted'
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finally, if the user has denied notifications and you
|
// Finally, if the user has denied notifications and you
|
||||||
// want to be respectful there is no need to bother them any more.
|
// want to be respectful there is no need to bother them any more.
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue