notify.js.coffee 970 Bytes
Newer Older
1
((w) ->
Phil Hughes's avatar
Phil Hughes committed
2 3 4 5 6
  notifyPermissions = ->
    if 'Notification' of window
      Notification.requestPermission()

  notifyMe = (message, body, icon, onclick) ->
7
    notification = undefined
Phil Hughes's avatar
Phil Hughes committed
8
    opts =
9
      body: body
10
      icon: icon
11 12 13 14 15 16
    # Let's check if the browser supports notifications
    if !('Notification' of window)
      # do nothing
    else if Notification.permission == 'granted'
      # If it's okay let's create a notification
      notification = new Notification(message, opts)
Phil Hughes's avatar
Phil Hughes committed
17 18 19

      if onclick
        notification.onclick = onclick
20 21 22 23 24
    else if Notification.permission != 'denied'
      Notification.requestPermission (permission) ->
        # If the user accepts, let's create a notification
        if permission == 'granted'
          notification = new Notification(message, opts)
Phil Hughes's avatar
Phil Hughes committed
25 26 27

          if onclick
            notification.onclick = onclick
28 29 30 31
        return
    return

  w.notify = notifyMe
Phil Hughes's avatar
Phil Hughes committed
32
  w.notifyPermissions = notifyPermissions
33 34
  return
) window