local-notification-core.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. ids = [ids];
  243. }
  244. ids = this.convertIds(ids);
  245. this.exec('getAll', ids, callback, scope);
  246. };
  247. /**
  248. * Property list for all local notifications.
  249. *
  250. * @param {Function} callback
  251. * A callback function to be called with the list
  252. * @param {Object?} scope
  253. * The scope for the callback function
  254. */
  255. exports.getAll = function (callback, scope) {
  256. this.exec('getAll', null, callback, scope);
  257. };
  258. /**
  259. * Property list for given scheduled notifications.
  260. * If called without IDs, all notification will be returned.
  261. *
  262. * @param {Number[]?} ids
  263. * Set of notification IDs
  264. * @param {Function} callback
  265. * A callback function to be called with the list
  266. * @param {Object?} scope
  267. * The scope for the callback function
  268. */
  269. exports.getScheduled = function () {
  270. var args = Array.apply(null, arguments);
  271. if (typeof args[0] == 'function') {
  272. args.unshift([]);
  273. }
  274. var ids = args[0],
  275. callback = args[1],
  276. scope = args[2];
  277. if (!Array.isArray(ids)) {
  278. ids = [ids];
  279. }
  280. ids = this.convertIds(ids);
  281. this.exec('getScheduled', ids, callback, scope);
  282. };
  283. /**
  284. * Retrieve the properties for all scheduled notifications.
  285. *
  286. * @param {Function} callback
  287. * A callback function to be called with the list
  288. * @param {Object?} scope
  289. * The scope for the callback function
  290. */
  291. exports.getAllScheduled = function (callback, scope) {
  292. this.exec('getScheduled', null, callback, scope);
  293. };
  294. /**
  295. * Property list for given triggered notifications.
  296. * If called without IDs, all notification will be returned.
  297. *
  298. * @param {Number[]?} ids
  299. * Set of notification IDs
  300. * @param {Function} callback
  301. * A callback function to be called with the list
  302. * @param {Object?} scope
  303. * The scope for the callback function
  304. */
  305. exports.getTriggered = function () {
  306. var args = Array.apply(null, arguments);
  307. if (typeof args[0] == 'function') {
  308. args.unshift([]);
  309. }
  310. var ids = args[0],
  311. callback = args[1],
  312. scope = args[2];
  313. if (!Array.isArray(ids)) {
  314. ids = [ids];
  315. }
  316. ids = this.convertIds(ids);
  317. this.exec('getTriggered', ids, callback, scope);
  318. };
  319. /**
  320. * Retrieve the properties for all triggered notifications.
  321. *
  322. * @param {Function} callback
  323. * A callback function to be called with the list
  324. * @param {Object?} scope
  325. * The scope for the callback function
  326. */
  327. exports.getAllTriggered = function (callback, scope) {
  328. this.exec('getTriggered', null, callback, scope);
  329. };
  330. /**
  331. * Informs if the app has the permission to show notifications.
  332. *
  333. * @param {Function} callback
  334. * The function to be exec as the callback
  335. * @param {Object?} scope
  336. * The callback function's scope
  337. */
  338. exports.hasPermission = function (callback, scope) {
  339. var fn = this.createCallbackFn(callback, scope);
  340. if (device.platform != 'iOS') {
  341. fn(true);
  342. return;
  343. }
  344. exec(fn, null, 'LocalNotification', 'hasPermission', []);
  345. };
  346. /**
  347. * Register permission to show notifications if not already granted.
  348. *
  349. * @param {Function} callback
  350. * The function to be exec as the callback
  351. * @param {Object?} scope
  352. * The callback function's scope
  353. */
  354. exports.registerPermission = function (callback, scope) {
  355. var fn = this.createCallbackFn(callback, scope);
  356. if (device.platform != 'iOS') {
  357. fn(true);
  358. return;
  359. }
  360. exec(fn, null, 'LocalNotification', 'registerPermission', []);
  361. };
  362. /**********
  363. * EVENTS *
  364. **********/
  365. /**
  366. * Register callback for given event.
  367. *
  368. * @param {String} event
  369. * The event's name
  370. * @param {Function} callback
  371. * The function to be exec as callback
  372. * @param {Object?} scope
  373. * The callback function's scope
  374. */
  375. exports.on = function (event, callback, scope) {
  376. if (!this._listener[event]) {
  377. this._listener[event] = [];
  378. }
  379. var item = [callback, scope || window];
  380. this._listener[event].push(item);
  381. };
  382. /**
  383. * Unregister callback for given event.
  384. *
  385. * @param {String} event
  386. * The event's name
  387. * @param {Function} callback
  388. * The function to be exec as callback
  389. */
  390. exports.un = function (event, callback) {
  391. var listener = this._listener[event];
  392. if (!listener)
  393. return;
  394. for (var i = 0; i < listener.length; i++) {
  395. var fn = listener[i][0];
  396. if (fn == callback) {
  397. listener.splice(i, 1);
  398. break;
  399. }
  400. }
  401. };