forked from ttsvetko/HTML5-Desktop-Notifications
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NOTES
190 lines (155 loc) · 7.4 KB
/
NOTES
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
NOTES:
========================================================================================================================
W3C Specification:
http://dvcs.w3.org/hg/notifications/raw-file/tip/Overview.html
========================================================================================================================
Safari:
- As of OS X 10.8 Mountain Lion, web pages in Safari can post notifications to the system wide notification system known as Notification Center.
Notifications are dispatched by the WebKit Notification object and follow the implementation outlined by the W3C specification.
Safari Notification documentation:
https://developer.apple.com/library/mac/#documentation/AppleApplications/Conceptual/SafariJSProgTopics/Articles/SendingNotifications.html#//apple_ref/doc/uid/TP40001483-CH23-SW1
W3C Notification documentation:
http://dev.w3.org/2006/webapi/WebNotifications/publish/Notifications.html
- Script could not be run in strict mode - some of the Safari native code is written using with expression,
that is not allowed in strict mode.
- Calling 'requestPermission' in Safari requires a callback function parameter, otherwise it throws an exception
'TypeError: Not enough arguments'.
========================================================================================================================
Chrome:
- Chrome does not display notification if both title and body are not set.
========================================================================================================================
<html data-ng-app>
<head>
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
<script>
/*
Do not strict mode - Safari 6 throws an error that "with" statement
is not allowed in strict mode
*/
//"use strict"
(function(win) {
var notify = ((win.Notification && win.Notification.permissionLevel) ?
win.Notification : win.webkitNotifications),
//Chrome style permission levels
PERMISSION_ALLOWED = 0,
PERMISSION_NOT_ALLOWED = 1,
PERMISSION_DISALLOWED = 2,
//Safari style permission levels
PERMISSION_DEFAULT = "default",
PERMISSION_GRANTED = "granted",
PERMISSION_DENIED = "denied",
NOTIFICATION_PROPERTIES = "ondisplay|onerror|onclick".split("|"),
createNotification = function(details) {
var notification;
if (hasPermission()) {
notification = notify.createNotification(details.image || "", details.title || "", details.body || ""),
i = 0, length = NOTIFICATION_PROPERTIES.length;
for (i; i < length; i++) {
if (notification.hasOwnProperty(NOTIFICATION_PROPERTIES[i])) {
notification[NOTIFICATION_PROPERTIES[i]] = details[NOTIFICATION_PROPERTIES[i]];
}
}
}
return notification;
},
getPermission = function() {
var permissionLevel = ((notify.permissionLevel && notify.permissionLevel()) ||
(notify.checkPermission && notify.checkPermission())),
permission;
switch (permissionLevel) {
case PERMISSION_DEFAULT:
case PERMISSION_NOT_ALLOWED:
permission = PERMISSION_NOT_ALLOWED;
break;
case PERMISSION_GRANTED:
case PERMISSION_ALLOWED:
permission = PERMISSION_ALLOWED;
break;
case PERMISSION_DENIED:
case PERMISSION_DISALLOWED:
permission = PERMISSION_DISALLOWED;
break;
}
return permission;
},
hasPermission = function() {
var hasPermission;
if (notify.permissionLevel) {
hasPermission = (notify.permissionLevel() === PERMISSION_GRANTED);
} else if (notify.checkPermission) {
hasPermission = !notify.checkPermission();
}
return hasPermission;
},
requestPermission = function(callback) {
/*
Safari 6 requires a callback function otherwise it
throws an error. For chrome, the callback function is an optional.
*/
notify.requestPermission(callback)
};
win.notify = {
/**
*
* @param notification
*/
showNotification: function(/* object */ details, /* boolean */show) {
var notification = createNotification(details);
if (notification) {
if (show) {
notification.show();
}
}
return notification;
},
createHTMLNotification: function() {
},
/**
*
* @return {*}
*/
getPermission: function() {
return getPermission();
},
/**
*
* @return {*}
*/
hasPermission: function() {
return hasPermission();
},
/**
*
*/
requestPermission: function(callback) {
requestPermission(callback);
}
}
}(window));
</script>
<script>
function NotificationCenter($scope) {
$scope.permissionLevel = notify.getPermission();
$scope.callback = function() {
console.log("test");
}
$scope.requestPermissions = function() {
if (!notify.hasPermission()) {
notify.requestPermission(function() {
$scope.$apply($scope.permissionLevel = notify.getPermission());
});
}
}
}
</script>
</head>
<body data-ng-controller="NotificationCenter">
<a data-ng-click="requestPermissions()" data-ng-pluralize data-count="permissionLevel" when="{
'0': 'Notifications allowed.',
'1': 'Notifications not allowed.',
'2': 'Notifications denied.',
'other': 'Permission level could not be detected'}">
</a>
</body>
</html>