local-notification-core.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. /********
  25. * CORE *
  26. ********/
  27. /**
  28. * Returns the default settings.
  29. *
  30. * @return {Object}
  31. */
  32. exports.getDefaults = function () {
  33. return this._defaults;
  34. };
  35. /**
  36. * Overwrite default settings.
  37. *
  38. * @param {Object} defaults
  39. */
  40. exports.setDefaults = function (newDefaults) {
  41. var defaults = this.getDefaults();
  42. for (var key in defaults) {
  43. if (newDefaults.hasOwnProperty(key)) {
  44. defaults[key] = newDefaults[key];
  45. }
  46. }
  47. };
  48. /**
  49. * Schedule a new local notification.
  50. *
  51. * @param {Object} opts
  52. * The notification properties
  53. * @param {Function} callback
  54. * A function to be called after the notification has been canceled
  55. * @param {Object?} scope
  56. * The scope for the callback function
  57. */
  58. exports.schedule = function (opts, callback, scope) {
  59. this.registerPermission(function(granted) {
  60. if (!granted)
  61. return;
  62. var notifications = Array.isArray(opts) ? opts : [opts];
  63. for (var i = 0; i < notifications.length; i++) {
  64. var properties = notifications[i];
  65. this.mergeWithDefaults(properties);
  66. this.convertProperties(properties);
  67. }
  68. this.exec('schedule', notifications, callback, scope);
  69. }, this);
  70. };
  71. /**
  72. * Update existing notifications specified by IDs in options.
  73. *
  74. * @param {Object} options
  75. * The notification properties to update
  76. * @param {Function} callback
  77. * A function to be called after the notification has been updated
  78. * @param {Object?} scope
  79. * The scope for the callback function
  80. */
  81. exports.update = function (opts, callback, scope) {
  82. var notifications = Array.isArray(opts) ? opts : [opts];
  83. for (var i = 0; i < notifications.length; i++) {
  84. var properties = notifications[i];
  85. this.convertProperties(properties);
  86. }
  87. this.exec('update', notifications, callback, scope);
  88. };
  89. /**
  90. * Clear the specified notification.
  91. *
  92. * @param {String} id
  93. * The ID of the notification
  94. * @param {Function} callback
  95. * A function to be called after the notification has been cleared
  96. * @param {Object?} scope
  97. * The scope for the callback function
  98. */
  99. exports.clear = function (ids, callback, scope) {
  100. ids = Array.isArray(ids) ? ids : [ids];
  101. ids = this.convertIds(ids);
  102. this.exec('clear', ids, callback, scope);
  103. };
  104. /**
  105. * Clear all previously sheduled notifications.
  106. *
  107. * @param {Function} callback
  108. * A function to be called after all notifications have been cleared
  109. * @param {Object?} scope
  110. * The scope for the callback function
  111. */
  112. exports.clearAll = function (callback, scope) {
  113. this.exec('clearAll', null, callback, scope);
  114. };
  115. /**
  116. * Cancel the specified notifications.
  117. *
  118. * @param {String[]} ids
  119. * The IDs of the notifications
  120. * @param {Function} callback
  121. * A function to be called after the notifications has been canceled
  122. * @param {Object?} scope
  123. * The scope for the callback function
  124. */
  125. exports.cancel = function (ids, callback, scope) {
  126. ids = Array.isArray(ids) ? ids : [ids];
  127. ids = this.convertIds(ids);
  128. this.exec('cancel', ids, callback, scope);
  129. };
  130. /**
  131. * Remove all previously registered notifications.
  132. *
  133. * @param {Function} callback
  134. * A function to be called after all notifications have been canceled
  135. * @param {Object?} scope
  136. * The scope for the callback function
  137. */
  138. exports.cancelAll = function (callback, scope) {
  139. this.exec('cancelAll', null, callback, scope);
  140. };
  141. /**
  142. * Check if a notification with an ID is present.
  143. *
  144. * @param {String} id
  145. * The ID of the notification
  146. * @param {Function} callback
  147. * A callback function to be called with the list
  148. * @param {Object?} scope
  149. * The scope for the callback function
  150. */
  151. exports.isPresent = function (id, callback, scope) {
  152. var notId = (id || '0').toString();
  153. this.exec('isPresent', notId, callback, scope);
  154. };
  155. /**
  156. * Check if a notification with an ID is scheduled.
  157. *
  158. * @param {String} id
  159. * The ID of the notification
  160. * @param {Function} callback
  161. * A callback function to be called with the list
  162. * @param {Object?} scope
  163. * The scope for the callback function
  164. */
  165. exports.isScheduled = function (id, callback, scope) {
  166. var notId = (id || '0').toString();
  167. this.exec('isScheduled', notId, callback, scope);
  168. };
  169. /**
  170. * Check if a notification with an ID was triggered.
  171. *
  172. * @param {String} id
  173. * The ID of the notification
  174. * @param {Function} callback
  175. * A callback function to be called with the list
  176. * @param {Object?} scope
  177. * The scope for the callback function
  178. */
  179. exports.isTriggered = function (id, callback, scope) {
  180. var notId = (id || '0').toString();
  181. this.exec('isTriggered', notId, callback, scope);
  182. };
  183. /**
  184. * List all local notification IDs.
  185. *
  186. * @param {Function} callback
  187. * A callback function to be called with the list
  188. * @param {Object?} scope
  189. * The scope for the callback function
  190. */
  191. exports.getAllIds = function (callback, scope) {
  192. this.exec('getAllIds', null, callback, scope);
  193. };
  194. /**
  195. * Alias for `getAllIds`.
  196. */
  197. exports.getIds = function () {
  198. this.getAllIds.apply(this, arguments);
  199. };
  200. /**
  201. * List all scheduled notification IDs.
  202. *
  203. * @param {Function} callback
  204. * A callback function to be called with the list
  205. * @param {Object?} scope
  206. * The scope for the callback function
  207. */
  208. exports.getScheduledIds = function (callback, scope) {
  209. this.exec('getScheduledIds', null, callback, scope);
  210. };
  211. /**
  212. * List all triggered notification IDs.
  213. *
  214. * @param {Function} callback
  215. * A callback function to be called with the list
  216. * @param {Object?} scope
  217. * The scope for the callback function
  218. */
  219. exports.getTriggeredIds = function (callback, scope) {
  220. this.exec('getTriggeredIds', null, callback, scope);
  221. };
  222. /**
  223. * Property list for given local notifications.
  224. * If called without IDs, all notification will be returned.
  225. *
  226. * @param {Number[]?} ids
  227. * Set of notification IDs
  228. * @param {Function} callback
  229. * A callback function to be called with the list
  230. * @param {Object?} scope
  231. * The scope for the callback function
  232. */
  233. exports.get = function () {
  234. var args = Array.apply(null, arguments);
  235. if (typeof args[0] == 'function') {
  236. args.unshift([]);
  237. }
  238. var ids = args[0],
  239. callback = args[1],
  240. scope = args[2];
  241. if (!Array.isArray(ids)) {
  242. this.exec('getSingle', ids.toString(), callback, scope);
  243. return;
  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. if (!Array.isArray(ids)) {
  282. this.exec('getSingleScheduled', ids.toString(), callback, scope);
  283. return;
  284. }
  285. ids = this.convertIds(ids);
  286. this.exec('getScheduled', ids, callback, scope);
  287. };
  288. /**
  289. * Property list for all scheduled notifications.
  290. *
  291. * @param {Function} callback
  292. * A callback function to be called with the list
  293. * @param {Object?} scope
  294. * The scope for the callback function
  295. */
  296. exports.getAllScheduled = function (callback, scope) {
  297. this.exec('getScheduled', null, callback, scope);
  298. };
  299. /**
  300. * Property list for given triggered notifications.
  301. * If called without IDs, all notification will be returned.
  302. *
  303. * @param {Number[]?} ids
  304. * Set of notification IDs
  305. * @param {Function} callback
  306. * A callback function to be called with the list
  307. * @param {Object?} scope
  308. * The scope for the callback function
  309. */
  310. exports.getTriggered = function () {
  311. var args = Array.apply(null, arguments);
  312. if (typeof args[0] == 'function') {
  313. args.unshift([]);
  314. }
  315. var ids = args[0],
  316. callback = args[1],
  317. scope = args[2];
  318. if (!Array.isArray(ids)) {
  319. ids = [ids];
  320. }
  321. if (!Array.isArray(ids)) {
  322. this.exec('getSingleTriggered', ids.toString(), callback, scope);
  323. return;
  324. }
  325. ids = this.convertIds(ids);
  326. this.exec('getTriggered', ids, callback, scope);
  327. };
  328. /**
  329. * Property list for all triggered notifications.
  330. *
  331. * @param {Function} callback
  332. * A callback function to be called with the list
  333. * @param {Object?} scope
  334. * The scope for the callback function
  335. */
  336. exports.getAllTriggered = function (callback, scope) {
  337. this.exec('getTriggered', null, callback, scope);
  338. };
  339. /**
  340. * Informs if the app has the permission to show notifications.
  341. *
  342. * @param {Function} callback
  343. * The function to be exec as the callback
  344. * @param {Object?} scope
  345. * The callback function's scope
  346. */
  347. exports.hasPermission = function (callback, scope) {
  348. var fn = this.createCallbackFn(callback, scope);
  349. if (device.platform != 'iOS') {
  350. fn(true);
  351. return;
  352. }
  353. exec(fn, null, 'LocalNotification', 'hasPermission', []);
  354. };
  355. /**
  356. * Register permission to show notifications if not already granted.
  357. *
  358. * @param {Function} callback
  359. * The function to be exec as the callback
  360. * @param {Object?} scope
  361. * The callback function's scope
  362. */
  363. exports.registerPermission = function (callback, scope) {
  364. var fn = this.createCallbackFn(callback, scope);
  365. if (device.platform != 'iOS') {
  366. fn(true);
  367. return;
  368. }
  369. exec(fn, null, 'LocalNotification', 'registerPermission', []);
  370. };
  371. /**********
  372. * EVENTS *
  373. **********/
  374. /**
  375. * Register callback for given event.
  376. *
  377. * @param {String} event
  378. * The event's name
  379. * @param {Function} callback
  380. * The function to be exec as callback
  381. * @param {Object?} scope
  382. * The callback function's scope
  383. */
  384. exports.on = function (event, callback, scope) {
  385. if (!this._listener[event]) {
  386. this._listener[event] = [];
  387. }
  388. var item = [callback, scope || window];
  389. this._listener[event].push(item);
  390. };
  391. /**
  392. * Unregister callback for given event.
  393. *
  394. * @param {String} event
  395. * The event's name
  396. * @param {Function} callback
  397. * The function to be exec as callback
  398. */
  399. exports.un = function (event, callback) {
  400. var listener = this._listener[event];
  401. if (!listener)
  402. return;
  403. for (var i = 0; i < listener.length; i++) {
  404. var fn = listener[i][0];
  405. if (fn == callback) {
  406. listener.splice(i, 1);
  407. break;
  408. }
  409. }
  410. };