local-notification.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  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. * Merge custom properties with the default values.
  447. *
  448. * @param [ Object ] options Set of custom values.
  449. *
  450. * @retrun [ Object ]
  451. */
  452. exports._mergeWithDefaults = function (options) {
  453. var values = this.getDefaults();
  454. if (values.hasOwnProperty('sticky')) {
  455. options.sticky = this._getValueFor(options, 'sticky', 'ongoing');
  456. }
  457. if (options.sticky && options.autoClear !== true) {
  458. options.autoClear = false;
  459. }
  460. Object.assign(values, options);
  461. for (var key in values) {
  462. if (values[key] !== null) {
  463. options[key] = values[key];
  464. } else {
  465. delete options[key];
  466. }
  467. if (!this._defaults.hasOwnProperty(key)) {
  468. console.warn('Unknown property: ' + key);
  469. }
  470. }
  471. options.meta = {
  472. plugin: 'cordova-plugin-local-notification',
  473. version: '0.9-beta.2'
  474. };
  475. return options;
  476. };
  477. /**
  478. * Convert the passed values to their required type.
  479. *
  480. * @param [ Object ] options Properties to convert for.
  481. *
  482. * @return [ Object ] The converted property list
  483. */
  484. exports._convertProperties = function (options) {
  485. var parseToInt = function (prop, options) {
  486. if (isNaN(options[prop])) {
  487. console.warn(prop + ' is not a number: ' + options[prop]);
  488. return this._defaults[prop];
  489. } else {
  490. return Number(options[prop]);
  491. }
  492. };
  493. if (options.id) {
  494. options.id = parseToInt('id', options);
  495. }
  496. if (options.title) {
  497. options.title = options.title.toString();
  498. }
  499. if (options.badge) {
  500. options.badge = parseToInt('badge', options);
  501. }
  502. if (options.priority) {
  503. options.priority = parseToInt('priority', options);
  504. }
  505. if (options.foreground === true) {
  506. options.priority = Math.max(options.priority, 1);
  507. }
  508. if (options.foreground === false) {
  509. options.priority = Math.min(options.priority, 0);
  510. }
  511. if (options.defaults) {
  512. options.defaults = parseToInt('defaults', options);
  513. }
  514. if (options.smallIcon && !options.smallIcon.match(/^res:/)) {
  515. console.warn('Property "smallIcon" must be of kind res://...');
  516. }
  517. options.data = JSON.stringify(options.data);
  518. this._convertTrigger(options);
  519. this._convertActions(options);
  520. this._convertProgressBar(options);
  521. return options;
  522. };
  523. /**
  524. * Convert the passed values to their required type, modifying them
  525. * directly for Android and passing the converted list back for iOS.
  526. *
  527. * @param [ Map ] options Set of custom values.
  528. *
  529. * @return [ Map ] Interaction object with category & actions.
  530. */
  531. exports._convertActions = function (options) {
  532. var actions = [];
  533. if (!options.actions)
  534. return null;
  535. for (var i = 0, len = options.actions.length; i < len; i++) {
  536. var action = options.actions[i];
  537. if (!action.id) {
  538. console.warn('Action with title ' + action.title + ' ' +
  539. 'has no id and will not be added.');
  540. continue;
  541. }
  542. action.id = action.id.toString();
  543. actions.push(action);
  544. }
  545. options.actions = actions;
  546. return options;
  547. };
  548. /**
  549. * Convert the passed values for the trigger to their required type.
  550. *
  551. * @param [ Map ] options Set of custom values.
  552. *
  553. * @return [ Map ] Interaction object with trigger spec.
  554. */
  555. exports._convertTrigger = function (options) {
  556. var trigger = options.trigger || {},
  557. date = this._getValueFor(trigger, 'at', 'firstAt', 'date');
  558. var dateToNum = function (date) {
  559. var num = typeof date == 'object' ? date.getTime() : date;
  560. return Math.round(num);
  561. };
  562. if (!options.trigger)
  563. return;
  564. if (!trigger.type) {
  565. trigger.type = trigger.center ? 'location' : 'calendar';
  566. }
  567. var isCal = trigger.type == 'calendar';
  568. if (isCal && !date) {
  569. date = this._getValueFor(options, 'at', 'firstAt', 'date');
  570. }
  571. if (isCal && !trigger.every && options.every) {
  572. trigger.every = options.every;
  573. }
  574. if (isCal && (trigger.in || trigger.every)) {
  575. date = null;
  576. }
  577. if (isCal && date) {
  578. trigger.at = dateToNum(date);
  579. }
  580. if (isCal && trigger.firstAt) {
  581. trigger.firstAt = dateToNum(trigger.firstAt);
  582. }
  583. if (isCal && trigger.before) {
  584. trigger.before = dateToNum(trigger.before);
  585. }
  586. if (isCal && trigger.after) {
  587. trigger.after = dateToNum(trigger.after);
  588. }
  589. if (!trigger.count && device.platform == 'windows') {
  590. trigger.count = trigger.every ? 5 : 1;
  591. }
  592. if (trigger.count && device.platform == 'iOS') {
  593. console.warn('trigger: { count: } is not supported on iOS.');
  594. }
  595. if (!isCal) {
  596. trigger.notifyOnEntry = !!trigger.notifyOnEntry;
  597. trigger.notifyOnExit = trigger.notifyOnExit === true;
  598. trigger.radius = trigger.radius || 5;
  599. trigger.single = !!trigger.single;
  600. }
  601. if (!isCal || trigger.at) {
  602. delete trigger.every;
  603. }
  604. delete options.every;
  605. delete options.at;
  606. delete options.firstAt;
  607. delete options.date;
  608. options.trigger = trigger;
  609. return options;
  610. };
  611. /**
  612. * Convert the passed values for the progressBar to their required type.
  613. *
  614. * @param [ Map ] options Set of custom values.
  615. *
  616. * @return [ Map ] Interaction object with trigger spec.
  617. */
  618. exports._convertProgressBar = function (options) {
  619. var isAndroid = device.platform == 'Android',
  620. cfg = options.progressBar;
  621. if (cfg === undefined)
  622. return;
  623. if (typeof cfg === 'boolean') {
  624. cfg = options.progressBar = { enabled: cfg };
  625. }
  626. if (typeof cfg.enabled !== 'boolean') {
  627. cfg.enabled = !!(cfg.value || cfg.maxValue || cfg.indeterminate !== null);
  628. }
  629. cfg.value = cfg.value || 0;
  630. if (isAndroid) {
  631. cfg.maxValue = cfg.maxValue || 100;
  632. cfg.indeterminate = !!cfg.indeterminate;
  633. }
  634. cfg.enabled = !!cfg.enabled;
  635. return options;
  636. };
  637. /**
  638. * Create a callback function to get executed within a specific scope.
  639. *
  640. * @param [ Function ] fn The function to be exec as the callback.
  641. * @param [ Object ] scope The callback function's scope.
  642. *
  643. * @return [ Function ]
  644. */
  645. exports._createCallbackFn = function (fn, scope) {
  646. if (typeof fn != 'function')
  647. return;
  648. return function () {
  649. fn.apply(scope || this, arguments);
  650. };
  651. };
  652. /**
  653. * Convert the IDs to numbers.
  654. *
  655. * @param [ Array ] ids
  656. *
  657. * @return [ Array<Number> ]
  658. */
  659. exports._convertIds = function (ids) {
  660. var convertedIds = [];
  661. for (var i = 0, len = ids.length; i < len; i++) {
  662. convertedIds.push(Number(ids[i]));
  663. }
  664. return convertedIds;
  665. };
  666. /**
  667. * First found value for the given keys.
  668. *
  669. * @param [ Object ] options Object with key-value properties.
  670. * @param [ *Array<String> ] keys List of keys.
  671. *
  672. * @return [ Object ]
  673. */
  674. exports._getValueFor = function (options) {
  675. var keys = Array.apply(null, arguments).slice(1);
  676. for (var i = 0, key = keys[i], len = keys.length; i < len; key = keys[++i]) {
  677. if (options.hasOwnProperty(key)) {
  678. return options[key];
  679. }
  680. }
  681. return null;
  682. };
  683. /**
  684. * Convert a value to an array.
  685. *
  686. * @param [ Object ] obj Any kind of object.
  687. *
  688. * @return [ Array ] An array with the object as first item.
  689. */
  690. exports._toArray = function (obj) {
  691. return Array.isArray(obj) ? Array.from(obj) : [obj];
  692. };
  693. /**
  694. * Execute the native counterpart.
  695. *
  696. * @param [ String ] action The name of the action.
  697. * @param [ Array ] args Array of arguments.
  698. * @param [ Function] callback The callback function.
  699. * @param [ Object ] scope The scope for the function.
  700. *
  701. * @return [ Void ]
  702. */
  703. exports._exec = function (action, args, callback, scope) {
  704. var fn = this._createCallbackFn(callback, scope),
  705. params = [];
  706. if (Array.isArray(args)) {
  707. params = args;
  708. } else if (args) {
  709. params.push(args);
  710. }
  711. exec(fn, null, 'LocalNotification', action, params);
  712. };
  713. /**
  714. * Set the launch details if the app was launched by clicking on a toast.
  715. *
  716. * @return [ Void ]
  717. */
  718. exports._setLaunchDetails = function () {
  719. exports._exec('launch', null, function (details) {
  720. if (details) {
  721. exports.launchDetails = details;
  722. }
  723. });
  724. };
  725. // Polyfill for Object.assign
  726. if (typeof Object.assign != 'function') {
  727. Object.assign = function(target) {
  728. 'use strict';
  729. if (target == null) {
  730. throw new TypeError('Cannot convert undefined or null to object');
  731. }
  732. target = Object(target);
  733. for (var index = 1; index < arguments.length; index++) {
  734. var source = arguments[index];
  735. if (source != null) {
  736. for (var key in source) {
  737. if (Object.prototype.hasOwnProperty.call(source, key)) {
  738. target[key] = source[key];
  739. }
  740. }
  741. }
  742. }
  743. return target;
  744. };
  745. }
  746. // Polyfill for Array.from
  747. // Production steps of ECMA-262, Edition 6, 22.1.2.1
  748. // Reference: https://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.from
  749. if (!Array.from) {
  750. Array.from = (function () {
  751. var toStr = Object.prototype.toString;
  752. var isCallable = function (fn) {
  753. return typeof fn === 'function' || toStr.call(fn) === '[object Function]';
  754. };
  755. var toInteger = function (value) {
  756. var number = Number(value);
  757. if (isNaN(number)) { return 0; }
  758. if (number === 0 || !isFinite(number)) { return number; }
  759. return (number > 0 ? 1 : -1) * Math.floor(Math.abs(number));
  760. };
  761. var maxSafeInteger = Math.pow(2, 53) - 1;
  762. var toLength = function (value) {
  763. var len = toInteger(value);
  764. return Math.min(Math.max(len, 0), maxSafeInteger);
  765. };
  766. // The length property of the from method is 1.
  767. return function from(arrayLike/*, mapFn, thisArg */) {
  768. // 1. Let C be the this value.
  769. var C = this;
  770. // 2. Let items be ToObject(arrayLike).
  771. var items = Object(arrayLike);
  772. // 3. ReturnIfAbrupt(items).
  773. if (arrayLike == null) {
  774. throw new TypeError("Array.from requires an array-like object - not null or undefined");
  775. }
  776. // 4. If mapfn is undefined, then let mapping be false.
  777. var mapFn = arguments.length > 1 ? arguments[1] : void undefined;
  778. var T;
  779. if (typeof mapFn !== 'undefined') {
  780. // 5. else
  781. // 5. a If IsCallable(mapfn) is false, throw a TypeError exception.
  782. if (!isCallable(mapFn)) {
  783. throw new TypeError('Array.from: when provided, the second argument must be a function');
  784. }
  785. // 5. b. If thisArg was supplied, let T be thisArg; else let T be undefined.
  786. if (arguments.length > 2) {
  787. T = arguments[2];
  788. }
  789. }
  790. // 10. Let lenValue be Get(items, "length").
  791. // 11. Let len be ToLength(lenValue).
  792. var len = toLength(items.length);
  793. // 13. If IsConstructor(C) is true, then
  794. // 13. a. Let A be the result of calling the [[Construct]] internal method of C with an argument list containing the single item len.
  795. // 14. a. Else, Let A be ArrayCreate(len).
  796. var A = isCallable(C) ? Object(new C(len)) : new Array(len);
  797. // 16. Let k be 0.
  798. var k = 0;
  799. // 17. Repeat, while k < len… (also steps a - h)
  800. var kValue;
  801. while (k < len) {
  802. kValue = items[k];
  803. if (mapFn) {
  804. A[k] = typeof T === 'undefined' ? mapFn(kValue, k) : mapFn.call(T, kValue, k);
  805. } else {
  806. A[k] = kValue;
  807. }
  808. k += 1;
  809. }
  810. // 18. Let putStatus be Put(A, "length", len, true).
  811. A.length = len;
  812. // 20. Return A.
  813. return A;
  814. };
  815. }());
  816. }
  817. // Called after 'deviceready' event
  818. channel.deviceready.subscribe(function () {
  819. exports._exec('ready');
  820. });
  821. // Called before 'deviceready' event
  822. channel.onCordovaReady.subscribe(function () {
  823. channel.onCordovaInfoReady.subscribe(function () {
  824. exports._setLaunchDetails();
  825. });
  826. });