local-notification-core.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. this.exec('isPresent', id || 0, callback, scope);
  153. };
  154. /**
  155. * Check if a notification with an ID is scheduled.
  156. *
  157. * @param {String} id
  158. * The ID of the notification
  159. * @param {Function} callback
  160. * A callback function to be called with the list
  161. * @param {Object?} scope
  162. * The scope for the callback function
  163. */
  164. exports.isScheduled = function (id, callback, scope) {
  165. this.exec('isScheduled', id || 0, callback, scope);
  166. };
  167. /**
  168. * Check if a notification with an ID was triggered.
  169. *
  170. * @param {String} id
  171. * The ID of the notification
  172. * @param {Function} callback
  173. * A callback function to be called with the list
  174. * @param {Object?} scope
  175. * The scope for the callback function
  176. */
  177. exports.isTriggered = function (id, callback, scope) {
  178. this.exec('isTriggered', id || 0, callback, scope);
  179. };
  180. /**
  181. * List all local notification IDs.
  182. *
  183. * @param {Function} callback
  184. * A callback function to be called with the list
  185. * @param {Object?} scope
  186. * The scope for the callback function
  187. */
  188. exports.getAllIds = function (callback, scope) {
  189. this.exec('getAllIds', null, callback, scope);
  190. };
  191. /**
  192. * Alias for `getAllIds`.
  193. */
  194. exports.getIds = function () {
  195. this.getAllIds.apply(this, arguments);
  196. };
  197. /**
  198. * List all scheduled notification IDs.
  199. *
  200. * @param {Function} callback
  201. * A callback function to be called with the list
  202. * @param {Object?} scope
  203. * The scope for the callback function
  204. */
  205. exports.getScheduledIds = function (callback, scope) {
  206. this.exec('getScheduledIds', null, callback, scope);
  207. };
  208. /**
  209. * List all triggered notification IDs.
  210. *
  211. * @param {Function} callback
  212. * A callback function to be called with the list
  213. * @param {Object?} scope
  214. * The scope for the callback function
  215. */
  216. exports.getTriggeredIds = function (callback, scope) {
  217. this.exec('getTriggeredIds', null, callback, scope);
  218. };
  219. /**
  220. * Property list for given local notifications.
  221. * If called without IDs, all notification will be returned.
  222. *
  223. * @param {Number[]?} ids
  224. * Set of notification IDs
  225. * @param {Function} callback
  226. * A callback function to be called with the list
  227. * @param {Object?} scope
  228. * The scope for the callback function
  229. */
  230. exports.get = function () {
  231. var args = Array.apply(null, arguments);
  232. if (typeof args[0] == 'function') {
  233. args.unshift([]);
  234. }
  235. var ids = args[0],
  236. callback = args[1],
  237. scope = args[2];
  238. if (!Array.isArray(ids)) {
  239. this.exec('getSingle', Number(ids), callback, scope);
  240. return;
  241. }
  242. ids = this.convertIds(ids);
  243. this.exec('getAll', ids, callback, scope);
  244. };
  245. /**
  246. * Property list for all local notifications.
  247. *
  248. * @param {Function} callback
  249. * A callback function to be called with the list
  250. * @param {Object?} scope
  251. * The scope for the callback function
  252. */
  253. exports.getAll = function (callback, scope) {
  254. this.exec('getAll', null, callback, scope);
  255. };
  256. /**
  257. * Property list for given scheduled notifications.
  258. * If called without IDs, all notification will be returned.
  259. *
  260. * @param {Number[]?} ids
  261. * Set of notification IDs
  262. * @param {Function} callback
  263. * A callback function to be called with the list
  264. * @param {Object?} scope
  265. * The scope for the callback function
  266. */
  267. exports.getScheduled = function () {
  268. var args = Array.apply(null, arguments);
  269. if (typeof args[0] == 'function') {
  270. args.unshift([]);
  271. }
  272. var ids = args[0],
  273. callback = args[1],
  274. scope = args[2];
  275. if (!Array.isArray(ids)) {
  276. ids = [ids];
  277. }
  278. if (!Array.isArray(ids)) {
  279. this.exec('getSingleScheduled', Number(ids), callback, scope);
  280. return;
  281. }
  282. ids = this.convertIds(ids);
  283. this.exec('getScheduled', ids, callback, scope);
  284. };
  285. /**
  286. * Property list for all scheduled notifications.
  287. *
  288. * @param {Function} callback
  289. * A callback function to be called with the list
  290. * @param {Object?} scope
  291. * The scope for the callback function
  292. */
  293. exports.getAllScheduled = function (callback, scope) {
  294. this.exec('getScheduled', null, callback, scope);
  295. };
  296. /**
  297. * Property list for given triggered notifications.
  298. * If called without IDs, all notification will be returned.
  299. *
  300. * @param {Number[]?} ids
  301. * Set of notification IDs
  302. * @param {Function} callback
  303. * A callback function to be called with the list
  304. * @param {Object?} scope
  305. * The scope for the callback function
  306. */
  307. exports.getTriggered = function () {
  308. var args = Array.apply(null, arguments);
  309. if (typeof args[0] == 'function') {
  310. args.unshift([]);
  311. }
  312. var ids = args[0],
  313. callback = args[1],
  314. scope = args[2];
  315. if (!Array.isArray(ids)) {
  316. ids = [ids];
  317. }
  318. if (!Array.isArray(ids)) {
  319. this.exec('getSingleTriggered', Number(ids), callback, scope);
  320. return;
  321. }
  322. ids = this.convertIds(ids);
  323. this.exec('getTriggered', ids, callback, scope);
  324. };
  325. /**
  326. * Property list for all triggered notifications.
  327. *
  328. * @param {Function} callback
  329. * A callback function to be called with the list
  330. * @param {Object?} scope
  331. * The scope for the callback function
  332. */
  333. exports.getAllTriggered = function (callback, scope) {
  334. this.exec('getTriggered', null, callback, scope);
  335. };
  336. /**
  337. * Informs if the app has the permission to show notifications.
  338. *
  339. * @param {Function} callback
  340. * The function to be exec as the callback
  341. * @param {Object?} scope
  342. * The callback function's scope
  343. */
  344. exports.hasPermission = function (callback, scope) {
  345. var fn = this.createCallbackFn(callback, scope);
  346. if (device.platform != 'iOS') {
  347. fn(true);
  348. return;
  349. }
  350. exec(fn, null, 'LocalNotification', 'hasPermission', []);
  351. };
  352. /**
  353. * Register permission to show notifications if not already granted.
  354. *
  355. * @param {Function} callback
  356. * The function to be exec as the callback
  357. * @param {Object?} scope
  358. * The callback function's scope
  359. */
  360. exports.registerPermission = function (callback, scope) {
  361. var fn = this.createCallbackFn(callback, scope);
  362. if (device.platform != 'iOS') {
  363. fn(true);
  364. return;
  365. }
  366. exec(fn, null, 'LocalNotification', 'registerPermission', []);
  367. };
  368. /**********
  369. * EVENTS *
  370. **********/
  371. /**
  372. * Register callback for given event.
  373. *
  374. * @param {String} event
  375. * The event's name
  376. * @param {Function} callback
  377. * The function to be exec as callback
  378. * @param {Object?} scope
  379. * The callback function's scope
  380. */
  381. exports.on = function (event, callback, scope) {
  382. if (typeof callback !== "function")
  383. return;
  384. if (!this._listener[event]) {
  385. this._listener[event] = [];
  386. }
  387. var item = [callback, scope || window];
  388. this._listener[event].push(item);
  389. };
  390. /**
  391. * Unregister callback for given event.
  392. *
  393. * @param {String} event
  394. * The event's name
  395. * @param {Function} callback
  396. * The function to be exec as callback
  397. */
  398. exports.un = function (event, callback) {
  399. var listener = this._listener[event];
  400. if (!listener)
  401. return;
  402. for (var i = 0; i < listener.length; i++) {
  403. var fn = listener[i][0];
  404. if (fn == callback) {
  405. listener.splice(i, 1);
  406. break;
  407. }
  408. }
  409. };