2020-11-10 03:14:04 +08:00
|
|
|
import * as Links from 'Common/Links';
|
2022-02-17 16:36:29 +08:00
|
|
|
import { addObservablesTo } from 'External/ko';
|
2022-02-11 19:21:16 +08:00
|
|
|
import { fireEvent } from 'Common/Globals';
|
2020-11-10 03:14:04 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Might not work due to the new ServiceWorkerRegistration.showNotification
|
|
|
|
*/
|
2021-04-23 05:53:39 +08:00
|
|
|
const HTML5Notification = window.Notification,
|
2022-09-02 17:52:07 +08:00
|
|
|
HTML5NotificationStatus = () => HTML5Notification?.permission || 'denied',
|
2020-11-10 16:28:33 +08:00
|
|
|
NotificationsDenied = () => 'denied' === HTML5NotificationStatus(),
|
|
|
|
NotificationsGranted = () => 'granted' === HTML5NotificationStatus(),
|
2020-11-10 03:14:04 +08:00
|
|
|
dispatchMessage = data => {
|
|
|
|
focus();
|
2023-01-25 01:58:25 +08:00
|
|
|
if (data.folder && data.uid) {
|
2022-02-11 19:21:16 +08:00
|
|
|
fireEvent('mailbox.message.show', data);
|
2020-11-10 16:59:56 +08:00
|
|
|
} else if (data.Url) {
|
2022-03-03 23:28:05 +08:00
|
|
|
hasher.setHash(data.Url);
|
2020-11-10 03:14:04 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let DesktopNotifications = false,
|
|
|
|
WorkerNotifications = navigator.serviceWorker;
|
|
|
|
|
|
|
|
// Are Notifications supported in the service worker?
|
|
|
|
if (WorkerNotifications && ServiceWorkerRegistration && ServiceWorkerRegistration.prototype.showNotification) {
|
|
|
|
/* Listen for close requests from the ServiceWorker */
|
|
|
|
WorkerNotifications.addEventListener('message', event => {
|
|
|
|
const obj = JSON.parse(event.data);
|
2022-09-02 17:52:07 +08:00
|
|
|
'notificationclick' === obj?.action && dispatchMessage(obj.data);
|
2020-11-10 03:14:04 +08:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
WorkerNotifications = null;
|
2020-11-10 16:28:33 +08:00
|
|
|
console.log('ServiceWorker Notifications not supported');
|
2020-11-10 03:14:04 +08:00
|
|
|
}
|
2015-01-26 07:09:22 +08:00
|
|
|
|
2021-03-11 05:41:35 +08:00
|
|
|
export const NotificationUserStore = new class {
|
2016-08-17 06:01:20 +08:00
|
|
|
constructor() {
|
2021-11-30 17:19:43 +08:00
|
|
|
addObservablesTo(this, {
|
2022-06-08 00:46:06 +08:00
|
|
|
enabled: false,/*.extend({ notify: 'always' })*/
|
|
|
|
allowed: !NotificationsDenied()
|
2021-11-30 17:19:43 +08:00
|
|
|
});
|
2015-01-26 07:09:22 +08:00
|
|
|
|
2022-06-08 00:46:06 +08:00
|
|
|
this.enabled.subscribe(value => {
|
2020-11-10 03:14:04 +08:00
|
|
|
DesktopNotifications = !!value;
|
2020-11-10 16:28:33 +08:00
|
|
|
if (value && HTML5Notification && !NotificationsGranted()) {
|
2020-11-10 03:14:04 +08:00
|
|
|
HTML5Notification.requestPermission(() =>
|
2022-06-08 00:46:06 +08:00
|
|
|
this.allowed(!NotificationsDenied())
|
2020-11-10 03:14:04 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2015-01-27 05:06:00 +08:00
|
|
|
|
2020-11-10 03:14:04 +08:00
|
|
|
/**
|
|
|
|
* Used with DesktopNotifications setting
|
|
|
|
*/
|
2022-06-08 00:46:06 +08:00
|
|
|
display(title, text, messageData, imageSrc) {
|
2020-11-10 16:28:33 +08:00
|
|
|
if (DesktopNotifications && NotificationsGranted()) {
|
2020-11-10 03:14:04 +08:00
|
|
|
const options = {
|
|
|
|
body: text,
|
2023-02-03 00:19:06 +08:00
|
|
|
icon: imageSrc || Links.staticLink('images/icon-message-notification.png'),
|
2020-11-10 03:14:04 +08:00
|
|
|
data: messageData
|
|
|
|
};
|
2023-01-25 01:58:25 +08:00
|
|
|
if (messageData?.uid) {
|
|
|
|
options.tag = messageData.uid;
|
2020-11-10 16:28:33 +08:00
|
|
|
}
|
2020-11-10 03:14:04 +08:00
|
|
|
if (WorkerNotifications) {
|
|
|
|
// Service-Worker-Allowed HTTP header to allow the scope.
|
|
|
|
WorkerNotifications.register('/serviceworker.js')
|
2021-03-17 00:07:29 +08:00
|
|
|
// WorkerNotifications.register(Links.staticLink('js/serviceworker.js'), {scope:'/'})
|
2020-11-10 03:14:04 +08:00
|
|
|
.then(() =>
|
|
|
|
WorkerNotifications.ready.then(registration =>
|
|
|
|
/* Show the notification */
|
|
|
|
registration
|
|
|
|
.showNotification(title, options)
|
|
|
|
.then(() =>
|
|
|
|
registration.getNotifications().then((/*notifications*/) => {
|
|
|
|
/* Send an empty message so the Worker knows who the client is */
|
|
|
|
registration.active.postMessage('');
|
|
|
|
})
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.catch(e => console.error(e));
|
|
|
|
} else {
|
|
|
|
const notification = new HTML5Notification(title, options);
|
2022-09-02 17:52:07 +08:00
|
|
|
notification.show?.();
|
2020-11-10 03:14:04 +08:00
|
|
|
notification.onclick = messageData ? () => dispatchMessage(messageData) : null;
|
|
|
|
setTimeout(() => notification.close(), 7000);
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
|
|
|
}
|
2021-03-11 05:41:35 +08:00
|
|
|
};
|