2016-01-25 21:20:24 +00:00
|
|
|
((w) ->
|
2016-03-18 11:08:03 +00:00
|
|
|
notificationGranted = (message, opts, onclick) ->
|
|
|
|
notification = new Notification(message, opts)
|
|
|
|
|
2016-04-06 14:06:27 +00:00
|
|
|
# Hide the notification after X amount of seconds
|
|
|
|
setTimeout ->
|
|
|
|
notification.close()
|
|
|
|
, 8000
|
|
|
|
|
2016-03-18 11:08:03 +00:00
|
|
|
if onclick
|
|
|
|
notification.onclick = onclick
|
|
|
|
|
2016-03-16 14:31:35 +00:00
|
|
|
notifyPermissions = ->
|
|
|
|
if 'Notification' of window
|
|
|
|
Notification.requestPermission()
|
|
|
|
|
|
|
|
notifyMe = (message, body, icon, onclick) ->
|
|
|
|
opts =
|
2016-01-25 21:20:24 +00:00
|
|
|
body: body
|
2016-03-03 23:02:18 +00:00
|
|
|
icon: icon
|
2016-01-25 21:20:24 +00:00
|
|
|
# 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
|
2016-03-18 11:08:03 +00:00
|
|
|
notificationGranted message, opts, onclick
|
2016-01-25 21:20:24 +00:00
|
|
|
else if Notification.permission != 'denied'
|
|
|
|
Notification.requestPermission (permission) ->
|
|
|
|
# If the user accepts, let's create a notification
|
|
|
|
if permission == 'granted'
|
2016-03-18 11:08:03 +00:00
|
|
|
notificationGranted message, opts, onclick
|
2016-01-25 21:20:24 +00:00
|
|
|
|
|
|
|
w.notify = notifyMe
|
2016-03-16 14:31:35 +00:00
|
|
|
w.notifyPermissions = notifyPermissions
|
2016-01-25 21:20:24 +00:00
|
|
|
) window
|