-
Notifications
You must be signed in to change notification settings - Fork 38
/
notificationsService.js
34 lines (31 loc) 路 1.24 KB
/
notificationsService.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import path from 'path';
import { ipcRenderer } from 'electron';
import { ipcConsts } from '/vars';
// @flow
class NotificationsService {
static notify = ({ title, notification, callback }: { title: string, notification: string, callback: () => void }) => {
NotificationsService.getNotificationAllowedStatus().then(({ isNotificationAllowed }: { isNotificationAllowed: boolean }) => {
if (isNotificationAllowed) {
const notificationOptions: any = {
body: notification,
icon: path.join(__dirname, '..', 'app', 'assets', 'icons', 'app_icon.png')
};
const desktopNotification = new Notification(title || 'Alert', notificationOptions);
desktopNotification.onclick = () => {
ipcRenderer.send(ipcConsts.NOTIFICATION_CLICK);
callback && callback();
};
}
});
};
static getNotificationAllowedStatus = async () => {
const isPermitted = await Notification.requestPermission();
ipcRenderer.send(ipcConsts.CAN_NOTIFY);
return new Promise((resolve) => {
ipcRenderer.once(ipcConsts.CAN_NOTIFY_SUCCESS, (event, isInFocus) => {
resolve({ isNotificationAllowed: isPermitted && !isInFocus });
});
});
};
}
export default NotificationsService;