local-notification.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. /*
  2. * Copyright (c) 2013-2015 by appPlant UG. All rights reserved.
  3. *
  4. * @APPPLANT_LICENSE_HEADER_START@
  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. * @APPPLANT_LICENSE_HEADER_END@
  22. */
  23. var exec = require('cordova/exec'),
  24. channel = require('cordova/channel');
  25. /*************
  26. * INTERFACE *
  27. *************/
  28. /**
  29. * Returns the default settings
  30. *
  31. * @return {Object}
  32. */
  33. exports.getDefaults = function () {
  34. return this._defaults;
  35. };
  36. /**
  37. * Overwrite default settings
  38. *
  39. * @param {Object} defaults
  40. */
  41. exports.setDefaults = function (newDefaults) {
  42. var defaults = this.getDefaults();
  43. for (var key in defaults) {
  44. if (newDefaults.hasOwnProperty(key)) {
  45. defaults[key] = newDefaults[key];
  46. }
  47. }
  48. };
  49. /**
  50. * Schedule a new local notification.
  51. *
  52. * @param {Object} opts
  53. * The notification properties
  54. * @param {Function} callback
  55. * A function to be called after the notification has been canceled
  56. * @param {Object?} scope
  57. * The scope for the callback function
  58. */
  59. exports.schedule = function (opts, callback, scope) {
  60. this.registerPermission(function(granted) {
  61. if (!granted)
  62. return;
  63. var notifications = Array.isArray(opts) ? opts : [opts];
  64. for (var i = 0; i < notifications.length; i++) {
  65. var properties = notifications[i];
  66. this.mergeWithDefaults(properties);
  67. this.convertProperties(properties);
  68. }
  69. this.exec('schedule', notifications, callback, scope);
  70. }, this);
  71. };
  72. /**
  73. * Update existing notifications specified by IDs in options.
  74. *
  75. * @param {Object} options
  76. * The notification properties to update
  77. * @param {Function} callback
  78. * A function to be called after the notification has been updated
  79. * @param {Object?} scope
  80. * The scope for the callback function
  81. */
  82. exports.update = function (opts, callback, scope) {
  83. var notifications = Array.isArray(opts) ? opts : [opts];
  84. for (var i = 0; i < notifications.length; i++) {
  85. var properties = notifications[i];
  86. this.convertProperties(properties);
  87. }
  88. this.exec('update', notifications, callback, scope);
  89. };
  90. /**
  91. * Clear the specified notification.
  92. *
  93. * @param {String} id
  94. * The ID of the notification
  95. * @param {Function} callback
  96. * A function to be called after the notification has been cleared
  97. * @param {Object?} scope
  98. * The scope for the callback function
  99. */
  100. exports.clear = function (ids, callback, scope) {
  101. ids = Array.isArray(ids) ? ids : [ids];
  102. ids = this.convertIds(ids);
  103. this.exec('clear', ids, callback, scope);
  104. };
  105. /**
  106. * Clear all previously sheduled notifications.
  107. *
  108. * @param {Function} callback
  109. * A function to be called after all notifications have been cleared
  110. * @param {Object?} scope
  111. * The scope for the callback function
  112. */
  113. exports.clearAll = function (callback, scope) {
  114. this.exec('clearAll', null, callback, scope);
  115. };
  116. /**
  117. * Cancel the specified notifications.
  118. *
  119. * @param {String[]} ids
  120. * The IDs of the notifications
  121. * @param {Function} callback
  122. * A function to be called after the notifications has been canceled
  123. * @param {Object?} scope
  124. * The scope for the callback function
  125. */
  126. exports.cancel = function (ids, callback, scope) {
  127. ids = Array.isArray(ids) ? ids : [ids];
  128. ids = this.convertIds(ids);
  129. this.exec('cancel', ids, callback, scope);
  130. };
  131. /**
  132. * Remove all previously registered notifications.
  133. *
  134. * @param {Function} callback
  135. * A function to be called after all notifications have been canceled
  136. * @param {Object?} scope
  137. * The scope for the callback function
  138. */
  139. exports.cancelAll = function (callback, scope) {
  140. this.exec('cancelAll', null, callback, scope);
  141. };
  142. /**
  143. * Check if a notification with an ID is present.
  144. *
  145. * @param {String} id
  146. * The ID of the notification
  147. * @param {Function} callback
  148. * A callback function to be called with the list
  149. * @param {Object?} scope
  150. * The scope for the callback function
  151. */
  152. exports.isPresent = function (id, callback, scope) {
  153. var notId = (id || '0').toString();
  154. this.exec('isPresent', notId, callback, scope);
  155. };
  156. /**
  157. * Check if a notification with an ID is scheduled.
  158. *
  159. * @param {String} id
  160. * The ID of the notification
  161. * @param {Function} callback
  162. * A callback function to be called with the list
  163. * @param {Object?} scope
  164. * The scope for the callback function
  165. */
  166. exports.isScheduled = function (id, callback, scope) {
  167. var notId = (id || '0').toString();
  168. this.exec('isScheduled', notId, callback, scope);
  169. };
  170. /**
  171. * Check if a notification with an ID was triggered.
  172. *
  173. * @param {String} id
  174. * The ID of the notification
  175. * @param {Function} callback
  176. * A callback function to be called with the list
  177. * @param {Object?} scope
  178. * The scope for the callback function
  179. */
  180. exports.isTriggered = function (id, callback, scope) {
  181. var notId = (id || '0').toString();
  182. this.exec('isTriggered', notId, callback, scope);
  183. };
  184. /**
  185. * List all local notification IDs.
  186. *
  187. * @param {Function} callback
  188. * A callback function to be called with the list
  189. * @param {Object?} scope
  190. * The scope for the callback function
  191. */
  192. exports.getAllIds = function (callback, scope) {
  193. this.exec('getAllIds', null, callback, scope);
  194. };
  195. /**
  196. * Alias for `getAllIds`.
  197. */
  198. exports.getIds = function () {
  199. this.getAllIds.apply(this, arguments);
  200. };
  201. /**
  202. * List all scheduled notification IDs.
  203. *
  204. * @param {Function} callback
  205. * A callback function to be called with the list
  206. * @param {Object?} scope
  207. * The scope for the callback function
  208. */
  209. exports.getScheduledIds = function (callback, scope) {
  210. this.exec('getScheduledIds', null, callback, scope);
  211. };
  212. /**
  213. * List all triggered notification IDs.
  214. *
  215. * @param {Function} callback
  216. * A callback function to be called with the list
  217. * @param {Object?} scope
  218. * The scope for the callback function
  219. */
  220. exports.getTriggeredIds = function (callback, scope) {
  221. this.exec('getTriggeredIds', null, callback, scope);
  222. };
  223. /**
  224. * Property list for given local notifications.
  225. * If called without IDs, all notification will be returned.
  226. *
  227. * @param {Number[]?} ids
  228. * Set of notification IDs
  229. * @param {Function} callback
  230. * A callback function to be called with the list
  231. * @param {Object?} scope
  232. * The scope for the callback function
  233. */
  234. exports.get = function () {
  235. var args = Array.apply(null, arguments);
  236. if (typeof args[0] == 'function') {
  237. args.unshift([]);
  238. }
  239. var ids = args[0],
  240. callback = args[1],
  241. scope = args[2];
  242. if (!Array.isArray(ids)) {
  243. ids = [ids];
  244. }
  245. ids = this.convertIds(ids);
  246. this.exec('getAll', ids, callback, scope);
  247. };
  248. /**
  249. * Property list for all local notifications.
  250. *
  251. * @param {Function} callback
  252. * A callback function to be called with the list
  253. * @param {Object?} scope
  254. * The scope for the callback function
  255. */
  256. exports.getAll = function (callback, scope) {
  257. this.exec('getAll', null, callback, scope);
  258. };
  259. /**
  260. * Property list for given scheduled notifications.
  261. * If called without IDs, all notification will be returned.
  262. *
  263. * @param {Number[]?} ids
  264. * Set of notification IDs
  265. * @param {Function} callback
  266. * A callback function to be called with the list
  267. * @param {Object?} scope
  268. * The scope for the callback function
  269. */
  270. exports.getScheduled = function () {
  271. var args = Array.apply(null, arguments);
  272. if (typeof args[0] == 'function') {
  273. args.unshift([]);
  274. }
  275. var ids = args[0],
  276. callback = args[1],
  277. scope = args[2];
  278. if (!Array.isArray(ids)) {
  279. ids = [ids];
  280. }
  281. ids = this.convertIds(ids);
  282. this.exec('getScheduled', ids, callback, scope);
  283. };
  284. /**
  285. * Retrieve the properties for all scheduled notifications.
  286. *
  287. * @param {Function} callback
  288. * A callback function to be called with the list
  289. * @param {Object?} scope
  290. * The scope for the callback function
  291. */
  292. exports.getAllScheduled = function (callback, scope) {
  293. this.exec('getScheduled', null, callback, scope);
  294. };
  295. /**
  296. * Property list for given triggered notifications.
  297. * If called without IDs, all notification will be returned.
  298. *
  299. * @param {Number[]?} ids
  300. * Set of notification IDs
  301. * @param {Function} callback
  302. * A callback function to be called with the list
  303. * @param {Object?} scope
  304. * The scope for the callback function
  305. */
  306. exports.getTriggered = function () {
  307. var args = Array.apply(null, arguments);
  308. if (typeof args[0] == 'function') {
  309. args.unshift([]);
  310. }
  311. var ids = args[0],
  312. callback = args[1],
  313. scope = args[2];
  314. if (!Array.isArray(ids)) {
  315. ids = [ids];
  316. }
  317. ids = this.convertIds(ids);
  318. this.exec('getTriggered', ids, callback, scope);
  319. };
  320. /**
  321. * Retrieve the properties for all triggered notifications.
  322. *
  323. * @param {Function} callback
  324. * A callback function to be called with the list
  325. * @param {Object?} scope
  326. * The scope for the callback function
  327. */
  328. exports.getAllTriggered = function (callback, scope) {
  329. this.exec('getTriggered', null, callback, scope);
  330. };
  331. /**
  332. * Informs if the app has the permission to show notifications.
  333. *
  334. * @param {Function} callback
  335. * The function to be exec as the callback
  336. * @param {Object?} scope
  337. * The callback function's scope
  338. */
  339. exports.hasPermission = function (callback, scope) {
  340. var fn = this.createCallbackFn(callback, scope);
  341. if (device.platform != 'iOS') {
  342. fn(true);
  343. return;
  344. }
  345. exec(fn, null, 'LocalNotification', 'hasPermission', []);
  346. };
  347. /**
  348. * Register permission to show notifications if not already granted.
  349. *
  350. * @param {Function} callback
  351. * The function to be exec as the callback
  352. * @param {Object?} scope
  353. * The callback function's scope
  354. */
  355. exports.registerPermission = function (callback, scope) {
  356. var fn = this.createCallbackFn(callback, scope);
  357. if (device.platform != 'iOS') {
  358. fn(true);
  359. return;
  360. }
  361. exec(fn, null, 'LocalNotification', 'registerPermission', []);
  362. };
  363. /**
  364. * @deprecated
  365. *
  366. * Register permission to show notifications if not already granted.
  367. *
  368. * @param {Function} callback
  369. * The function to be exec as the callback
  370. * @param {Object?} scope
  371. * The callback function's scope
  372. */
  373. exports.promptForPermission = function (callback, scope) {
  374. console.warn('Depreated: Please use `notification.local.registerPermission` instead.');
  375. exports.registerPermission.apply(this, arguments);
  376. };
  377. /**********
  378. * EVENTS *
  379. **********/
  380. /**
  381. * Register callback for given event.
  382. *
  383. * @param {String} event
  384. * The event's name
  385. * @param {Function} callback
  386. * The function to be exec as callback
  387. * @param {Object?} scope
  388. * The callback function's scope
  389. */
  390. exports.on = function (event, callback, scope) {
  391. if (!this._listener[event]) {
  392. this._listener[event] = [];
  393. }
  394. var item = [callback, scope || window];
  395. this._listener[event].push(item);
  396. };
  397. /**
  398. * Unregister callback for given event.
  399. *
  400. * @param {String} event
  401. * The event's name
  402. * @param {Function} callback
  403. * The function to be exec as callback
  404. */
  405. exports.un = function (event, callback) {
  406. var listener = this._listener[event];
  407. if (!listener)
  408. return;
  409. for (var i = 0; i < listener.length; i++) {
  410. var fn = listener[i][0];
  411. if (fn == callback) {
  412. listener.splice(i, 1);
  413. break;
  414. }
  415. }
  416. };
  417. /***********
  418. * MEMBERS *
  419. ***********/
  420. // Default values
  421. exports._defaults = {
  422. text: '',
  423. title: '',
  424. sound: 'res://platform_default',
  425. badge: 0,
  426. id: 0,
  427. data: undefined,
  428. every: undefined,
  429. at: undefined
  430. };
  431. // listener
  432. exports._listener = {};
  433. /***********
  434. * PRIVATE *
  435. ***********/
  436. /**
  437. * Merge custom properties with the default values.
  438. *
  439. * @param {Object} options
  440. * Set of custom values
  441. *
  442. * @retrun {Object}
  443. * The merged property list
  444. */
  445. exports.mergeWithDefaults = function (options) {
  446. var defaults = this.getDefaults();
  447. options.at = this.getValueFor(options, 'at', 'firstAt', 'date');
  448. options.text = this.getValueFor(options, 'text', 'message');
  449. options.data = this.getValueFor(options, 'data', 'json');
  450. if (options.at === undefined || options.at === null) {
  451. options.at = new Date();
  452. }
  453. for (var key in defaults) {
  454. if (options[key] === null || options[key] === undefined) {
  455. if (options.hasOwnProperty(key) && ['data','sound'].indexOf(key) > -1) {
  456. options[key] = undefined;
  457. } else {
  458. options[key] = defaults[key];
  459. }
  460. }
  461. }
  462. for (key in options) {
  463. if (!defaults.hasOwnProperty(key)) {
  464. delete options[key];
  465. }
  466. }
  467. return options;
  468. };
  469. /**
  470. * Convert the passed values to their required type.
  471. *
  472. * @param {Object} options
  473. * Set of custom values
  474. *
  475. * @retrun {Object}
  476. * The converted property list
  477. */
  478. exports.convertProperties = function (options) {
  479. if (options.id) {
  480. if (isNaN(options.id)) {
  481. options.id = this.getDefaults().id;
  482. } else {
  483. options.id = options.id.toString();
  484. }
  485. }
  486. if (options.title) {
  487. options.title = options.title.toString();
  488. }
  489. if (options.text) {
  490. options.text = options.text.toString();
  491. }
  492. if (options.badge) {
  493. if (isNaN(options.badge)) {
  494. options.badge = this.getDefaults().badge;
  495. } else {
  496. options.badge = Number(options.badge);
  497. }
  498. }
  499. if (typeof options.at == 'object') {
  500. options.at = Math.round(options.at.getTime()/1000);
  501. }
  502. if (typeof options.data == 'object') {
  503. options.data = JSON.stringify(options.data);
  504. }
  505. return options;
  506. };
  507. /**
  508. * Merge platform specific properties into the default ones.
  509. *
  510. * @return {Object}
  511. * The default properties for the platform
  512. */
  513. exports.applyPlatformSpecificOptions = function () {
  514. var defaults = this._defaults;
  515. switch (device.platform) {
  516. case 'Android':
  517. defaults.icon = 'res://ic_popup_reminder';
  518. defaults.smallIcon = 'res://ic_popup_reminder';
  519. defaults.ongoing = false;
  520. defaults.led = 'FFFFFF';
  521. break;
  522. }
  523. return defaults;
  524. };
  525. /**
  526. * Create callback, which will be executed within a specific scope.
  527. *
  528. * @param {Function} callbackFn
  529. * The callback function
  530. * @param {Object} scope
  531. * The scope for the function
  532. *
  533. * @return {Function}
  534. * The new callback function
  535. */
  536. exports.createCallbackFn = function (callbackFn, scope) {
  537. if (typeof callbackFn != 'function')
  538. return;
  539. return function () {
  540. callbackFn.apply(scope || this, arguments);
  541. };
  542. };
  543. /**
  544. * Convert the IDs to Strings.
  545. *
  546. * @param {String/Number[]} ids
  547. *
  548. * @return Array of Strings
  549. */
  550. exports.convertIds = function (ids) {
  551. var convertedIds = [];
  552. for (var i = 0; i < ids.length; i++) {
  553. convertedIds.push(ids[i].toString());
  554. }
  555. return convertedIds;
  556. };
  557. /**
  558. * First found value for the given keys.
  559. *
  560. * @param {Object} options
  561. * Object with key-value properties
  562. * @param {String[]} keys*
  563. * Key list
  564. */
  565. exports.getValueFor = function (options) {
  566. var keys = Array.apply(null, arguments).slice(1);
  567. for (var i = 0; i < keys.length; i++) {
  568. var key = keys[i];
  569. if (options.hasOwnProperty(key)) {
  570. return options[key];
  571. }
  572. }
  573. };
  574. /**
  575. * Fire event with given arguments.
  576. *
  577. * @param {String} event
  578. * The event's name
  579. * @param {args*}
  580. * The callback's arguments
  581. */
  582. exports.fireEvent = function (event) {
  583. var args = Array.apply(null, arguments).slice(1),
  584. listener = this._listener[event];
  585. if (!listener)
  586. return;
  587. for (var i = 0; i < listener.length; i++) {
  588. var fn = listener[i][0],
  589. scope = listener[i][1];
  590. fn.apply(scope, args);
  591. }
  592. };
  593. /**
  594. * Execute the native counterpart.
  595. *
  596. * @param {String} action
  597. * The name of the action
  598. * @param args[]
  599. * Array of arguments
  600. * @param {Function} callback
  601. * The callback function
  602. * @param {Object} scope
  603. * The scope for the function
  604. */
  605. exports.exec = function (action, args, callback, scope) {
  606. var fn = this.createCallbackFn(callback, scope),
  607. params = [];
  608. if (Array.isArray(args)) {
  609. params = args;
  610. } else if (args) {
  611. params.push(args);
  612. }
  613. exec(fn, null, 'LocalNotification', action, params);
  614. };
  615. /*********
  616. * HOOKS *
  617. *********/
  618. // Called after 'deviceready' event
  619. channel.deviceready.subscribe(function () {
  620. // Device is ready now, the listeners are registered
  621. // and all queued events can be executed.
  622. exec(null, null, 'LocalNotification', 'deviceready', []);
  623. });
  624. // Called before 'deviceready' event
  625. channel.onCordovaReady.subscribe(function () {
  626. // Device plugin is ready now
  627. channel.onCordovaInfoReady.subscribe(function () {
  628. // Merge platform specifics into defaults
  629. exports.applyPlatformSpecificOptions();
  630. });
  631. });