local-notification-util.js 8.2 KB

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