-
Notifications
You must be signed in to change notification settings - Fork 9
/
push_notification.js.coffee
71 lines (57 loc) · 2.11 KB
/
push_notification.js.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
(($) ->
notify_methods =
# create notification
create_notification: (options) ->
new Notification(options.title, options)
# close notification
close_notification: (notification, options) ->
setTimeout(notification.close.bind(notification), options.closeTime)
set_default_icon: (icon_url) ->
default_options.icon = icon_url
#return true if NotificationAPI is supported in your browser
isSupported: () ->
if ("Notification" of window) and (Notification.permission isnt "denied")
return true
else
return false
permission_request: () ->
if Notification.permission is "default"
Notification.requestPermission()
$.extend
notify: (body, arguments_options) ->
throw "Notification: few arguments" if arguments.length < 1
throw "Notification: body must 'String'" if typeof body != 'string'
# set default options
default_options =
'title' : "Notification"
'body' : "Body"
'closeTime' : 5000
# merge default_options and arguments_options to options
default_options.body = body
options = $.extend default_options, arguments_options
if notify_methods.isSupported()
notify_methods.permission_request()
notification = notify_methods.create_notification(options)
notify_methods.close_notification(notification, options)
# **callback functions**
# callback after notification is clicked
click : (callback) ->
notification.addEventListener 'click', ->
callback()
return this
# callback after notification is shown
show : (callback) ->
notification.addEventListener 'show', ->
callback()
return this
# callback after notification is displaied
close: (callback) ->
notification.addEventListener 'close', ->
callback()
return this
# callback after notification raises errors
error : (callback) ->
notification.addEventListener 'error', ->
callback()
return this
) jQuery