LocalNotificationUtil.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. exports = require('de.appplant.cordova.plugin.local-notification.LocalNotification.Proxy').core;
  19. var channel = require('cordova/channel');
  20. /***********
  21. * MEMBERS *
  22. ***********/
  23. // True if App is running, false if suspended
  24. exports.isInBackground = true;
  25. // Indicates if the device is ready (to receive events)
  26. exports.isReady = false;
  27. // Queues all events before deviceready
  28. exports.eventQueue = [];
  29. /********
  30. * UTIL *
  31. ********/
  32. /**
  33. * The repeating interval in milliseconds.
  34. *
  35. * @param {String} interval
  36. * A number or a placeholder like `minute`.
  37. *
  38. * @return {Number}
  39. * Interval in milliseconds
  40. */
  41. exports.getRepeatInterval = function (every) {
  42. if (!every)
  43. return 0;
  44. if (every == 'minute')
  45. return 60000;
  46. if (every == 'hour')
  47. return 360000;
  48. if (!NaN(every))
  49. return parseInt(every) * 60000;
  50. return 0;
  51. };
  52. /**
  53. * If the notification is repeating.
  54. *
  55. * @param {Object} notification
  56. * Local notification object
  57. *
  58. * @return Boolean
  59. */
  60. exports.isRepeating = function (notification) {
  61. return this.getRepeatInterval(notification.every) !== 0;
  62. };
  63. /**
  64. * Parses sound file path.
  65. *
  66. * @param {String} path
  67. * Relative path to sound resource
  68. *
  69. * @return {String} XML Tag for Sound-File
  70. */
  71. exports.parseSound = function (path) {
  72. if (!path.match(/^file/))
  73. return '';
  74. var uri = this.parseUri(path),
  75. audio = "<audio src=" + uri + " loop='false'/>";
  76. return audio;
  77. };
  78. /**
  79. * Parses image file path.
  80. *
  81. * @param {String} path
  82. * Relative path to image resource
  83. *
  84. * @return {String} XML-Tag for Image-File
  85. */
  86. exports.parseImage = function (path) {
  87. if (!path.match(/^file/))
  88. return '';
  89. var uri = this.parseUri(path),
  90. image = "<image id='1' src=" + uri + " />";
  91. return image;
  92. };
  93. /**
  94. * Parses file path to URI.
  95. *
  96. * @param {String} path
  97. * Relative path to a resource
  98. *
  99. * @return {String} URI to File
  100. */
  101. exports.parseUri = function (path) {
  102. var pkg = Windows.ApplicationModel.Package.current,
  103. pkgId = pkg.id,
  104. pkgName = pkgId.name;
  105. var uri = "'ms-appx://" + pkgName + "/www" + path.slice(6, path.length) + "'";
  106. return uri;
  107. };
  108. /**
  109. * Builds the xml payload for a local notification based on its options.
  110. *
  111. * @param {Object} options
  112. * Local notification properties
  113. *
  114. * @return Windows.Data.Xml.Dom.XmlDocument
  115. */
  116. exports.build = function (options) {
  117. var template = this.buildToastTemplate(options),
  118. notification = new Windows.Data.Xml.Dom.XmlDocument();
  119. try {
  120. notification.loadXml(template);
  121. } catch (e) {
  122. console.error(
  123. 'LocalNotification#schedule',
  124. 'Error loading the xml, check for invalid characters.');
  125. }
  126. // Launch Attribute to enable onClick event
  127. var launchAttr = notification.createAttribute('launch'),
  128. toastNode = notification.selectSingleNode('/toast');
  129. launchAttr.value = options.id.toString();
  130. toastNode.attributes.setNamedItem(launchAttr);
  131. return notification;
  132. };
  133. /**
  134. * Builds the toast template with the right style depend on the options.
  135. *
  136. * @param {Object} options
  137. * Local notification properties
  138. *
  139. * @return String
  140. */
  141. exports.buildToastTemplate = function (options) {
  142. var title = options.title,
  143. message = options.text || '',
  144. json = JSON.stringify(options),
  145. sound = '';
  146. if (options.sound && options.sound !== '') {
  147. sound = this.parseSound(options.sound);
  148. }
  149. var templateName = "ToastText",
  150. imageNode;
  151. if (options.icon && options.icon !== '') {
  152. imageNode = this.parseImage(options.icon);
  153. // template with Image
  154. if (imageNode !== '') {
  155. templateName = "ToastImageAndText";
  156. }
  157. } else {
  158. imageNode = "";
  159. }
  160. var bindingNode;
  161. if (title && title !== '') {
  162. bindingNode = "<binding template='" + templateName + "02'>" +
  163. imageNode +
  164. "<text id='1'>" + title + "</text>" +
  165. "<text id='2'>" + message + "</text>" +
  166. "</binding>";
  167. } else {
  168. bindingNode = "<binding template='" + templateName + "01'>" +
  169. imageNode +
  170. "<text id='1'>" + message + "</text>" +
  171. "</binding>";
  172. }
  173. return "<toast>" +
  174. "<visual>" +
  175. bindingNode +
  176. "</visual>" +
  177. sound +
  178. "<json>" + json + "</json>" +
  179. "</toast>";
  180. };
  181. /**
  182. * Short-hand method for the toast notification history.
  183. */
  184. exports.getToastHistory = function () {
  185. return Windows.UI.Notifications.ToastNotificationManager.history;
  186. };
  187. /**
  188. * Gets a toast notifier instance.
  189. *
  190. * @return Object
  191. */
  192. exports.getToastNotifier = function () {
  193. return Windows.UI.Notifications.ToastNotificationManager
  194. .createToastNotifier();
  195. };
  196. /**
  197. * List of all scheduled toast notifiers.
  198. *
  199. * @return Array
  200. */
  201. exports.getScheduledToasts = function () {
  202. return this.getToastNotifier().getScheduledToastNotifications();
  203. };
  204. /**
  205. * Gets the Id from the toast notifier.
  206. *
  207. * @param {Object} toast
  208. * A toast notifier object
  209. *
  210. * @return String
  211. */
  212. exports.getToastId = function (toast) {
  213. var id = toast.id;
  214. if (id.match(/-2$/))
  215. return id.match(/^[^-]+/)[0];
  216. return id;
  217. };
  218. /**
  219. * Gets the notification life cycle type
  220. * (scheduled or triggered)
  221. *
  222. * @param {Object} toast
  223. * A toast notifier object
  224. *
  225. * @return String
  226. */
  227. exports.getToastType = function (toast) {
  228. return this.isToastTriggered(toast) ? 'triggered' : 'scheduled';
  229. };
  230. /**
  231. * If the toast is already scheduled.
  232. *
  233. * @param {Object} toast
  234. * A toast notifier object
  235. *
  236. * @return Boolean
  237. */
  238. exports.isToastScheduled = function (toast) {
  239. return !this.isToastTriggered(toast);
  240. };
  241. /**
  242. * If the toast is already triggered.
  243. *
  244. * @param {Object} toast
  245. * A toast notifier object
  246. *
  247. * @return Boolean
  248. */
  249. exports.isToastTriggered = function (toast) {
  250. var id = this.getToastId(toast),
  251. notification = this.getAll(id)[0];
  252. fireDate = new Date((notification.at) * 1000);
  253. if (this.isRepeating(notification))
  254. return false;
  255. return fireDate <= new Date();
  256. };
  257. /**
  258. * Finds the toast by it's ID.
  259. *
  260. * @param {String} id
  261. * Local notification ID
  262. *
  263. * @param Object
  264. */
  265. exports.findToastById = function (id) {
  266. var toasts = this.getScheduledToasts();
  267. for (var i = 0; i < toasts.length; i++) {
  268. var toast = toasts[i];
  269. if (this.getToastId(toast) == id)
  270. return toast;
  271. }
  272. return null;
  273. };
  274. /**
  275. * Sets trigger event for local notification.
  276. *
  277. * @param {Object} notification
  278. * Local notification object
  279. * @param {Function} callback
  280. * Callback function
  281. */
  282. exports.callOnTrigger = function (notification, callback) {
  283. var triggerTime = new Date((notification.at * 1000)),
  284. interval = triggerTime - new Date();
  285. if (interval <= 0) {
  286. callback.call(this, notification);
  287. return;
  288. }
  289. WinJS.Promise.timeout(interval).then(function () {
  290. if (exports.isPresent(notification.id)) {
  291. callback.call(exports, notification);
  292. }
  293. });
  294. };
  295. /**
  296. * Sets trigger event for all scheduled local notification.
  297. *
  298. * @param {Function} callback
  299. * Callback function
  300. */
  301. exports.callOnTriggerForScheduled = function (callback) {
  302. var notifications = this.getScheduled();
  303. for (var i = 0; i < notifications.length; i++) {
  304. this.callOnTrigger(notifications[i], callback);
  305. }
  306. };
  307. /**
  308. * The application state - background or foreground.
  309. *
  310. * @return String
  311. */
  312. exports.getApplicationState = function () {
  313. return this.isInBackground ? 'background' : 'foreground';
  314. };
  315. /**
  316. * Fires the event about a local notification.
  317. *
  318. * @param {String} event
  319. * The event
  320. * @param {Object} notification
  321. * The notification
  322. */
  323. exports.fireEvent = function (event, notification) {
  324. var plugin = cordova.plugins.notification.local.core,
  325. state = this.getApplicationState(),
  326. args;
  327. if (notification) {
  328. args = [event, notification, state];
  329. } else {
  330. args = [event, state];
  331. }
  332. if (this.isReady && plugin) {
  333. plugin.fireEvent.apply(plugin, args);
  334. } else {
  335. this.eventQueue.push(args);
  336. }
  337. };
  338. /**************
  339. * LIFE CYCLE *
  340. **************/
  341. // Called before 'deviceready' event
  342. channel.onCordovaReady.subscribe(function () {
  343. // Register trigger handler for each scheduled notification
  344. exports.callOnTriggerForScheduled(function (notification) {
  345. this.updateBadge(notification.badge);
  346. this.fireEvent('trigger', notification);
  347. });
  348. });
  349. // Handle onclick event
  350. WinJS.Application.addEventListener('activated', function (args) {
  351. var id = args.detail.arguments,
  352. notification = exports.getAll([id])[0];
  353. if (!notification)
  354. return;
  355. exports.clearLocalNotification(id);
  356. var repeating = exports.isRepeating(notification);
  357. exports.fireEvent('click', notification);
  358. exports.fireEvent(repeating ? 'clear' : 'cancel', notification);
  359. }, false);
  360. // App is running in background
  361. document.addEventListener('pause', function () {
  362. exports.isInBackground = true;
  363. }, false);
  364. // App is running in foreground
  365. document.addEventListener('resume', function () {
  366. exports.isInBackground = false;
  367. }, false);
  368. // App is running in foreground
  369. document.addEventListener('deviceready', function () {
  370. exports.isInBackground = false;
  371. }, false);