local-notification.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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. // Defaults
  24. exports._defaults = {
  25. actionGroupId : null,
  26. actions : [],
  27. attachments : [],
  28. autoClear : true,
  29. badge : null,
  30. channel : null,
  31. color : null,
  32. data : null,
  33. defaults : 0,
  34. foreground : false,
  35. group : null,
  36. groupSummary : false,
  37. icon : null,
  38. id : 0,
  39. launch : true,
  40. led : true,
  41. lockscreen : true,
  42. mediaSession : null,
  43. number : 0,
  44. priority : 0,
  45. progressBar : false,
  46. showWhen : true,
  47. silent : false,
  48. smallIcon : 'res://icon',
  49. sound : true,
  50. sticky : false,
  51. summary : null,
  52. text : '',
  53. title : '',
  54. trigger : { type : 'calendar' },
  55. vibrate : false,
  56. wakeup : true
  57. };
  58. // Event listener
  59. exports._listener = {};
  60. /**
  61. * Check permission to show notifications.
  62. *
  63. * @param [ Function ] callback The function to be exec as the callback.
  64. * @param [ Object ] scope The callback function's scope.
  65. *
  66. * @return [ Void ]
  67. */
  68. exports.hasPermission = function (callback, scope) {
  69. this._exec('check', null, callback, scope);
  70. };
  71. /**
  72. * Request permission to show notifications.
  73. *
  74. * @param [ Function ] callback The function to be exec as the callback.
  75. * @param [ Object ] scope The callback function's scope.
  76. *
  77. * @return [ Void ]
  78. */
  79. exports.requestPermission = function (callback, scope) {
  80. this._exec('request', null, callback, scope);
  81. };
  82. /**
  83. * Schedule notifications.
  84. *
  85. * @param [ Array ] notifications The notifications to schedule.
  86. * @param [ Function ] callback The function to be exec as the callback.
  87. * @param [ Object ] scope The callback function's scope.
  88. * @param [ Object ] args Optional flags how to schedule.
  89. *
  90. * @return [ Void ]
  91. */
  92. exports.schedule = function (msgs, callback, scope, args) {
  93. var fn = function (granted) {
  94. var toasts = this._toArray(msgs);
  95. if (!granted && callback) {
  96. callback.call(scope || this, false);
  97. return;
  98. }
  99. for (var i = 0, len = toasts.length; i < len; i++) {
  100. var toast = toasts[i];
  101. this._mergeWithDefaults(toast);
  102. this._convertProperties(toast);
  103. }
  104. this._exec('schedule', toasts, callback, scope);
  105. };
  106. if (args && args.skipPermission) {
  107. fn.call(this, true);
  108. } else {
  109. this.requestPermission(fn, this);
  110. }
  111. };
  112. /**
  113. * Schedule notifications.
  114. *
  115. * @param [ Array ] notifications The notifications to schedule.
  116. * @param [ Function ] callback The function to be exec as the callback.
  117. * @param [ Object ] scope The callback function's scope.
  118. * @param [ Object ] args Optional flags how to schedule.
  119. *
  120. * @return [ Void ]
  121. */
  122. exports.update = function (msgs, callback, scope, args) {
  123. var fn = function(granted) {
  124. var toasts = this._toArray(msgs);
  125. if (!granted && callback) {
  126. callback.call(scope || this, false);
  127. return;
  128. }
  129. for (var i = 0, len = toasts.length; i < len; i++) {
  130. this._convertProperties(toasts[i]);
  131. }
  132. this._exec('update', toasts, callback, scope);
  133. };
  134. if (args && args.skipPermission) {
  135. fn.call(this, true);
  136. } else {
  137. this.requestPermission(fn, this);
  138. }
  139. };
  140. /**
  141. * Clear the specified notifications by id.
  142. *
  143. * @param [ Array<Int> ] ids The IDs of the notifications.
  144. * @param [ Function ] callback The function to be exec as the callback.
  145. * @param [ Object ] scope The callback function's scope.
  146. *
  147. * @return [ Void ]
  148. */
  149. exports.clear = function (ids, callback, scope) {
  150. ids = this._toArray(ids);
  151. ids = this._convertIds(ids);
  152. this._exec('clear', ids, callback, scope);
  153. };
  154. /**
  155. * Clear all triggered notifications.
  156. *
  157. * @param [ Function ] callback The function to be exec as the callback.
  158. * @param [ Object ] scope The callback function's scope.
  159. *
  160. * @return [ Void ]
  161. */
  162. exports.clearAll = function (callback, scope) {
  163. this._exec('clearAll', null, callback, scope);
  164. };
  165. /**
  166. * Clear the specified notifications by id.
  167. *
  168. * @param [ Array<Int> ] ids The IDs of the notifications.
  169. * @param [ Function ] callback The function to be exec as the callback.
  170. * @param [ Object ] scope The callback function's scope.
  171. *
  172. * @return [ Void ]
  173. */
  174. exports.cancel = function (ids, callback, scope) {
  175. ids = this._toArray(ids);
  176. ids = this._convertIds(ids);
  177. this._exec('cancel', ids, callback, scope);
  178. };
  179. /**
  180. * Cancel all scheduled notifications.
  181. *
  182. * @param [ Function ] callback The function to be exec as the callback.
  183. * @param [ Object ] scope The callback function's scope.
  184. *
  185. * @return [ Void ]
  186. */
  187. exports.cancelAll = function (callback, scope) {
  188. this._exec('cancelAll', null, callback, scope);
  189. };
  190. /**
  191. * Check if a notification is present.
  192. *
  193. * @param [ Int ] id The ID of the notification.
  194. * @param [ Function ] callback The function to be exec as the callback.
  195. * @param [ Object ] scope The callback function's scope.
  196. *
  197. * @return [ Void ]
  198. */
  199. exports.isPresent = function (id, callback, scope) {
  200. var fn = this._createCallbackFn(callback, scope);
  201. this.getType(id, function (type) {
  202. fn(type != 'unknown');
  203. });
  204. };
  205. /**
  206. * Check if a notification is scheduled.
  207. *
  208. * @param [ Int ] id The ID of the notification.
  209. * @param [ Function ] callback The function to be exec as the callback.
  210. * @param [ Object ] scope The callback function's scope.
  211. *
  212. * @return [ Void ]
  213. */
  214. exports.isScheduled = function (id, callback, scope) {
  215. this.hasType(id, 'scheduled', callback, scope);
  216. };
  217. /**
  218. * Check if a notification was triggered.
  219. *
  220. * @param [ Int ] id The ID of the notification.
  221. * @param [ Function ] callback The function to be exec as the callback.
  222. * @param [ Object ] scope The callback function's scope.
  223. *
  224. * @return [ Void ]
  225. */
  226. exports.isTriggered = function (id, callback, scope) {
  227. this.hasType(id, 'triggered', callback, scope);
  228. };
  229. /**
  230. * Check if a notification has a given type.
  231. *
  232. * @param [ Int ] id The ID of the notification.
  233. * @param [ String ] type The type of the notification.
  234. * @param [ Function ] callback The function to be exec as the callback.
  235. * @param [ Object ] scope The callback function's scope.
  236. *
  237. * @return [ Void ]
  238. */
  239. exports.hasType = function (id, type, callback, scope) {
  240. var fn = this._createCallbackFn(callback, scope);
  241. this.getType(id, function (type2) {
  242. fn(type == type2);
  243. });
  244. };
  245. /**
  246. * Get the type (triggered, scheduled) for the notification.
  247. *
  248. * @param [ Int ] id The ID of the notification.
  249. * @param [ Function ] callback The function to be exec as the callback.
  250. * @param [ Object ] scope The callback function's scope.
  251. *
  252. * @return [ Void ]
  253. */
  254. exports.getType = function (id, callback, scope) {
  255. this._exec('type', id, callback, scope);
  256. };
  257. /**
  258. * List of all notification ids.
  259. *
  260. * @param [ Function ] callback The function to be exec as the callback.
  261. * @param [ Object ] scope The callback function's scope.
  262. *
  263. * @return [ Void ]
  264. */
  265. exports.getIds = function (callback, scope) {
  266. this._exec('ids', null, callback, scope);
  267. };
  268. /**
  269. * List of all scheduled notification IDs.
  270. *
  271. * @param [ Function ] callback The function to be exec as the callback.
  272. * @param [ Object ] scope The callback function's scope.
  273. *
  274. * @return [ Void ]
  275. */
  276. exports.getScheduledIds = function (callback, scope) {
  277. this._exec('scheduledIds', null, callback, scope);
  278. };
  279. /**
  280. * List of all triggered notification IDs.
  281. *
  282. * @param [ Function ] callback The function to be exec as the callback.
  283. * @param [ Object ] scope The callback function's scope.
  284. *
  285. * @return [ Void ]
  286. */
  287. exports.getTriggeredIds = function (callback, scope) {
  288. this._exec('triggeredIds', null, callback, scope);
  289. };
  290. /**
  291. * List of local notifications specified by id.
  292. * If called without IDs, all notification will be returned.
  293. *
  294. * @param [ Array<Int> ] ids The IDs of the notifications.
  295. * @param [ Function ] callback The function to be exec as the callback.
  296. * @param [ Object ] scope The callback function's scope.
  297. *
  298. * @return [ Void ]
  299. */
  300. exports.get = function () {
  301. var args = Array.apply(null, arguments);
  302. if (typeof args[0] == 'function') {
  303. args.unshift([]);
  304. }
  305. var ids = args[0],
  306. callback = args[1],
  307. scope = args[2];
  308. if (!Array.isArray(ids)) {
  309. this._exec('notification', Number(ids), callback, scope);
  310. return;
  311. }
  312. ids = this._convertIds(ids);
  313. this._exec('notifications', ids, callback, scope);
  314. };
  315. /**
  316. * List for all notifications.
  317. *
  318. * @param [ Function ] callback The function to be exec as the callback.
  319. * @param [ Object ] scope The callback function's scope.
  320. *
  321. * @return [ Void ]
  322. */
  323. exports.getAll = function (callback, scope) {
  324. this._exec('notifications', null, callback, scope);
  325. };
  326. /**
  327. * List of all scheduled notifications.
  328. *
  329. * @param [ Function ] callback The function to be exec as the callback.
  330. * @param [ Object ] scope The callback function's scope.
  331. */
  332. exports.getScheduled = function (callback, scope) {
  333. this._exec('scheduledNotifications', null, callback, scope);
  334. };
  335. /**
  336. * List of all triggered notifications.
  337. *
  338. * @param [ Function ] callback The function to be exec as the callback.
  339. * @param [ Object ] scope The callback function's scope.
  340. */
  341. exports.getTriggered = function (callback, scope) {
  342. this._exec('triggeredNotifications', null, callback, scope);
  343. };
  344. /**
  345. * Register an group of actions by id.
  346. *
  347. * @param [ String ] id The Id of the group.
  348. * @param [ Array] actions The action config settings.
  349. * @param [ Function ] callback The function to be exec as the callback.
  350. * @param [ Object ] scope The callback function's scope.
  351. *
  352. * @return [ Void ]
  353. */
  354. exports.addActionGroup = function (id, actions, callback, scope) {
  355. var config = { actionGroupId: id, actions: actions };
  356. this._exec('actions', config, callback, scope);
  357. };
  358. /**
  359. * The (platform specific) default settings.
  360. *
  361. * @return [ Object ]
  362. */
  363. exports.getDefaults = function () {
  364. var map = Object.assign({}, this._defaults);
  365. for (var key in map) {
  366. if (Array.isArray(map[key])) {
  367. map[key] = Array.from(map[key]);
  368. } else
  369. if (Object.prototype.isPrototypeOf(map[key])) {
  370. map[key] = Object.assign({}, map[key]);
  371. }
  372. }
  373. return map;
  374. };
  375. /**
  376. * Overwrite default settings.
  377. *
  378. * @param [ Object ] newDefaults New default values.
  379. *
  380. * @return [ Void ]
  381. */
  382. exports.setDefaults = function (newDefaults) {
  383. Object.assign(this._defaults, newDefaults);
  384. };
  385. /**
  386. * Register callback for given event.
  387. *
  388. * @param [ String ] event The name of the event.
  389. * @param [ Function ] callback The function to be exec as callback.
  390. * @param [ Object ] scope The callback function's scope.
  391. *
  392. * @return [ Void ]
  393. */
  394. exports.on = function (event, callback, scope) {
  395. if (typeof callback !== "function")
  396. return;
  397. if (!this._listener[event]) {
  398. this._listener[event] = [];
  399. }
  400. var item = [callback, scope || window];
  401. this._listener[event].push(item);
  402. };
  403. /**
  404. * Unregister callback for given event.
  405. *
  406. * @param [ String ] event The name of the event.
  407. * @param [ Function ] callback The function to be exec as callback.
  408. *
  409. * @return [ Void ]
  410. */
  411. exports.un = function (event, callback) {
  412. var listener = this._listener[event];
  413. if (!listener)
  414. return;
  415. for (var i = 0; i < listener.length; i++) {
  416. var fn = listener[i][0];
  417. if (fn == callback) {
  418. listener.splice(i, 1);
  419. break;
  420. }
  421. }
  422. };
  423. /**
  424. * Fire the event with given arguments.
  425. *
  426. * @param [ String ] event The event's name.
  427. * @param [ *Array] args The callback's arguments.
  428. *
  429. * @return [ Void]
  430. */
  431. exports.fireEvent = function (event) {
  432. var args = Array.apply(null, arguments).slice(1),
  433. listener = this._listener[event];
  434. if (!listener)
  435. return;
  436. if (args[0] && typeof args[0].data === 'string') {
  437. args[0].data = JSON.parse(args[0].data);
  438. }
  439. for (var i = 0; i < listener.length; i++) {
  440. var fn = listener[i][0],
  441. scope = listener[i][1];
  442. fn.apply(scope, args);
  443. }
  444. };
  445. /**
  446. * Fire queued events once the device is ready and all listeners are registered.
  447. *
  448. * @return [ Void ]
  449. */
  450. exports.fireQueuedEvents = function() {
  451. exports._exec('ready');
  452. };
  453. /**
  454. * Merge custom properties with the default values.
  455. *
  456. * @param [ Object ] options Set of custom values.
  457. *
  458. * @retrun [ Object ]
  459. */
  460. exports._mergeWithDefaults = function (options) {
  461. var values = this.getDefaults();
  462. if (values.hasOwnProperty('sticky')) {
  463. options.sticky = this._getValueFor(options, 'sticky', 'ongoing');
  464. }
  465. if (options.sticky && options.autoClear !== true) {
  466. options.autoClear = false;
  467. }
  468. Object.assign(values, options);
  469. for (var key in values) {
  470. if (values[key] !== null) {
  471. options[key] = values[key];
  472. } else {
  473. delete options[key];
  474. }
  475. if (!this._defaults.hasOwnProperty(key)) {
  476. console.warn('Unknown property: ' + key);
  477. }
  478. }
  479. options.meta = {
  480. plugin: 'cordova-plugin-local-notification',
  481. version: '0.9-beta.2'
  482. };
  483. return options;
  484. };
  485. /**
  486. * Convert the passed values to their required type.
  487. *
  488. * @param [ Object ] options Properties to convert for.
  489. *
  490. * @return [ Object ] The converted property list
  491. */
  492. exports._convertProperties = function (options) {
  493. var parseToInt = function (prop, options) {
  494. if (isNaN(options[prop])) {
  495. console.warn(prop + ' is not a number: ' + options[prop]);
  496. return this._defaults[prop];
  497. } else {
  498. return Number(options[prop]);
  499. }
  500. };
  501. if (options.id) {
  502. options.id = parseToInt('id', options);
  503. }
  504. if (options.title) {
  505. options.title = options.title.toString();
  506. }
  507. if (options.badge) {
  508. options.badge = parseToInt('badge', options);
  509. }
  510. if (options.priority) {
  511. options.priority = parseToInt('priority', options);
  512. }
  513. if (options.foreground === true) {
  514. options.priority = Math.max(options.priority, 1);
  515. }
  516. if (options.foreground === false) {
  517. options.priority = Math.min(options.priority, 0);
  518. }
  519. if (options.defaults) {
  520. options.defaults = parseToInt('defaults', options);
  521. }
  522. if (options.smallIcon && !options.smallIcon.match(/^res:/)) {
  523. console.warn('Property "smallIcon" must be of kind res://...');
  524. }
  525. options.data = JSON.stringify(options.data);
  526. this._convertTrigger(options);
  527. this._convertActions(options);
  528. this._convertProgressBar(options);
  529. return options;
  530. };
  531. /**
  532. * Convert the passed values to their required type, modifying them
  533. * directly for Android and passing the converted list back for iOS.
  534. *
  535. * @param [ Map ] options Set of custom values.
  536. *
  537. * @return [ Map ] Interaction object with category & actions.
  538. */
  539. exports._convertActions = function (options) {
  540. var actions = [];
  541. if (!options.actions)
  542. return null;
  543. for (var i = 0, len = options.actions.length; i < len; i++) {
  544. var action = options.actions[i];
  545. if (!action.id) {
  546. console.warn('Action with title ' + action.title + ' ' +
  547. 'has no id and will not be added.');
  548. continue;
  549. }
  550. action.id = action.id.toString();
  551. actions.push(action);
  552. }
  553. options.actions = actions;
  554. return options;
  555. };
  556. /**
  557. * Convert the passed values for the trigger to their required type.
  558. *
  559. * @param [ Map ] options Set of custom values.
  560. *
  561. * @return [ Map ] Interaction object with trigger spec.
  562. */
  563. exports._convertTrigger = function (options) {
  564. var trigger = options.trigger || {},
  565. date = this._getValueFor(trigger, 'at', 'firstAt', 'date');
  566. var dateToNum = function (date) {
  567. var num = typeof date == 'object' ? date.getTime() : date;
  568. return Math.round(num);
  569. };
  570. if (!options.trigger)
  571. return;
  572. if (!trigger.type) {
  573. trigger.type = trigger.center ? 'location' : 'calendar';
  574. }
  575. var isCal = trigger.type == 'calendar';
  576. if (isCal && !date) {
  577. date = this._getValueFor(options, 'at', 'firstAt', 'date');
  578. }
  579. if (isCal && !trigger.every && options.every) {
  580. trigger.every = options.every;
  581. }
  582. if (isCal && (trigger.in || trigger.every)) {
  583. date = null;
  584. }
  585. if (isCal && date) {
  586. trigger.at = dateToNum(date);
  587. }
  588. if (isCal && trigger.firstAt) {
  589. trigger.firstAt = dateToNum(trigger.firstAt);
  590. }
  591. if (isCal && trigger.before) {
  592. trigger.before = dateToNum(trigger.before);
  593. }
  594. if (isCal && trigger.after) {
  595. trigger.after = dateToNum(trigger.after);
  596. }
  597. if (!trigger.count && device.platform == 'windows') {
  598. trigger.count = trigger.every ? 5 : 1;
  599. }
  600. if (trigger.count && device.platform == 'iOS') {
  601. console.warn('trigger: { count: } is not supported on iOS.');
  602. }
  603. if (!isCal) {
  604. trigger.notifyOnEntry = !!trigger.notifyOnEntry;
  605. trigger.notifyOnExit = trigger.notifyOnExit === true;
  606. trigger.radius = trigger.radius || 5;
  607. trigger.single = !!trigger.single;
  608. }
  609. if (!isCal || trigger.at) {
  610. delete trigger.every;
  611. }
  612. delete options.every;
  613. delete options.at;
  614. delete options.firstAt;
  615. delete options.date;
  616. options.trigger = trigger;
  617. return options;
  618. };
  619. /**
  620. * Convert the passed values for the progressBar to their required type.
  621. *
  622. * @param [ Map ] options Set of custom values.
  623. *
  624. * @return [ Map ] Interaction object with trigger spec.
  625. */
  626. exports._convertProgressBar = function (options) {
  627. var isAndroid = device.platform == 'Android',
  628. cfg = options.progressBar;
  629. if (cfg === undefined)
  630. return;
  631. if (typeof cfg === 'boolean') {
  632. cfg = options.progressBar = { enabled: cfg };
  633. }
  634. if (typeof cfg.enabled !== 'boolean') {
  635. cfg.enabled = !!(cfg.value || cfg.maxValue || cfg.indeterminate !== null);
  636. }
  637. cfg.value = cfg.value || 0;
  638. if (isAndroid) {
  639. cfg.maxValue = cfg.maxValue || 100;
  640. cfg.indeterminate = !!cfg.indeterminate;
  641. }
  642. cfg.enabled = !!cfg.enabled;
  643. return options;
  644. };
  645. /**
  646. * Create a callback function to get executed within a specific scope.
  647. *
  648. * @param [ Function ] fn The function to be exec as the callback.
  649. * @param [ Object ] scope The callback function's scope.
  650. *
  651. * @return [ Function ]
  652. */
  653. exports._createCallbackFn = function (fn, scope) {
  654. if (typeof fn != 'function')
  655. return;
  656. return function () {
  657. fn.apply(scope || this, arguments);
  658. };
  659. };
  660. /**
  661. * Convert the IDs to numbers.
  662. *
  663. * @param [ Array ] ids
  664. *
  665. * @return [ Array<Number> ]
  666. */
  667. exports._convertIds = function (ids) {
  668. var convertedIds = [];
  669. for (var i = 0, len = ids.length; i < len; i++) {
  670. convertedIds.push(Number(ids[i]));
  671. }
  672. return convertedIds;
  673. };
  674. /**
  675. * First found value for the given keys.
  676. *
  677. * @param [ Object ] options Object with key-value properties.
  678. * @param [ *Array<String> ] keys List of keys.
  679. *
  680. * @return [ Object ]
  681. */
  682. exports._getValueFor = function (options) {
  683. var keys = Array.apply(null, arguments).slice(1);
  684. for (var i = 0, key = keys[i], len = keys.length; i < len; key = keys[++i]) {
  685. if (options.hasOwnProperty(key)) {
  686. return options[key];
  687. }
  688. }
  689. return null;
  690. };
  691. /**
  692. * Convert a value to an array.
  693. *
  694. * @param [ Object ] obj Any kind of object.
  695. *
  696. * @return [ Array ] An array with the object as first item.
  697. */
  698. exports._toArray = function (obj) {
  699. return Array.isArray(obj) ? Array.from(obj) : [obj];
  700. };
  701. /**
  702. * Execute the native counterpart.
  703. *
  704. * @param [ String ] action The name of the action.
  705. * @param [ Array ] args Array of arguments.
  706. * @param [ Function] callback The callback function.
  707. * @param [ Object ] scope The scope for the function.
  708. *
  709. * @return [ Void ]
  710. */
  711. exports._exec = function (action, args, callback, scope) {
  712. var fn = this._createCallbackFn(callback, scope),
  713. params = [];
  714. if (Array.isArray(args)) {
  715. params = args;
  716. } else if (args) {
  717. params.push(args);
  718. }
  719. exec(fn, null, 'LocalNotification', action, params);
  720. };
  721. /**
  722. * Set the launch details if the app was launched by clicking on a toast.
  723. *
  724. * @return [ Void ]
  725. */
  726. exports._setLaunchDetails = function () {
  727. exports._exec('launch', null, function (details) {
  728. if (details) {
  729. exports.launchDetails = details;
  730. }
  731. });
  732. };
  733. // Polyfill for Object.assign
  734. if (typeof Object.assign != 'function') {
  735. Object.assign = function(target) {
  736. 'use strict';
  737. if (target == null) {
  738. throw new TypeError('Cannot convert undefined or null to object');
  739. }
  740. target = Object(target);
  741. for (var index = 1; index < arguments.length; index++) {
  742. var source = arguments[index];
  743. if (source != null) {
  744. for (var key in source) {
  745. if (Object.prototype.hasOwnProperty.call(source, key)) {
  746. target[key] = source[key];
  747. }
  748. }
  749. }
  750. }
  751. return target;
  752. };
  753. }
  754. // Polyfill for Array.from
  755. // Production steps of ECMA-262, Edition 6, 22.1.2.1
  756. // Reference: https://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.from
  757. if (!Array.from) {
  758. Array.from = (function () {
  759. var toStr = Object.prototype.toString;
  760. var isCallable = function (fn) {
  761. return typeof fn === 'function' || toStr.call(fn) === '[object Function]';
  762. };
  763. var toInteger = function (value) {
  764. var number = Number(value);
  765. if (isNaN(number)) { return 0; }
  766. if (number === 0 || !isFinite(number)) { return number; }
  767. return (number > 0 ? 1 : -1) * Math.floor(Math.abs(number));
  768. };
  769. var maxSafeInteger = Math.pow(2, 53) - 1;
  770. var toLength = function (value) {
  771. var len = toInteger(value);
  772. return Math.min(Math.max(len, 0), maxSafeInteger);
  773. };
  774. // The length property of the from method is 1.
  775. return function from(arrayLike/*, mapFn, thisArg */) {
  776. // 1. Let C be the this value.
  777. var C = this;
  778. // 2. Let items be ToObject(arrayLike).
  779. var items = Object(arrayLike);
  780. // 3. ReturnIfAbrupt(items).
  781. if (arrayLike == null) {
  782. throw new TypeError("Array.from requires an array-like object - not null or undefined");
  783. }
  784. // 4. If mapfn is undefined, then let mapping be false.
  785. var mapFn = arguments.length > 1 ? arguments[1] : void undefined;
  786. var T;
  787. if (typeof mapFn !== 'undefined') {
  788. // 5. else
  789. // 5. a If IsCallable(mapfn) is false, throw a TypeError exception.
  790. if (!isCallable(mapFn)) {
  791. throw new TypeError('Array.from: when provided, the second argument must be a function');
  792. }
  793. // 5. b. If thisArg was supplied, let T be thisArg; else let T be undefined.
  794. if (arguments.length > 2) {
  795. T = arguments[2];
  796. }
  797. }
  798. // 10. Let lenValue be Get(items, "length").
  799. // 11. Let len be ToLength(lenValue).
  800. var len = toLength(items.length);
  801. // 13. If IsConstructor(C) is true, then
  802. // 13. a. Let A be the result of calling the [[Construct]] internal method of C with an argument list containing the single item len.
  803. // 14. a. Else, Let A be ArrayCreate(len).
  804. var A = isCallable(C) ? Object(new C(len)) : new Array(len);
  805. // 16. Let k be 0.
  806. var k = 0;
  807. // 17. Repeat, while k < len… (also steps a - h)
  808. var kValue;
  809. while (k < len) {
  810. kValue = items[k];
  811. if (mapFn) {
  812. A[k] = typeof T === 'undefined' ? mapFn(kValue, k) : mapFn.call(T, kValue, k);
  813. } else {
  814. A[k] = kValue;
  815. }
  816. k += 1;
  817. }
  818. // 18. Let putStatus be Put(A, "length", len, true).
  819. A.length = len;
  820. // 20. Return A.
  821. return A;
  822. };
  823. }());
  824. }
  825. // Called after 'deviceready' event
  826. channel.deviceready.subscribe(function () {
  827. if (!window.skipLocalNotificationReady) {
  828. exports.fireQueuedEvents();
  829. }
  830. });
  831. // Called before 'deviceready' event
  832. channel.onCordovaReady.subscribe(function () {
  833. channel.onCordovaInfoReady.subscribe(function () {
  834. exports._setLaunchDetails();
  835. });
  836. });