local-notification-core.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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} msgs
  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. * @param {Object?} args
  58. * skipPermission:true schedules the notifications immediatly without
  59. * registering or checking for permission
  60. */
  61. exports.schedule = function (msgs, callback, scope, args) {
  62. var fn = function(granted) {
  63. if (!granted) return;
  64. var notifications = Array.isArray(msgs) ? msgs : [msgs];
  65. for (var i = 0; i < notifications.length; i++) {
  66. var notification = notifications[i];
  67. this.mergeWithDefaults(notification);
  68. this.convertProperties(notification);
  69. }
  70. this.exec('schedule', notifications, callback, scope);
  71. };
  72. if (args && args.skipPermission) {
  73. fn.call(this, true);
  74. } else {
  75. this.registerPermission(fn, this);
  76. }
  77. };
  78. /**
  79. * Update existing notifications specified by IDs in options.
  80. *
  81. * @param {Object} notifications
  82. * The notification properties to update
  83. * @param {Function} callback
  84. * A function to be called after the notification has been updated
  85. * @param {Object?} scope
  86. * The scope for the callback function
  87. * @param {Object?} args
  88. * skipPermission:true schedules the notifications immediatly without
  89. * registering or checking for permission
  90. */
  91. exports.update = function (msgs, callback, scope, args) {
  92. var fn = function(granted) {
  93. if (!granted) return;
  94. var notifications = Array.isArray(msgs) ? msgs : [msgs];
  95. for (var i = 0; i < notifications.length; i++) {
  96. var notification = notifications[i];
  97. this.convertProperties(notification);
  98. }
  99. this.exec('update', notifications, callback, scope);
  100. };
  101. if (args && args.skipPermission) {
  102. fn.call(this, true);
  103. } else {
  104. this.registerPermission(fn, this);
  105. }
  106. };
  107. /**
  108. * Clear the specified notification.
  109. *
  110. * @param {String} id
  111. * The ID of the notification
  112. * @param {Function} callback
  113. * A function to be called after the notification has been cleared
  114. * @param {Object?} scope
  115. * The scope for the callback function
  116. */
  117. exports.clear = function (ids, callback, scope) {
  118. ids = Array.isArray(ids) ? ids : [ids];
  119. ids = this.convertIds(ids);
  120. this.exec('clear', ids, callback, scope);
  121. };
  122. /**
  123. * Clear all previously sheduled notifications.
  124. *
  125. * @param {Function} callback
  126. * A function to be called after all notifications have been cleared
  127. * @param {Object?} scope
  128. * The scope for the callback function
  129. */
  130. exports.clearAll = function (callback, scope) {
  131. this.exec('clearAll', null, callback, scope);
  132. };
  133. /**
  134. * Cancel the specified notifications.
  135. *
  136. * @param {String[]} ids
  137. * The IDs of the notifications
  138. * @param {Function} callback
  139. * A function to be called after the notifications has been canceled
  140. * @param {Object?} scope
  141. * The scope for the callback function
  142. */
  143. exports.cancel = function (ids, callback, scope) {
  144. ids = Array.isArray(ids) ? ids : [ids];
  145. ids = this.convertIds(ids);
  146. this.exec('cancel', ids, callback, scope);
  147. };
  148. /**
  149. * Remove all previously registered notifications.
  150. *
  151. * @param {Function} callback
  152. * A function to be called after all notifications have been canceled
  153. * @param {Object?} scope
  154. * The scope for the callback function
  155. */
  156. exports.cancelAll = function (callback, scope) {
  157. this.exec('cancelAll', null, callback, scope);
  158. };
  159. /**
  160. * Check if a notification with an ID is present.
  161. *
  162. * @param {String} id
  163. * The ID of the notification
  164. * @param {Function} callback
  165. * A callback function to be called with the list
  166. * @param {Object?} scope
  167. * The scope for the callback function
  168. */
  169. exports.isPresent = function (id, callback, scope) {
  170. this.exec('isPresent', id || 0, callback, scope);
  171. };
  172. /**
  173. * Check if a notification with an ID is scheduled.
  174. *
  175. * @param {String} id
  176. * The ID of the notification
  177. * @param {Function} callback
  178. * A callback function to be called with the list
  179. * @param {Object?} scope
  180. * The scope for the callback function
  181. */
  182. exports.isScheduled = function (id, callback, scope) {
  183. this.exec('isScheduled', id || 0, callback, scope);
  184. };
  185. /**
  186. * Check if a notification with an ID was triggered.
  187. *
  188. * @param {String} id
  189. * The ID of the notification
  190. * @param {Function} callback
  191. * A callback function to be called with the list
  192. * @param {Object?} scope
  193. * The scope for the callback function
  194. */
  195. exports.isTriggered = function (id, callback, scope) {
  196. this.exec('isTriggered', id || 0, callback, scope);
  197. };
  198. /**
  199. * List all local notification IDs.
  200. *
  201. * @param {Function} callback
  202. * A callback function to be called with the list
  203. * @param {Object?} scope
  204. * The scope for the callback function
  205. */
  206. exports.getAllIds = function (callback, scope) {
  207. this.exec('getAllIds', null, callback, scope);
  208. };
  209. /**
  210. * Alias for `getAllIds`.
  211. */
  212. exports.getIds = function () {
  213. this.getAllIds.apply(this, arguments);
  214. };
  215. /**
  216. * List all scheduled notification IDs.
  217. *
  218. * @param {Function} callback
  219. * A callback function to be called with the list
  220. * @param {Object?} scope
  221. * The scope for the callback function
  222. */
  223. exports.getScheduledIds = function (callback, scope) {
  224. this.exec('getScheduledIds', null, callback, scope);
  225. };
  226. /**
  227. * List all triggered notification IDs.
  228. *
  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.getTriggeredIds = function (callback, scope) {
  235. this.exec('getTriggeredIds', null, callback, scope);
  236. };
  237. /**
  238. * Property list for given local notifications.
  239. * If called without IDs, all notification will be returned.
  240. *
  241. * @param {Number[]?} ids
  242. * Set of notification IDs
  243. * @param {Function} callback
  244. * A callback function to be called with the list
  245. * @param {Object?} scope
  246. * The scope for the callback function
  247. */
  248. exports.get = function () {
  249. var args = Array.apply(null, arguments);
  250. if (typeof args[0] == 'function') {
  251. args.unshift([]);
  252. }
  253. var ids = args[0],
  254. callback = args[1],
  255. scope = args[2];
  256. if (!Array.isArray(ids)) {
  257. this.exec('getSingle', Number(ids), callback, scope);
  258. return;
  259. }
  260. ids = this.convertIds(ids);
  261. this.exec('getAll', ids, callback, scope);
  262. };
  263. /**
  264. * Property list for all local notifications.
  265. *
  266. * @param {Function} callback
  267. * A callback function to be called with the list
  268. * @param {Object?} scope
  269. * The scope for the callback function
  270. */
  271. exports.getAll = function (callback, scope) {
  272. this.exec('getAll', null, callback, scope);
  273. };
  274. /**
  275. * Property list for given scheduled notifications.
  276. * If called without IDs, all notification will be returned.
  277. *
  278. * @param {Number[]?} ids
  279. * Set of notification IDs
  280. * @param {Function} callback
  281. * A callback function to be called with the list
  282. * @param {Object?} scope
  283. * The scope for the callback function
  284. */
  285. exports.getScheduled = function () {
  286. var args = Array.apply(null, arguments);
  287. if (typeof args[0] == 'function') {
  288. args.unshift([]);
  289. }
  290. var ids = args[0],
  291. callback = args[1],
  292. scope = args[2];
  293. if (!Array.isArray(ids)) {
  294. ids = [ids];
  295. }
  296. if (!Array.isArray(ids)) {
  297. this.exec('getSingleScheduled', Number(ids), callback, scope);
  298. return;
  299. }
  300. ids = this.convertIds(ids);
  301. this.exec('getScheduled', ids, callback, scope);
  302. };
  303. /**
  304. * Property list for all scheduled notifications.
  305. *
  306. * @param {Function} callback
  307. * A callback function to be called with the list
  308. * @param {Object?} scope
  309. * The scope for the callback function
  310. */
  311. exports.getAllScheduled = function (callback, scope) {
  312. this.exec('getScheduled', null, callback, scope);
  313. };
  314. /**
  315. * Property list for given triggered notifications.
  316. * If called without IDs, all notification will be returned.
  317. *
  318. * @param {Number[]?} ids
  319. * Set of notification IDs
  320. * @param {Function} callback
  321. * A callback function to be called with the list
  322. * @param {Object?} scope
  323. * The scope for the callback function
  324. */
  325. exports.getTriggered = function () {
  326. var args = Array.apply(null, arguments);
  327. if (typeof args[0] == 'function') {
  328. args.unshift([]);
  329. }
  330. var ids = args[0],
  331. callback = args[1],
  332. scope = args[2];
  333. if (!Array.isArray(ids)) {
  334. ids = [ids];
  335. }
  336. if (!Array.isArray(ids)) {
  337. this.exec('getSingleTriggered', Number(ids), callback, scope);
  338. return;
  339. }
  340. ids = this.convertIds(ids);
  341. this.exec('getTriggered', ids, callback, scope);
  342. };
  343. /**
  344. * Property list for all triggered notifications.
  345. *
  346. * @param {Function} callback
  347. * A callback function to be called with the list
  348. * @param {Object?} scope
  349. * The scope for the callback function
  350. */
  351. exports.getAllTriggered = function (callback, scope) {
  352. this.exec('getTriggered', null, callback, scope);
  353. };
  354. /**
  355. * Informs if the app has the permission to show notifications.
  356. *
  357. * @param {Function} callback
  358. * The function to be exec as the callback
  359. * @param {Object?} scope
  360. * The callback function's scope
  361. */
  362. exports.hasPermission = function (callback, scope) {
  363. var fn = this.createCallbackFn(callback, scope);
  364. if (device.platform != 'iOS') {
  365. fn(true);
  366. return;
  367. }
  368. exec(fn, null, 'LocalNotification', 'hasPermission', []);
  369. };
  370. /**
  371. * Register permission to show notifications if not already granted.
  372. *
  373. * @param {Function} callback
  374. * The function to be exec as the callback
  375. * @param {Object?} scope
  376. * The callback function's scope
  377. */
  378. exports.registerPermission = function (callback, scope) {
  379. if (this._registered) {
  380. return this.hasPermission(callback, scope);
  381. } else {
  382. this._registered = true;
  383. }
  384. var fn = this.createCallbackFn(callback, scope);
  385. if (device.platform != 'iOS') {
  386. fn(true);
  387. return;
  388. }
  389. exec(fn, null, 'LocalNotification', 'registerPermission', []);
  390. };
  391. /**********
  392. * EVENTS *
  393. **********/
  394. /**
  395. * Register callback for given event.
  396. *
  397. * @param {String} event
  398. * The event's name
  399. * @param {Function} callback
  400. * The function to be exec as callback
  401. * @param {Object?} scope
  402. * The callback function's scope
  403. */
  404. exports.on = function (event, callback, scope) {
  405. if (typeof callback !== "function")
  406. return;
  407. if (!this._listener[event]) {
  408. this._listener[event] = [];
  409. }
  410. var item = [callback, scope || window];
  411. this._listener[event].push(item);
  412. };
  413. /**
  414. * Unregister callback for given event.
  415. *
  416. * @param {String} event
  417. * The event's name
  418. * @param {Function} callback
  419. * The function to be exec as callback
  420. */
  421. exports.un = function (event, callback) {
  422. var listener = this._listener[event];
  423. if (!listener)
  424. return;
  425. for (var i = 0; i < listener.length; i++) {
  426. var fn = listener[i][0];
  427. if (fn == callback) {
  428. listener.splice(i, 1);
  429. break;
  430. }
  431. }
  432. };