local-notification-util.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. id: 0,
  22. type: 'normal',
  23. text: '',
  24. title: '',
  25. sound: 'res://platform_default',
  26. badge: undefined,
  27. data: undefined,
  28. every: undefined,
  29. at: undefined,
  30. actions: undefined,
  31. actionGroupId: undefined
  32. };
  33. // Listener
  34. exports._listener = {};
  35. /**
  36. * Merge platform specific properties into the default ones.
  37. *
  38. * @return [ Void ]
  39. */
  40. exports.applyPlatformSpecificOptions = function () {
  41. var defaults = this._defaults;
  42. switch (device.platform) {
  43. case 'Android':
  44. defaults.icon = 'res://ic_popup_reminder';
  45. defaults.smallIcon = undefined;
  46. defaults.ongoing = false;
  47. defaults.autoClear = true;
  48. defaults.led = undefined;
  49. defaults.color = undefined;
  50. break;
  51. case 'iOS':
  52. defaults.attachments = undefined;
  53. defaults.region = undefined;
  54. defaults.radius = undefined;
  55. break;
  56. }
  57. };
  58. /**
  59. * Merge custom properties with the default values.
  60. *
  61. * @param [ Object ] options Set of custom values.
  62. *
  63. * @retrun [ Object ]
  64. */
  65. exports.mergeWithDefaults = function (options) {
  66. var defaults = this.getDefaults();
  67. options.at = this.getValueFor(options, 'at', 'firstAt', 'date');
  68. options.text = this.getValueFor(options, 'text', 'message');
  69. options.data = this.getValueFor(options, 'data', 'json');
  70. if (defaults.hasOwnProperty('autoClear')) {
  71. options.autoClear = this.getValueFor(options, 'autoClear', 'autoCancel');
  72. }
  73. if (options.autoClear !== true && options.ongoing) {
  74. options.autoClear = false;
  75. }
  76. if (options.at === undefined || options.at === null) {
  77. options.at = new Date();
  78. }
  79. for (var key in defaults) {
  80. if (options[key] === null || options[key] === undefined) {
  81. if (options.hasOwnProperty(key) && ['data','sound'].indexOf(key) > -1) {
  82. options[key] = undefined;
  83. } else {
  84. options[key] = defaults[key];
  85. }
  86. }
  87. }
  88. for (key in options) {
  89. if (!defaults.hasOwnProperty(key)) {
  90. delete options[key];
  91. console.warn('Unknown property: ' + key);
  92. }
  93. }
  94. return options;
  95. };
  96. /**
  97. * Convert the passed values to their required type.
  98. *
  99. * @param [ Object ] options Properties to convert for.
  100. *
  101. * @return [ Object ] The converted property list
  102. */
  103. exports.convertProperties = function (options) {
  104. if (options.id) {
  105. if (isNaN(options.id)) {
  106. options.id = this.getDefaults().id;
  107. console.warn('Id is not a number: ' + options.id);
  108. } else {
  109. options.id = Number(options.id);
  110. }
  111. }
  112. if (options.title) {
  113. options.title = options.title.toString();
  114. }
  115. if (options.text) {
  116. options.text = options.text.toString();
  117. }
  118. if (options.badge) {
  119. if (isNaN(options.badge)) {
  120. options.badge = this.getDefaults().badge;
  121. console.warn('Badge number is not a number: ' + options.id);
  122. } else {
  123. options.badge = Number(options.badge);
  124. }
  125. }
  126. if (options.at) {
  127. if (typeof options.at == 'object') {
  128. options.at = options.at.getTime();
  129. }
  130. options.at = Math.round(options.at/1000);
  131. }
  132. if (typeof options.data == 'object') {
  133. options.data = JSON.stringify(options.data);
  134. }
  135. if (options.actions) {
  136. this.convertActions(options);
  137. }
  138. return options;
  139. };
  140. /**
  141. * Convert the passed values to their required type, modifying them
  142. * directly for Android and passing the converted list back for iOS.
  143. *
  144. * @param [ Map ] options Set of custom values.
  145. *
  146. * @return [ Map ] Interaction object with category & actions.
  147. */
  148. exports.convertActions = function (options) {
  149. if (!options.actions)
  150. return null;
  151. var MAX_ACTIONS = (device.platform === 'iOS') ? 4 : 3,
  152. actions = [];
  153. if (options.actions.length > MAX_ACTIONS)
  154. console.warn('Count of actions exceeded count of ' + MAX_ACTIONS);
  155. for (var i = 0; i < options.actions.length && MAX_ACTIONS > 0; i++) {
  156. var action = options.actions[i];
  157. if (!action.id) {
  158. console.warn(
  159. 'Action with title ' + action.title + ' has no id and will not be added.');
  160. continue;
  161. }
  162. action.id = action.id.toString();
  163. action.title = (action.title || action.id).toString();
  164. actions.push(action);
  165. MAX_ACTIONS--;
  166. }
  167. options.category = (options.category || 'DEFAULT_GROUP').toString();
  168. options.actions = actions;
  169. };
  170. /**
  171. * Create a callback function to get executed within a specific scope.
  172. *
  173. * @param [ Function ] fn The function to be exec as the callback.
  174. * @param [ Object ] scope The callback function's scope.
  175. *
  176. * @return [ Function ]
  177. */
  178. exports.createCallbackFn = function (fn, scope) {
  179. if (typeof fn != 'function')
  180. return;
  181. return function () {
  182. fn.apply(scope || this, arguments);
  183. };
  184. };
  185. /**
  186. * Convert the IDs to numbers.
  187. *
  188. * @param [ Array ] ids
  189. *
  190. * @return [ Array<Number> ]
  191. */
  192. exports.convertIds = function (ids) {
  193. var convertedIds = [];
  194. for (var i = 0; i < ids.length; i++) {
  195. convertedIds.push(Number(ids[i]));
  196. }
  197. return convertedIds;
  198. };
  199. /**
  200. * First found value for the given keys.
  201. *
  202. * @param [ Object ] options Object with key-value properties.
  203. * @param [ *Array<String> ] keys List of keys.
  204. *
  205. * @return [ Object ]
  206. */
  207. exports.getValueFor = function (options) {
  208. var keys = Array.apply(null, arguments).slice(1);
  209. for (var i = 0; i < keys.length; i++) {
  210. var key = keys[i];
  211. if (options.hasOwnProperty(key)) {
  212. return options[key];
  213. }
  214. }
  215. };
  216. /**
  217. * Fire the event with given arguments.
  218. *
  219. * @param [ String ] event The event's name.
  220. * @param [ *Array] args The callback's arguments.
  221. *
  222. * @return [ Void]
  223. */
  224. exports.fireEvent = function (event) {
  225. var args = Array.apply(null, arguments).slice(1),
  226. listener = this._listener[event];
  227. if (!listener)
  228. return;
  229. for (var i = 0; i < listener.length; i++) {
  230. var fn = listener[i][0],
  231. scope = listener[i][1];
  232. fn.apply(scope, args);
  233. }
  234. };
  235. /**
  236. * Execute the native counterpart.
  237. *
  238. * @param [ String ] action The name of the action.
  239. * @param [ Array ] args Array of arguments.
  240. * @param [ Function] callback The callback function.
  241. * @param [ Object ] scope The scope for the function.
  242. *
  243. * @return [ Void ]
  244. */
  245. exports.exec = function (action, args, callback, scope) {
  246. var fn = this.createCallbackFn(callback, scope),
  247. params = [];
  248. if (Array.isArray(args)) {
  249. params = args;
  250. } else if (args) {
  251. params.push(args);
  252. }
  253. exec(fn, null, 'LocalNotification', action, params);
  254. };
  255. // Called after 'deviceready' event
  256. channel.deviceready.subscribe(function () {
  257. // Device is ready now, the listeners are registered
  258. // and all queued events can be executed.
  259. exports.exec('deviceready');
  260. });
  261. // Called before 'deviceready' event
  262. channel.onCordovaReady.subscribe(function () {
  263. // Set launchDetails object
  264. exports.exec('launchDetails');
  265. // Device plugin is ready now
  266. channel.onCordovaInfoReady.subscribe(function () {
  267. // Merge platform specifics into defaults
  268. exports.applyPlatformSpecificOptions();
  269. });
  270. });