/ src / main / utils / notification.ts
notification.ts
 1  import { Notification } from 'electron'
 2  import Constants from './Constants'
 3  
 4  export interface NotificationOptions {
 5    title?: string
 6    body: string
 7    silent?: boolean
 8    icon?: string
 9  }
10  
11  export interface NotificationEvents {
12    onClick?: () => void
13    onShow?: () => void
14    onClose?: () => void
15  }
16  
17  export function showNotification(
18    options: NotificationOptions,
19    events?: NotificationEvents
20  ): boolean {
21    if (!Notification.isSupported()) {
22      return false
23    }
24  
25    try {
26      const notification = new Notification({
27        title: options.title ?? Constants.APP_NAME,
28        body: options.body,
29        silent: options.silent ?? false,
30        icon: options.icon ?? Constants.ASSETS_PATH.icon_raw
31      })
32  
33      if (events?.onClick) {
34        notification.on('click', events.onClick)
35      }
36  
37      if (events?.onShow) {
38        notification.on('show', events.onShow)
39      }
40  
41      if (events?.onClose) {
42        notification.on('close', events.onClose)
43      }
44  
45      notification.show()
46      return true
47    } catch (error) {
48      console.error('Failed to show notification:', error)
49      return false
50    }
51  }
52  
53  export function isNotificationSupported(): boolean {
54    return Notification.isSupported()
55  }