local-notification-util.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright (c) 2013-2015 by appPlant UG. All rights reserved.
  3. *
  4. * @APPPLANT_LICENSE_HEADER_START@
  5. *
  6. * This file contains Original Code and/or Modifications of Original Code
  7. * as defined in and that are subject to the Apache License
  8. * Version 2.0 (the 'License'). You may not use this file except in
  9. * compliance with the License. Please obtain a copy of the License at
  10. * http://opensource.org/licenses/Apache-2.0/ and read it before using this
  11. * file.
  12. *
  13. * The Original Code and all software distributed under the License are
  14. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  15. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  16. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  18. * Please see the License for the specific language governing rights and
  19. * limitations under the License.
  20. *
  21. * @APPPLANT_LICENSE_HEADER_END@
  22. */
  23. var exec = require('cordova/exec'),
  24. channel = require('cordova/channel');
  25. /***********
  26. * MEMBERS *
  27. ***********/
  28. // Default values
  29. exports._defaults = {
  30. text: '',
  31. title: '',
  32. sound: 'res://platform_default',
  33. badge: 0,
  34. id: 0,
  35. data: undefined,
  36. every: undefined,
  37. at: undefined
  38. };
  39. // listener
  40. exports._listener = {};
  41. // Registered permission flag
  42. exports._registered = false;
  43. /********
  44. * UTIL *
  45. ********/
  46. /**
  47. * Merge platform specific properties into the default ones.
  48. *
  49. * @return {Object}
  50. * The default properties for the platform
  51. */
  52. exports.applyPlatformSpecificOptions = function () {
  53. var defaults = this._defaults;
  54. switch (device.platform) {
  55. case 'Android':
  56. defaults.icon = 'res://ic_popup_reminder';
  57. defaults.smallIcon = undefined;
  58. defaults.ongoing = false;
  59. defaults.autoClear = true;
  60. defaults.led = undefined;
  61. defaults.ledOnTime = undefined;
  62. defaults.ledOffTime = undefined;
  63. defaults.color = undefined;
  64. defaults.vibrate = undefined;
  65. break;
  66. }
  67. return defaults;
  68. };
  69. /**
  70. * Merge custom properties with the default values.
  71. *
  72. * @param {Object} options
  73. * Set of custom values
  74. *
  75. * @retrun {Object}
  76. * The merged property list
  77. */
  78. exports.mergeWithDefaults = function (options) {
  79. var defaults = this.getDefaults();
  80. options.at = this.getValueFor(options, 'at', 'firstAt', 'date');
  81. options.text = this.getValueFor(options, 'text', 'message');
  82. options.data = this.getValueFor(options, 'data', 'json');
  83. if (defaults.hasOwnProperty('autoClear')) {
  84. options.autoClear = this.getValueFor(options, 'autoClear', 'autoCancel');
  85. }
  86. if (options.autoClear !== true && options.ongoing) {
  87. options.autoClear = false;
  88. }
  89. if (options.at === undefined || options.at === null) {
  90. options.at = new Date();
  91. }
  92. for (var key in defaults) {
  93. if (options[key] === null || options[key] === undefined) {
  94. if (options.hasOwnProperty(key) && ['data','sound'].indexOf(key) > -1) {
  95. options[key] = undefined;
  96. } else {
  97. options[key] = defaults[key];
  98. }
  99. }
  100. }
  101. for (key in options) {
  102. if (!defaults.hasOwnProperty(key)) {
  103. delete options[key];
  104. console.warn('Unknown property: ' + key);
  105. }
  106. }
  107. return options;
  108. };
  109. /**
  110. * Convert the passed values to their required type.
  111. *
  112. * @param {Object} options
  113. * Set of custom values
  114. *
  115. * @retrun {Object}
  116. * The converted property list
  117. */
  118. exports.convertProperties = function (options) {
  119. if (options.id) {
  120. if (isNaN(options.id)) {
  121. options.id = this.getDefaults().id;
  122. console.warn('Id is not a number: ' + options.id);
  123. } else {
  124. options.id = Number(options.id);
  125. }
  126. }
  127. if (options.title) {
  128. options.title = options.title.toString();
  129. }
  130. if (options.text) {
  131. options.text = options.text.toString();
  132. }
  133. if (options.badge) {
  134. if (isNaN(options.badge)) {
  135. options.badge = this.getDefaults().badge;
  136. console.warn('Badge number is not a number: ' + options.id);
  137. } else {
  138. options.badge = Number(options.badge);
  139. }
  140. }
  141. if (options.at) {
  142. if (typeof options.at == 'object') {
  143. options.at = options.at.getTime();
  144. }
  145. options.at = Math.round(options.at/1000);
  146. }
  147. if (typeof options.data == 'object') {
  148. options.data = JSON.stringify(options.data);
  149. }
  150. if (options.every) {
  151. if (device.platform == 'iOS' && typeof options.every != 'string') {
  152. options.every = this.getDefaults().every;
  153. var warning = 'Every option is not a string: ' + options.id;
  154. warning += '. Expects one of: second, minute, hour, day, week, ';
  155. warning += 'month, year on iOS.';
  156. console.warn(warning);
  157. }
  158. }
  159. return options;
  160. };
  161. /**
  162. * Create callback, which will be executed within a specific scope.
  163. *
  164. * @param {Function} callbackFn
  165. * The callback function
  166. * @param {Object} scope
  167. * The scope for the function
  168. *
  169. * @return {Function}
  170. * The new callback function
  171. */
  172. exports.createCallbackFn = function (callbackFn, scope) {
  173. if (typeof callbackFn != 'function')
  174. return;
  175. return function () {
  176. callbackFn.apply(scope || this, arguments);
  177. };
  178. };
  179. /**
  180. * Convert the IDs to numbers.
  181. *
  182. * @param {String/Number[]} ids
  183. *
  184. * @return Array of Numbers
  185. */
  186. exports.convertIds = function (ids) {
  187. var convertedIds = [];
  188. for (var i = 0; i < ids.length; i++) {
  189. convertedIds.push(Number(ids[i]));
  190. }
  191. return convertedIds;
  192. };
  193. /**
  194. * First found value for the given keys.
  195. *
  196. * @param {Object} options
  197. * Object with key-value properties
  198. * @param {String[]} keys*
  199. * Key list
  200. */
  201. exports.getValueFor = function (options) {
  202. var keys = Array.apply(null, arguments).slice(1);
  203. for (var i = 0; i < keys.length; i++) {
  204. var key = keys[i];
  205. if (options.hasOwnProperty(key)) {
  206. return options[key];
  207. }
  208. }
  209. };
  210. /**
  211. * Fire event with given arguments.
  212. *
  213. * @param {String} event
  214. * The event's name
  215. * @param {args*}
  216. * The callback's arguments
  217. */
  218. exports.fireEvent = function (event) {
  219. var args = Array.apply(null, arguments).slice(1),
  220. listener = this._listener[event];
  221. if (!listener)
  222. return;
  223. for (var i = 0; i < listener.length; i++) {
  224. var fn = listener[i][0],
  225. scope = listener[i][1];
  226. fn.apply(scope, args);
  227. }
  228. };
  229. /**
  230. * Execute the native counterpart.
  231. *
  232. * @param {String} action
  233. * The name of the action
  234. * @param args[]
  235. * Array of arguments
  236. * @param {Function} callback
  237. * The callback function
  238. * @param {Object} scope
  239. * The scope for the function
  240. */
  241. exports.exec = function (action, args, callback, scope) {
  242. var fn = this.createCallbackFn(callback, scope),
  243. params = [];
  244. if (Array.isArray(args)) {
  245. params = args;
  246. } else if (args) {
  247. params.push(args);
  248. }
  249. exec(fn, null, 'LocalNotification', action, params);
  250. };
  251. /*********
  252. * HOOKS *
  253. *********/
  254. // Called after 'deviceready' event
  255. channel.deviceready.subscribe(function () {
  256. // Device is ready now, the listeners are registered
  257. // and all queued events can be executed.
  258. exec(null, null, 'LocalNotification', 'deviceready', []);
  259. });
  260. // Called before 'deviceready' event
  261. channel.onCordovaReady.subscribe(function () {
  262. // Device plugin is ready now
  263. channel.onCordovaInfoReady.subscribe(function () {
  264. // Merge platform specifics into defaults
  265. exports.applyPlatformSpecificOptions();
  266. });
  267. });