aboutsummaryrefslogtreecommitdiff
path: root/src/services/desktop_notification_utils/desktop_notification_utils.js
blob: eb58f39bb89535130e949159b374f9a8ac7284f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import { showDesktopNotification as swDesktopNotification, isSWSupported } from '../sw/sw.js'
const state = { failCreateNotif: false }

export const showDesktopNotification = (rootState, desktopNotificationOpts) => {
  if (!('Notification' in window && window.Notification.permission === 'granted')) return
  if (rootState.statuses.notifications.desktopNotificationSilence) { return }

  if (isSWSupported()) {
    swDesktopNotification(desktopNotificationOpts)
  } else if (!state.failCreateNotif) {
    try {
      const desktopNotification = new window.Notification(desktopNotificationOpts.title, desktopNotificationOpts)
      setTimeout(desktopNotification.close.bind(desktopNotification), 5000)
    } catch {
      state.failCreateNotif = true
    }
  }
}