2016-08-17 06:01:20 +08:00
|
|
|
import ko from 'ko';
|
2015-01-26 07:09:22 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
import Audio from 'Common/Audio';
|
2020-11-10 03:14:04 +08:00
|
|
|
import * as Links from 'Common/Links';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Might not work due to the new ServiceWorkerRegistration.showNotification
|
|
|
|
*/
|
|
|
|
const HTML5Notification = window.Notification ? Notification : null,
|
|
|
|
HTML5NotificationStatus = () => (HTML5Notification && HTML5Notification.permission) || 'denied',
|
|
|
|
dispatchMessage = data => {
|
|
|
|
focus();
|
|
|
|
if (data.Folder && data.Uid) {
|
|
|
|
dispatchEvent(new CustomEvent('mailbox.message.show', {detail:data}));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let DesktopNotifications = false,
|
|
|
|
WorkerNotifications = navigator.serviceWorker;
|
|
|
|
|
|
|
|
// Are Notifications supported in the service worker?
|
|
|
|
if (WorkerNotifications && ServiceWorkerRegistration && ServiceWorkerRegistration.prototype.showNotification) {
|
|
|
|
console.log('ServiceWorker supported');
|
|
|
|
/* Listen for close requests from the ServiceWorker */
|
|
|
|
WorkerNotifications.addEventListener('message', event => {
|
|
|
|
const obj = JSON.parse(event.data);
|
|
|
|
obj && 'notificationclick' === obj.action && dispatchMessage(obj.data);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
WorkerNotifications = null;
|
|
|
|
console.log('WorkerNotifications not supported');
|
|
|
|
}
|
2015-01-26 07:09:22 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
class NotificationUserStore {
|
2016-08-17 06:01:20 +08:00
|
|
|
constructor() {
|
|
|
|
this.enableSoundNotification = ko.observable(false);
|
2019-07-05 03:19:24 +08:00
|
|
|
|
2020-11-10 03:14:04 +08:00
|
|
|
this.enableDesktopNotification = ko.observable(false)/*.extend({ notify: 'always' })*/;
|
2019-07-05 03:19:24 +08:00
|
|
|
|
2020-11-10 03:14:04 +08:00
|
|
|
this.isDesktopNotificationDenied = ko.observable('denied' === HTML5NotificationStatus());
|
2015-01-26 07:09:22 +08:00
|
|
|
|
2020-11-10 03:14:04 +08:00
|
|
|
this.enableDesktopNotification.subscribe(value => {
|
|
|
|
DesktopNotifications = !!value;
|
|
|
|
if (value && HTML5Notification && 'granted' !== HTML5Notification.permission) {
|
|
|
|
HTML5Notification.requestPermission(() =>
|
|
|
|
this.isDesktopNotificationDenied('denied' === HTML5Notification.permission)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
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 SoundNotification setting
|
|
|
|
*/
|
2016-08-17 06:01:20 +08:00
|
|
|
playSoundNotification(skipSetting) {
|
2020-11-10 03:14:04 +08:00
|
|
|
if (skipSetting ? true : this.enableSoundNotification()) {
|
2016-08-17 06:01:20 +08:00
|
|
|
Audio.playNotification();
|
|
|
|
}
|
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
|
|
|
|
*/
|
|
|
|
displayDesktopNotification(title, text, messageData, imageSrc) {
|
|
|
|
if (DesktopNotifications && 'granted' === HTML5NotificationStatus()) {
|
|
|
|
const options = {
|
|
|
|
body: text,
|
|
|
|
icon: imageSrc || Links.notificationMailIcon(),
|
|
|
|
data: messageData
|
|
|
|
};
|
|
|
|
if (WorkerNotifications) {
|
|
|
|
// Service-Worker-Allowed HTTP header to allow the scope.
|
|
|
|
WorkerNotifications.register('/serviceworker.js')
|
|
|
|
// WorkerNotifications.register(Links.staticPrefix('js/serviceworker.js'), {scope:'/'})
|
|
|
|
.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);
|
|
|
|
notification.show && notification.show();
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
populate() {
|
2020-09-04 18:05:17 +08:00
|
|
|
this.enableSoundNotification(!!rl.settings.get('SoundNotification'));
|
|
|
|
this.enableDesktopNotification(!!rl.settings.get('DesktopNotifications'));
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-09-13 04:50:21 +08:00
|
|
|
export default new NotificationUserStore();
|