local-notification-util.js 6.9 KB

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