local-notification-util.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. }
  49. };
  50. /**
  51. * Merge custom properties with the default values.
  52. *
  53. * @param [ Object ] options Set of custom values.
  54. *
  55. * @retrun [ Object ]
  56. */
  57. exports.mergeWithDefaults = function (options) {
  58. var defaults = this.getDefaults();
  59. options.at = this.getValueFor(options, 'at', 'firstAt', 'date');
  60. options.text = this.getValueFor(options, 'text', 'message');
  61. options.data = this.getValueFor(options, 'data', 'json');
  62. if (defaults.hasOwnProperty('autoClear')) {
  63. options.autoClear = this.getValueFor(options, 'autoClear', 'autoCancel');
  64. }
  65. if (options.autoClear !== true && options.ongoing) {
  66. options.autoClear = false;
  67. }
  68. if (options.at === undefined || options.at === null) {
  69. options.at = new Date();
  70. }
  71. for (var key in defaults) {
  72. if (options[key] === null || options[key] === undefined) {
  73. if (options.hasOwnProperty(key) && ['data','sound'].indexOf(key) > -1) {
  74. options[key] = undefined;
  75. } else {
  76. options[key] = defaults[key];
  77. }
  78. }
  79. }
  80. for (key in options) {
  81. if (!defaults.hasOwnProperty(key)) {
  82. delete options[key];
  83. console.warn('Unknown property: ' + key);
  84. }
  85. }
  86. return options;
  87. };
  88. /**
  89. * Convert the passed values to their required type.
  90. *
  91. * @param [ Object ] options Properties to convert for.
  92. *
  93. * @return [ Object ] The converted property list
  94. */
  95. exports.convertProperties = function (options) {
  96. if (options.id) {
  97. if (isNaN(options.id)) {
  98. options.id = this.getDefaults().id;
  99. console.warn('Id is not a number: ' + options.id);
  100. } else {
  101. options.id = Number(options.id);
  102. }
  103. }
  104. if (options.title) {
  105. options.title = options.title.toString();
  106. }
  107. if (options.text) {
  108. options.text = options.text.toString();
  109. }
  110. if (options.badge) {
  111. if (isNaN(options.badge)) {
  112. options.badge = this.getDefaults().badge;
  113. console.warn('Badge number is not a number: ' + options.id);
  114. } else {
  115. options.badge = Number(options.badge);
  116. }
  117. }
  118. if (options.at) {
  119. if (typeof options.at == 'object') {
  120. options.at = options.at.getTime();
  121. }
  122. options.at = Math.round(options.at/1000);
  123. }
  124. if (typeof options.data == 'object') {
  125. options.data = JSON.stringify(options.data);
  126. }
  127. return options;
  128. };
  129. /**
  130. * Create a callback function to get executed within a specific scope.
  131. *
  132. * @param [ Function ] fn The function to be exec as the callback.
  133. * @param [ Object ] scope The callback function's scope.
  134. *
  135. * @return [ Function ]
  136. */
  137. exports.createCallbackFn = function (fn, scope) {
  138. if (typeof fn != 'function')
  139. return;
  140. return function () {
  141. fn.apply(scope || this, arguments);
  142. };
  143. };
  144. /**
  145. * Convert the IDs to numbers.
  146. *
  147. * @param [ Array ] ids
  148. *
  149. * @return [ Array<Number> ]
  150. */
  151. exports.convertIds = function (ids) {
  152. var convertedIds = [];
  153. for (var i = 0; i < ids.length; i++) {
  154. convertedIds.push(Number(ids[i]));
  155. }
  156. return convertedIds;
  157. };
  158. /**
  159. * First found value for the given keys.
  160. *
  161. * @param [ Object ] options Object with key-value properties.
  162. * @param [ *Array<String> ] keys List of keys.
  163. *
  164. * @return [ Object ]
  165. */
  166. exports.getValueFor = function (options) {
  167. var keys = Array.apply(null, arguments).slice(1);
  168. for (var i = 0; i < keys.length; i++) {
  169. var key = keys[i];
  170. if (options.hasOwnProperty(key)) {
  171. return options[key];
  172. }
  173. }
  174. };
  175. /**
  176. * Fire the event with given arguments.
  177. *
  178. * @param [ String ] event The event's name.
  179. * @param [ *Array] args The callback's arguments.
  180. *
  181. * @return [ Void]
  182. */
  183. exports.fireEvent = function (event) {
  184. var args = Array.apply(null, arguments).slice(1),
  185. listener = this._listener[event];
  186. if (!listener)
  187. return;
  188. for (var i = 0; i < listener.length; i++) {
  189. var fn = listener[i][0],
  190. scope = listener[i][1];
  191. fn.apply(scope, args);
  192. }
  193. };
  194. /**
  195. * Execute the native counterpart.
  196. *
  197. * @param [ String ] action The name of the action.
  198. * @param [ Array ] args Array of arguments.
  199. * @param [ Function] callback The callback function.
  200. * @param [ Object ] scope The scope for the function.
  201. *
  202. * @return [ Void ]
  203. */
  204. exports.exec = function (action, args, callback, scope) {
  205. var fn = this.createCallbackFn(callback, scope),
  206. params = [];
  207. if (Array.isArray(args)) {
  208. params = args;
  209. } else if (args) {
  210. params.push(args);
  211. }
  212. exec(fn, null, 'LocalNotification', action, params);
  213. };
  214. // Called after 'deviceready' event
  215. channel.deviceready.subscribe(function () {
  216. // Device is ready now, the listeners are registered
  217. // and all queued events can be executed.
  218. exec(null, null, 'LocalNotification', 'deviceready', []);
  219. });
  220. // Called before 'deviceready' event
  221. channel.onCordovaReady.subscribe(function () {
  222. // Device plugin is ready now
  223. channel.onCordovaInfoReady.subscribe(function () {
  224. // Merge platform specifics into defaults
  225. exports.applyPlatformSpecificOptions();
  226. });
  227. });