LocalNotificationCore.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. Copyright 2013-2015 appPlant UG
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing,
  12. software distributed under the License is distributed on an
  13. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. KIND, either express or implied. See the License for the
  15. specific language governing permissions and limitations
  16. under the License.
  17. */
  18. var Notifications = Windows.UI.Notifications,
  19. applicationData = Windows.Storage.ApplicationData.current,
  20. localSettings = applicationData.localSettings;
  21. exports.core = {
  22. /**
  23. * Executes all queued events.
  24. */
  25. deviceready: function () {
  26. var plugin = cordova.plugins.notification.local,
  27. events = this.eventQueue;
  28. this.isReady = true;
  29. for (var i = 0; i < events.length; i++) {
  30. plugin.fireEvent.apply(plugin, events[i]);
  31. }
  32. this.eventQueue = [];
  33. },
  34. /**
  35. * Schedules new local notifications.
  36. *
  37. * @param {Object[]} notifications
  38. * Array of local notifications
  39. * @param {String} event
  40. * 'schedule' or 'update'
  41. */
  42. schedule: function (notifications) {
  43. var triggerFn = function (notification) {
  44. this.updateBadge(notification.badge);
  45. this.fireEvent('trigger', notification);
  46. };
  47. for (var i = 0; i < notifications.length; i++) {
  48. var options = notifications[i],
  49. notification = this.build(options);
  50. this.cancelLocalNotification(options.id);
  51. this.scheduleLocalNotification(notification, options);
  52. this.scheduleBackupNotification(notification, options);
  53. this.fireEvent('schedule', options);
  54. this.callOnTrigger(options, triggerFn);
  55. }
  56. },
  57. /**
  58. * Schedules a single local notification.
  59. *
  60. * @param {Windows.Data.Xml.Dom.XmlDocument} notification
  61. * The local notification
  62. * @param {Object} options
  63. * Local notification properties
  64. */
  65. scheduleLocalNotification: function (notification, options) {
  66. var interval = this.getRepeatInterval(options.every),
  67. triggerTime = new Date((options.at * 1000)),
  68. now = new Date().getTime(),
  69. toast;
  70. if (triggerTime <= now) {
  71. triggerTime = new Date(now + 10);
  72. }
  73. try {
  74. if (interval !== 0 && interval < 360001 && interval > 59999) {
  75. toast = new Notifications.ScheduledToastNotification(
  76. notification, triggerTime, interval, 5);
  77. } else {
  78. toast = new Notifications.ScheduledToastNotification(
  79. notification, triggerTime);
  80. }
  81. } catch (e) {
  82. console.error(e);
  83. return;
  84. }
  85. toast.id = options.id;
  86. toast.tag = 'Toast' + toast.id;
  87. Notifications.ToastNotificationManager
  88. .createToastNotifier()
  89. .addToSchedule(toast);
  90. },
  91. /**
  92. * Schedules a backup local notification 10 years later.
  93. *
  94. * @param {Object} notification
  95. * The local notification
  96. */
  97. scheduleBackupNotification: function (notification, options) {
  98. var properties = Object.create(options);
  99. properties.id = options.id + '-2';
  100. properties.at = options.at + 315360000; // 10 years later
  101. this.scheduleLocalNotification(notification, properties);
  102. },
  103. /**
  104. * Updates the badge number of the active tile.
  105. *
  106. * @param {Number} badge
  107. * The badge number. Zero will clean the badge.
  108. */
  109. updateBadge: function (badge) {
  110. var notifications = Windows.UI.Notifications,
  111. type = notifications.BadgeTemplateType.badgeNumber,
  112. xml = notifications.BadgeUpdateManager.getTemplateContent(type),
  113. attrs = xml.getElementsByTagName('badge'),
  114. notification = new notifications.BadgeNotification(xml);
  115. attrs[0].setAttribute('value', badge);
  116. notifications.BadgeUpdateManager.createBadgeUpdaterForApplication()
  117. .update(notification);
  118. },
  119. /**
  120. * Updates existing notifications specified by IDs in options.
  121. *
  122. * @param {Object[]} notifications
  123. * Array of local notifications
  124. */
  125. update: function (notifications) {
  126. for (var i = 0; i < notifications.length; i++) {
  127. var updates = notifications[i],
  128. options = getAll(updates.id || '0')[0];
  129. this.updateLocalNotification(options, updates);
  130. this.fireEvent('update', options);
  131. }
  132. },
  133. /**
  134. * Updates a single local notification.
  135. *
  136. * @param {Object} notification
  137. * The local notification
  138. * @param {Object} updates
  139. * Updated properties
  140. */
  141. updateLocalNotification: function (notification, updates) {
  142. for (var key in updates) {
  143. notification[key] = updates[key];
  144. }
  145. this.cancelLocalNotification(notification.id);
  146. this.scheduleLocalNotification(notification);
  147. },
  148. /**
  149. * Clears the specified notifications.
  150. *
  151. * @param {int[]} ids
  152. * List of local notification IDs
  153. */
  154. clear: function (ids) {
  155. for (var i = 0; i < ids.length; i++) {
  156. var id = ids[i],
  157. notification = this.getAll([id])[0];
  158. this.clearLocalNotification(id);
  159. this.fireEvent('clear', notification);
  160. }
  161. },
  162. /**
  163. * Clears the local notification with the given ID.
  164. *
  165. * @param {String} id
  166. * Local notification ID
  167. */
  168. clearLocalNotification: function (id) {
  169. var notification = this.getAll([id])[0];
  170. this.getToastHistory().remove('Toast' + id);
  171. if (this.isRepeating(notification))
  172. return;
  173. if (this.isTriggered(id) && !this.isScheduled(id)) {
  174. this.cancelLocalNotification(id);
  175. }
  176. },
  177. /**
  178. * Clears all notifications.
  179. */
  180. clearAll: function () {
  181. var ids = this.getTriggeredIds();
  182. for (var i = 0; i < ids.length; i++) {
  183. this.clearLocalNotification(ids[i]);
  184. }
  185. this.getToastHistory().clear();
  186. this.fireEvent('clearall');
  187. },
  188. /**
  189. * Cancels all specified notifications.
  190. *
  191. * @param {int[]} ids
  192. * List of local notification IDs
  193. */
  194. cancel: function (ids) {
  195. for (var i = 0; i < ids.length; i++) {
  196. var id = ids[i],
  197. notification = this.getAll([id])[0];
  198. this.cancelLocalNotification(ids[i]);
  199. this.fireEvent('cancel', notification);
  200. }
  201. },
  202. /**
  203. * Cancels the local notification with the given ID.
  204. *
  205. * @param {String} id
  206. * Local notification ID
  207. */
  208. cancelLocalNotification: function (id) {
  209. var notifier = this.getToastNotifier(),
  210. history = this.getToastHistory(),
  211. toasts = this.getScheduledToasts();
  212. history.remove('Toast' + id);
  213. for (var i = 0; i < toasts.length; i++) {
  214. var toast = toasts[i];
  215. if (toast.id == id || toast.id == id + '-2') {
  216. notifier.removeFromSchedule(toast);
  217. }
  218. }
  219. },
  220. /**
  221. * Cancels all notifications.
  222. */
  223. cancelAll: function () {
  224. var ids = this.getAllIds();
  225. for (var i = 0; i < ids.length; i++) {
  226. this.cancelLocalNotification(ids[i]);
  227. }
  228. this.getToastHistory().clear();
  229. this.fireEvent('cancelall');
  230. },
  231. /**
  232. * Checks if a notification with an ID is present.
  233. *
  234. * @param {int} id
  235. * Local notification ID
  236. */
  237. isPresent: function (id) {
  238. return !!this.findToastById(id);
  239. },
  240. /**
  241. * Checks if a notification with an ID was scheduled.
  242. *
  243. * @param {int} id
  244. * Local notification ID
  245. */
  246. isScheduled: function (id) {
  247. var toast = this.findToastById(id);
  248. return toast && this.isToastScheduled(toast);
  249. },
  250. /**
  251. * Checks if a notification with an ID was triggered.
  252. *
  253. * @param {int} id
  254. * Local notification ID
  255. */
  256. isTriggered: function (id) {
  257. return this.getTriggeredIds().indexOf(id) > -1;
  258. },
  259. /**
  260. * Lists all local notification IDs.
  261. */
  262. getAllIds: function () {
  263. var toasts = this.getScheduledToasts(),
  264. ids = [];
  265. for (var i = 0; i < toasts.length; i++) {
  266. var toast = toasts[i];
  267. ids.push(this.getToastId(toast));
  268. }
  269. return ids;
  270. },
  271. /**
  272. * Lists all scheduled notification IDs.
  273. */
  274. getScheduledIds: function () {
  275. var toasts = this.getScheduledToasts(),
  276. ids = [];
  277. for (var i = 0; i < toasts.length; i++) {
  278. var toast = toasts[i];
  279. if (!this.isToastScheduled(toast))
  280. continue;
  281. ids.push(this.getToastId(toast));
  282. }
  283. return ids;
  284. },
  285. /**
  286. * Lists all scheduled notification IDs.
  287. */
  288. getTriggeredIds: function () {
  289. var toasts = this.getScheduledToasts(),
  290. ids = [];
  291. for (var i = 0; i < toasts.length; i++) {
  292. var toast = toasts[i];
  293. if (!this.isToastTriggered(toast))
  294. continue;
  295. ids.push(this.getToastId(toast));
  296. }
  297. return ids;
  298. },
  299. /**
  300. * Property list for given notifications.
  301. * If called without IDs, all notification will be returned.
  302. *
  303. * @param {int[]} ids
  304. * List of local notification IDs.
  305. * @param {String?} type
  306. * Local notification life cycle type
  307. */
  308. getAll: function (ids, type) {
  309. var toasts = this.getScheduledToasts(),
  310. notifications = [];
  311. if (!ids || ids.length === 0) {
  312. ids = this.getAllIds();
  313. }
  314. for (var index = 0; index < ids.length; index++) {
  315. var id = ids[index],
  316. toast = this.findToastById(id);
  317. if (!toast || type && this.getToastType(toast) != type)
  318. continue;
  319. var json = toast.content.lastChild.lastChild.innerText;
  320. notifications.push(JSON.parse(json));
  321. }
  322. return notifications;
  323. },
  324. /**
  325. * Property list for given notifications.
  326. * If called without IDs, all notification will be returned.
  327. *
  328. * @param {int[]} ids
  329. * List of local notification IDs
  330. */
  331. getScheduled: function (ids) {
  332. if (!ids || ids.length === 0) {
  333. ids = this.getAllIds();
  334. }
  335. return this.getAll(ids, 'scheduled');
  336. },
  337. /**
  338. * Property list for given notifications.
  339. * If called without IDs, all notification will be returned.
  340. *
  341. * @param {int[]} ids
  342. * List of local notification IDs
  343. */
  344. getTriggered: function (ids) {
  345. if (!ids || ids.length === 0) {
  346. ids = this.getAllIds();
  347. }
  348. return this.getAll(ids, 'triggered');
  349. },
  350. };