local-notification-util.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * Apache 2.0 License
  3. *
  4. * Copyright (c) Sebastian Katzer 2017
  5. *
  6. * This file contains Original Code and/or Modifications of Original Code
  7. * as defined in and that are subject to the Apache License
  8. * Version 2.0 (the 'License'). You may not use this file except in
  9. * compliance with the License. Please obtain a copy of the License at
  10. * http://opensource.org/licenses/Apache-2.0/ and read it before using this
  11. * file.
  12. *
  13. * The Original Code and all software distributed under the License are
  14. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  15. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  16. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  18. * Please see the License for the specific language governing rights and
  19. * limitations under the License.
  20. */
  21. var exec = require('cordova/exec'),
  22. channel = require('cordova/channel');
  23. // Default values
  24. exports._defaults = {
  25. id: 0,
  26. text: '',
  27. title: '',
  28. sound: 'res://platform_default',
  29. badge: undefined,
  30. data: undefined,
  31. icon: undefined,
  32. trigger: { type: 'calendar' },
  33. actions: [],
  34. actionGroupId: undefined,
  35. attachments: [],
  36. progressBar: false
  37. };
  38. // Listener
  39. exports._listener = {};
  40. /**
  41. * Merge platform specific properties into the default ones.
  42. *
  43. * @return [ Void ]
  44. */
  45. exports.applyPlatformSpecificOptions = function () {
  46. var defaults = this._defaults;
  47. switch (device.platform) {
  48. case 'Android':
  49. defaults.icon = 'res://ic_popup_reminder';
  50. defaults.smallIcon = undefined;
  51. defaults.ongoing = false;
  52. defaults.autoClear = true;
  53. defaults.led = undefined;
  54. defaults.color = 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.text = this.getValueFor(options, 'text', 'message');
  68. options.data = this.getValueFor(options, 'data', 'json');
  69. if (defaults.hasOwnProperty('autoClear')) {
  70. options.autoClear = this.getValueFor(options, 'autoClear', 'autoCancel');
  71. }
  72. if (options.autoClear !== true && options.ongoing) {
  73. options.autoClear = false;
  74. }
  75. for (var key in defaults) {
  76. if (options[key] === null || options[key] === undefined) {
  77. if (options.hasOwnProperty(key) && ['data','sound'].indexOf(key) > -1) {
  78. options[key] = undefined;
  79. } else {
  80. var obj = defaults[key];
  81. options[key] = typeof obj === 'object' ? Object.assign({}, obj) : obj;
  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 (typeof options.data == 'object') {
  124. options.data = JSON.stringify(options.data);
  125. }
  126. this.convertTrigger(options);
  127. this.convertActions(options);
  128. this.convertProgressBar(options);
  129. return options;
  130. };
  131. /**
  132. * Convert the passed values to their required type, modifying them
  133. * directly for Android and passing the converted list back for iOS.
  134. *
  135. * @param [ Map ] options Set of custom values.
  136. *
  137. * @return [ Map ] Interaction object with category & actions.
  138. */
  139. exports.convertActions = function (options) {
  140. if (!options.actions)
  141. return null;
  142. var actions = [];
  143. for (var i = 0, action; i < options.actions.length; i++) {
  144. action = options.actions[i];
  145. if (!action.id) {
  146. console.warn(
  147. 'Action with title ' + action.title + ' has no id and will not be added.');
  148. continue;
  149. }
  150. action.id = action.id.toString();
  151. actions.push(action);
  152. }
  153. options.category = (options.category || 'DEFAULT_GROUP').toString();
  154. options.actions = actions;
  155. return options;
  156. };
  157. /**
  158. * Convert the passed values for the trigger to their required type.
  159. *
  160. * @param [ Map ] options Set of custom values.
  161. *
  162. * @return [ Map ] Interaction object with trigger spec.
  163. */
  164. exports.convertTrigger = function (options) {
  165. var trigger = options.trigger || {},
  166. date = this.getValueFor(trigger, 'at', 'firstAt', 'date');
  167. if (!trigger.type) {
  168. trigger.type = trigger.center ? 'location' : 'calendar';
  169. }
  170. var isCal = trigger.type == 'calendar';
  171. if (isCal && !date) {
  172. date = this.getValueFor(options, 'at', 'firstAt', 'date');
  173. }
  174. if (isCal && !trigger.every && options.every) {
  175. trigger.every = options.every;
  176. }
  177. if (isCal && (trigger.in || trigger.every)) {
  178. date = null;
  179. }
  180. if (isCal && date) {
  181. date = typeof date == 'object' ? date.getTime() : date;
  182. trigger.at = Math.round(date / 1000);
  183. }
  184. if (!trigger.count && device.platform == 'windows') {
  185. trigger.count = trigger.every ? 5 : 1;
  186. }
  187. if (trigger.every && device.platform == 'windows') {
  188. trigger.every = trigger.every.toString();
  189. }
  190. if (!isCal) {
  191. trigger.notifyOnEntry = !!trigger.notifyOnEntry;
  192. trigger.notifyOnExit = trigger.notifyOnExit === true;
  193. trigger.radius = trigger.radius || 5;
  194. }
  195. if (!isCal || trigger.at) {
  196. delete trigger.every;
  197. }
  198. delete options.every;
  199. delete options.at;
  200. delete options.firstAt;
  201. delete options.date;
  202. options.trigger = trigger;
  203. return options;
  204. };
  205. /**
  206. * Convert the passed values for the progressBar to their required type.
  207. *
  208. * @param [ Map ] options Set of custom values.
  209. *
  210. * @return [ Map ] Interaction object with trigger spec.
  211. */
  212. exports.convertProgressBar = function (options) {
  213. var cfg = options.progressBar;
  214. if (typeof cfg === 'boolean') {
  215. options.progressBar = { enabled: cfg };
  216. }
  217. return options;
  218. };
  219. /**
  220. * Create a callback function to get executed within a specific scope.
  221. *
  222. * @param [ Function ] fn The function to be exec as the callback.
  223. * @param [ Object ] scope The callback function's scope.
  224. *
  225. * @return [ Function ]
  226. */
  227. exports.createCallbackFn = function (fn, scope) {
  228. if (typeof fn != 'function')
  229. return;
  230. return function () {
  231. fn.apply(scope || this, arguments);
  232. };
  233. };
  234. /**
  235. * Convert the IDs to numbers.
  236. *
  237. * @param [ Array ] ids
  238. *
  239. * @return [ Array<Number> ]
  240. */
  241. exports.convertIds = function (ids) {
  242. var convertedIds = [];
  243. for (var i = 0; i < ids.length; i++) {
  244. convertedIds.push(Number(ids[i]));
  245. }
  246. return convertedIds;
  247. };
  248. /**
  249. * First found value for the given keys.
  250. *
  251. * @param [ Object ] options Object with key-value properties.
  252. * @param [ *Array<String> ] keys List of keys.
  253. *
  254. * @return [ Object ]
  255. */
  256. exports.getValueFor = function (options) {
  257. var keys = Array.apply(null, arguments).slice(1);
  258. for (var i = 0; i < keys.length; i++) {
  259. var key = keys[i];
  260. if (options.hasOwnProperty(key)) {
  261. return options[key];
  262. }
  263. }
  264. };
  265. /**
  266. * Fire the event with given arguments.
  267. *
  268. * @param [ String ] event The event's name.
  269. * @param [ *Array] args The callback's arguments.
  270. *
  271. * @return [ Void]
  272. */
  273. exports.fireEvent = function (event) {
  274. var args = Array.apply(null, arguments).slice(1),
  275. listener = this._listener[event];
  276. if (!listener)
  277. return;
  278. for (var i = 0; i < listener.length; i++) {
  279. var fn = listener[i][0],
  280. scope = listener[i][1];
  281. fn.apply(scope, args);
  282. }
  283. };
  284. /**
  285. * Execute the native counterpart.
  286. *
  287. * @param [ String ] action The name of the action.
  288. * @param [ Array ] args Array of arguments.
  289. * @param [ Function] callback The callback function.
  290. * @param [ Object ] scope The scope for the function.
  291. *
  292. * @return [ Void ]
  293. */
  294. exports.exec = function (action, args, callback, scope) {
  295. var fn = this.createCallbackFn(callback, scope),
  296. params = [];
  297. if (Array.isArray(args)) {
  298. params = args;
  299. } else if (args) {
  300. params.push(args);
  301. }
  302. exec(fn, null, 'LocalNotification', action, params);
  303. };
  304. // Called after 'deviceready' event
  305. channel.deviceready.subscribe(function () {
  306. // Device is ready now, the listeners are registered
  307. // and all queued events can be executed.
  308. exports.exec('ready');
  309. });
  310. // Called before 'deviceready' event
  311. channel.onCordovaReady.subscribe(function () {
  312. // Set launchDetails object
  313. exports.exec('launch');
  314. // Device plugin is ready now
  315. channel.onCordovaInfoReady.subscribe(function () {
  316. // Merge platform specifics into defaults
  317. exports.applyPlatformSpecificOptions();
  318. });
  319. });