Skip to content

Instantly share code, notes, and snippets.

@MatthewCallis
Created November 15, 2016 18:08
Show Gist options
  • Save MatthewCallis/9a7188c8380ce52145c51f17f5c84ddd to your computer and use it in GitHub Desktop.
Save MatthewCallis/9a7188c8380ce52145c51f17f5c84ddd to your computer and use it in GitHub Desktop.
Simple Browser Notifications
notify: (title, options) ->
options.icon = options.icon or "#{window.location.protocol}//#{window.location.hostname}#{$('meta#icon-120x120').attr('href')}"
# Check for notification compatibility, if browser version is unsupported, be silent.
return unless window.Notification
# If the user has not been asked to grant or deny notifications from this domain
if Notification.permission is 'default'
Notification.requestPermission ->
@notify(title, options)
# If the user has granted permission for this domain to send notifications.
else if Notification.permission is 'granted'
n = new Notification(title, options)
n.onclick = ->
options.onclick or @close()
n.onclose = ->
options.onclose
n.onshow = ->
options.onshow
n.onerror = ->
options.onerror
# If the user does not want notifications to come from this domain, be silent.
else
return if Notification.permission is 'denied'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment