local-notification-util.js 8.4 KB

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