ariaNgNotificationService.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. (function () {
  2. 'use strict';
  3. angular.module('ariaNg').factory('ariaNgNotificationService', ['$window', 'Notification', 'ariaNgConstants', 'ariaNgCommonService', 'ariaNgStorageService', 'ariaNgLocalizationService', 'ariaNgLogService', 'ariaNgSettingService', 'ariaNgNativeElectronService', function ($window, Notification, ariaNgConstants, ariaNgCommonService, ariaNgStorageService, ariaNgLocalizationService, ariaNgLogService, ariaNgSettingService, ariaNgNativeElectronService) {
  4. var nativeNotificationPermission = 'granted';
  5. var isSupportBrowserNotification = true;
  6. var isBrowserNotifactionGranted = function (permission) {
  7. return permission === 'granted';
  8. };
  9. var getBrowserNotifactionPermission = function () {
  10. return nativeNotificationPermission;
  11. };
  12. var requestBrowserNotifactionPermission = function (callback) {
  13. var permission = nativeNotificationPermission;
  14. if (callback) {
  15. callback({
  16. granted: isBrowserNotifactionGranted(permission),
  17. permission: permission
  18. });
  19. }
  20. };
  21. var isReachBrowserNotificationFrequencyLimit = function () {
  22. if (!ariaNgSettingService.getBrowserNotificationFrequency() || ariaNgSettingService.getBrowserNotificationFrequency() === 'unlimited') {
  23. return false;
  24. }
  25. var lastNotifications = ariaNgStorageService.get(ariaNgConstants.browserNotificationHistoryStorageKey) || [];
  26. if (!angular.isArray(lastNotifications)) {
  27. return false;
  28. }
  29. if (lastNotifications.length < 1) {
  30. return false;
  31. }
  32. var oldestTime = null;
  33. var isReachLimit = false;
  34. if (ariaNgSettingService.getBrowserNotificationFrequency() === 'high') {
  35. if (lastNotifications.length < 10) {
  36. return false;
  37. }
  38. oldestTime = lastNotifications[lastNotifications.length - 10].time;
  39. isReachLimit = ariaNgCommonService.isUnixTimeAfter(oldestTime, '-1', 'minute');
  40. } else if (ariaNgSettingService.getBrowserNotificationFrequency() === 'middle') {
  41. oldestTime = lastNotifications[lastNotifications.length - 1].time;
  42. isReachLimit = ariaNgCommonService.isUnixTimeAfter(oldestTime, '-1', 'minute');
  43. } else if (ariaNgSettingService.getBrowserNotificationFrequency() === 'low') {
  44. oldestTime = lastNotifications[lastNotifications.length - 1].time;
  45. isReachLimit = ariaNgCommonService.isUnixTimeAfter(oldestTime, '-5', 'minute');
  46. }
  47. if (isReachLimit) {
  48. ariaNgLogService.debug('[ariaNgNotificationService.isReachBrowserNotificationFrequencyLimit] reach frequency limit'
  49. + (oldestTime ? ', the oldest time is ' + oldestTime : ''));
  50. }
  51. return isReachLimit;
  52. };
  53. var recordBrowserNotificationHistory = function () {
  54. if (!ariaNgSettingService.getBrowserNotificationFrequency() || ariaNgSettingService.getBrowserNotificationFrequency() === 'unlimited') {
  55. return;
  56. }
  57. var lastNotifications = ariaNgStorageService.get(ariaNgConstants.browserNotificationHistoryStorageKey) || [];
  58. if (!angular.isArray(lastNotifications)) {
  59. lastNotifications = [];
  60. }
  61. lastNotifications.push({
  62. time: ariaNgCommonService.getCurrentUnixTime()
  63. });
  64. if (lastNotifications.length > 10) {
  65. lastNotifications.splice(0, lastNotifications.length - 10);
  66. }
  67. ariaNgStorageService.set(ariaNgConstants.browserNotificationHistoryStorageKey, lastNotifications);
  68. };
  69. var showBrowserNotifaction = function (title, options) {
  70. if (isReachBrowserNotificationFrequencyLimit()) {
  71. return;
  72. }
  73. recordBrowserNotificationHistory();
  74. ariaNgNativeElectronService.showSystemNotification({
  75. title: title,
  76. body: options.body,
  77. silent: !!options.silent
  78. });
  79. };
  80. var notifyViaBrowser = function (title, content, options) {
  81. if (!options) {
  82. options = {};
  83. }
  84. options.body = content;
  85. if (!ariaNgSettingService.getBrowserNotificationSound()) {
  86. options.silent = true;
  87. }
  88. if (isSupportBrowserNotification && ariaNgSettingService.getBrowserNotification()) {
  89. showBrowserNotifaction(title, options);
  90. }
  91. };
  92. var notifyInPage = function (title, content, options) {
  93. if (!options) {
  94. options = {};
  95. }
  96. if (!content) {
  97. options.message = title;
  98. } else {
  99. options.title = title;
  100. options.message = content;
  101. }
  102. if (!options.type || !Notification[options.type]) {
  103. options.type = 'primary';
  104. }
  105. if (!options.positionY) {
  106. // AriaNg Native changes the notification position to right bottom of the page
  107. options.positionY = 'bottom';
  108. }
  109. return Notification[options.type](options);
  110. };
  111. return {
  112. isSupportBrowserNotification: function () {
  113. return isSupportBrowserNotification;
  114. },
  115. hasBrowserPermission: function () {
  116. if (!isSupportBrowserNotification) {
  117. return false;
  118. }
  119. return isBrowserNotifactionGranted(getBrowserNotifactionPermission());
  120. },
  121. requestBrowserPermission: function (callback) {
  122. if (!isSupportBrowserNotification) {
  123. return;
  124. }
  125. requestBrowserNotifactionPermission(function (result) {
  126. if (!result.granted) {
  127. ariaNgSettingService.setBrowserNotification(false);
  128. }
  129. if (callback) {
  130. callback(result);
  131. }
  132. });
  133. },
  134. notifyViaBrowser: function (title, content, options) {
  135. if (!options) {
  136. options = {};
  137. }
  138. if (title) {
  139. title = ariaNgLocalizationService.getLocalizedText(title, options.titleParams);
  140. }
  141. if (content) {
  142. content = ariaNgLocalizationService.getLocalizedText(content, options.contentParams);
  143. }
  144. return notifyViaBrowser(title, content, options);
  145. },
  146. notifyTaskComplete: function (task) {
  147. this.notifyViaBrowser('Download Completed', (task && task.taskName ? task.taskName : ''));
  148. },
  149. notifyBtTaskComplete: function (task) {
  150. this.notifyViaBrowser('BT Download Completed', (task && task.taskName ? task.taskName : ''));
  151. },
  152. notifyTaskError: function (task) {
  153. this.notifyViaBrowser('Download Error', (task && task.taskName ? task.taskName : ''));
  154. },
  155. notifyInPage: function (title, content, options) {
  156. if (!options) {
  157. options = {};
  158. }
  159. if (title) {
  160. title = ariaNgLocalizationService.getLocalizedText(title, options.titleParams);
  161. }
  162. if (content) {
  163. content = ariaNgLocalizationService.getLocalizedText(content, options.contentParams);
  164. if (options.contentPrefix) {
  165. content = options.contentPrefix + content;
  166. }
  167. }
  168. return notifyInPage(title, content, options);
  169. },
  170. clearNotificationInPage: function () {
  171. Notification.clearAll();
  172. }
  173. };
  174. }]);
  175. }());