2019-12-26 22:10:19 +05:30
|
|
|
/* eslint-disable consistent-return, no-return-assign */
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
function notificationGranted(message, opts, onclick) {
|
2019-12-26 22:10:19 +05:30
|
|
|
const notification = new Notification(message, opts);
|
2019-12-21 20:55:43 +05:30
|
|
|
setTimeout(
|
|
|
|
() =>
|
|
|
|
// Hide the notification after X amount of seconds
|
|
|
|
notification.close(),
|
|
|
|
8000,
|
|
|
|
);
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
return (notification.onclick = onclick || notification.close);
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
function notifyPermissions() {
|
2019-09-04 21:01:54 +05:30
|
|
|
/* eslint-disable-next-line @gitlab/i18n/no-non-i18n-strings */
|
2017-08-17 22:00:37 +05:30
|
|
|
if ('Notification' in window) {
|
|
|
|
return Notification.requestPermission();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function notifyMe(message, body, icon, onclick) {
|
2019-12-26 22:10:19 +05:30
|
|
|
const opts = {
|
2019-12-04 20:38:33 +05:30
|
|
|
body,
|
|
|
|
icon,
|
2017-08-17 22:00:37 +05:30
|
|
|
};
|
|
|
|
// Let's check if the browser supports notifications
|
2019-09-04 21:01:54 +05:30
|
|
|
/* eslint-disable-next-line @gitlab/i18n/no-non-i18n-strings */
|
2017-08-17 22:00:37 +05:30
|
|
|
if (!('Notification' in window)) {
|
|
|
|
// do nothing
|
|
|
|
} else if (Notification.permission === 'granted') {
|
|
|
|
// If it's okay let's create a notification
|
|
|
|
return notificationGranted(message, opts, onclick);
|
|
|
|
} else if (Notification.permission !== 'denied') {
|
2019-12-21 20:55:43 +05:30
|
|
|
return Notification.requestPermission(permission => {
|
2017-08-17 22:00:37 +05:30
|
|
|
// If the user accepts, let's create a notification
|
|
|
|
if (permission === 'granted') {
|
2016-09-13 17:45:13 +05:30
|
|
|
return notificationGranted(message, opts, onclick);
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const notify = {
|
|
|
|
notificationGranted,
|
|
|
|
notifyPermissions,
|
|
|
|
notifyMe,
|
|
|
|
};
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
export default notify;
|