forked from nylas/nylas-mail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnative-notifications.es6
58 lines (55 loc) · 1.64 KB
/
native-notifications.es6
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* eslint global-require: 0 */
let MacNotifierNotification = null;
if (process.platform === 'darwin') {
try {
MacNotifierNotification = require('node-mac-notifier');
} catch (err) {
console.error("node-mac-notifier (a platform-specific optionalDependency) was not installed correctly! Check the Travis build log for errors.")
}
}
class NativeNotifications {
constructor() {
if (MacNotifierNotification) {
this._macNotificationsByTag = {};
NylasEnv.onBeforeUnload(() => {
Object.keys(this._macNotificationsByTag).forEach((key) => {
this._macNotificationsByTag[key].close();
});
return true;
});
}
}
displayNotification({title, subtitle, body, tag, canReply, onActivate} = {}) {
let notif = null;
if (MacNotifierNotification) {
if (tag && this._macNotificationsByTag[tag]) {
this._macNotificationsByTag[tag].close();
}
notif = new MacNotifierNotification(title, {
bundleId: 'com.nylas.nylas-mail',
canReply: canReply,
subtitle: subtitle,
body: body,
id: tag,
});
notif.addEventListener('reply', ({response}) => {
onActivate({response, activationType: 'replied'});
});
notif.addEventListener('click', () => {
onActivate({response: null, activationType: 'clicked'});
});
if (tag) {
this._macNotificationsByTag[tag] = notif;
}
} else {
notif = new Notification(title, {
silent: true,
body: subtitle,
tag: tag,
});
notif.onclick = onActivate;
}
return notif;
}
}
export default new NativeNotifications()