aboutsummaryrefslogtreecommitdiffstats
path: root/src/webview/notifications.js
blob: 4055b10de8d814cba7af3cddb2d8e829a8978909 (plain) (blame)
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
const { ipcRenderer } = require('electron');
const uuidV1 = require('uuid/v1');

class Notification {
  static permission = 'granted';

  constructor(title = '', options = {}) {
    this.title = title;
    this.options = options;
    this.notificationId = uuidV1();

    ipcRenderer.sendToHost('notification', this.onNotify({
      notificationId: this.notificationId,
      title,
      options,
    }));

    ipcRenderer.once(`notification-onclick:${this.notificationId}`, () => {
      this.onclick();
    });
  }

  static requestPermission(cb = null) {
    if (!cb) {
      return new Promise((resolve) => {
        resolve(Notification.permission);
      });
    }

    if (typeof (cb) === 'function') {
      return cb(Notification.permission);
    }

    return Notification.permission;
  }

  onNotify(data) {
    return data;
  }

  onClick() {}

  close() {}
}

window.Notification = Notification;