local-notification-util.js 8.4 KB

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