NOTES 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. NOTES:
  2. ========================================================================================================================
  3. W3C Specification:
  4. http://dvcs.w3.org/hg/notifications/raw-file/tip/Overview.html
  5. ========================================================================================================================
  6. Safari:
  7. - 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.
  8. Notifications are dispatched by the WebKit Notification object and follow the implementation outlined by the W3C specification.
  9. Safari Notification documentation:
  10. https://developer.apple.com/library/mac/#documentation/AppleApplications/Conceptual/SafariJSProgTopics/Articles/SendingNotifications.html#//apple_ref/doc/uid/TP40001483-CH23-SW1
  11. W3C Notification documentation:
  12. http://dev.w3.org/2006/webapi/WebNotifications/publish/Notifications.html
  13. - Script could not be run in strict mode - some of the Safari native code is written using with expression,
  14. that is not allowed in strict mode.
  15. - Calling 'requestPermission' in Safari requires a callback function parameter, otherwise it throws an exception
  16. 'TypeError: Not enough arguments'.
  17. ========================================================================================================================
  18. Chrome:
  19. - Chrome does not display notification if both title and body are not set.
  20. ========================================================================================================================
  21. <html data-ng-app>
  22. <head>
  23. <title></title>
  24. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
  25. <script>
  26. /*
  27. Do not strict mode - Safari 6 throws an error that "with" statement
  28. is not allowed in strict mode
  29. */
  30. //"use strict"
  31. (function(win) {
  32. var notify = ((win.Notification && win.Notification.permissionLevel) ?
  33. win.Notification : win.webkitNotifications),
  34. //Chrome style permission levels
  35. PERMISSION_ALLOWED = 0,
  36. PERMISSION_NOT_ALLOWED = 1,
  37. PERMISSION_DISALLOWED = 2,
  38. //Safari style permission levels
  39. PERMISSION_DEFAULT = "default",
  40. PERMISSION_GRANTED = "granted",
  41. PERMISSION_DENIED = "denied",
  42. NOTIFICATION_PROPERTIES = "ondisplay|onerror|onclick".split("|"),
  43. createNotification = function(details) {
  44. var notification;
  45. if (hasPermission()) {
  46. notification = notify.createNotification(details.image || "", details.title || "", details.body || ""),
  47. i = 0, length = NOTIFICATION_PROPERTIES.length;
  48. for (i; i < length; i++) {
  49. if (notification.hasOwnProperty(NOTIFICATION_PROPERTIES[i])) {
  50. notification[NOTIFICATION_PROPERTIES[i]] = details[NOTIFICATION_PROPERTIES[i]];
  51. }
  52. }
  53. }
  54. return notification;
  55. },
  56. getPermission = function() {
  57. var permissionLevel = ((notify.permissionLevel && notify.permissionLevel()) ||
  58. (notify.checkPermission && notify.checkPermission())),
  59. permission;
  60. switch (permissionLevel) {
  61. case PERMISSION_DEFAULT:
  62. case PERMISSION_NOT_ALLOWED:
  63. permission = PERMISSION_NOT_ALLOWED;
  64. break;
  65. case PERMISSION_GRANTED:
  66. case PERMISSION_ALLOWED:
  67. permission = PERMISSION_ALLOWED;
  68. break;
  69. case PERMISSION_DENIED:
  70. case PERMISSION_DISALLOWED:
  71. permission = PERMISSION_DISALLOWED;
  72. break;
  73. }
  74. return permission;
  75. },
  76. hasPermission = function() {
  77. var hasPermission;
  78. if (notify.permissionLevel) {
  79. hasPermission = (notify.permissionLevel() === PERMISSION_GRANTED);
  80. } else if (notify.checkPermission) {
  81. hasPermission = !notify.checkPermission();
  82. }
  83. return hasPermission;
  84. },
  85. requestPermission = function(callback) {
  86. /*
  87. Safari 6 requires a callback function otherwise it
  88. throws an error. For chrome, the callback function is an optional.
  89. */
  90. notify.requestPermission(callback)
  91. };
  92. win.notify = {
  93. /**
  94. *
  95. * @param notification
  96. */
  97. showNotification: function(/* object */ details, /* boolean */show) {
  98. var notification = createNotification(details);
  99. if (notification) {
  100. if (show) {
  101. notification.show();
  102. }
  103. }
  104. return notification;
  105. },
  106. createHTMLNotification: function() {
  107. },
  108. /**
  109. *
  110. * @return {*}
  111. */
  112. getPermission: function() {
  113. return getPermission();
  114. },
  115. /**
  116. *
  117. * @return {*}
  118. */
  119. hasPermission: function() {
  120. return hasPermission();
  121. },
  122. /**
  123. *
  124. */
  125. requestPermission: function(callback) {
  126. requestPermission(callback);
  127. }
  128. }
  129. }(window));
  130. </script>
  131. <script>
  132. function NotificationCenter($scope) {
  133. $scope.permissionLevel = notify.getPermission();
  134. $scope.callback = function() {
  135. console.log("test");
  136. }
  137. $scope.requestPermissions = function() {
  138. if (!notify.hasPermission()) {
  139. notify.requestPermission(function() {
  140. $scope.$apply($scope.permissionLevel = notify.getPermission());
  141. });
  142. }
  143. }
  144. }
  145. </script>
  146. </head>
  147. <body data-ng-controller="NotificationCenter">
  148. <a data-ng-click="requestPermissions()" data-ng-pluralize data-count="permissionLevel" when="{
  149. '0': 'Notifications allowed.',
  150. '1': 'Notifications not allowed.',
  151. '2': 'Notifications denied.',
  152. 'other': 'Permission level could not be detected'}">
  153. </a>
  154. </body>
  155. </html>