local-notification-core.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * This file contains Original Code and/or Modifications of Original Code
  3. * as defined in and that are subject to the Apache License
  4. * Version 2.0 (the 'License'). You may not use this file except in
  5. * compliance with the License. Please obtain a copy of the License at
  6. * http://opensource.org/licenses/Apache-2.0/ and read it before using this
  7. * file.
  8. *
  9. * The Original Code and all software distributed under the License are
  10. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  11. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  12. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  13. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  14. * Please see the License for the specific language governing rights and
  15. * limitations under the License.
  16. */
  17. var exec = require('cordova/exec');
  18. /**
  19. * Request permission to show notifications.
  20. *
  21. * @param [ Function ] callback The function to be exec as the callback.
  22. * @param [ Object ] scope The callback function's scope.
  23. *
  24. * @return [ Void ]
  25. */
  26. exports.hasPermission = function (callback, scope) {
  27. var fn = this.createCallbackFn(callback, scope);
  28. if (device.platform != 'iOS') {
  29. fn(true);
  30. return;
  31. }
  32. exec(fn, null, 'LocalNotification', 'check', []);
  33. };
  34. /**
  35. * Request permission to show notifications.
  36. *
  37. * @param [ Function ] callback The function to be exec as the callback.
  38. * @param [ Object ] scope The callback function's scope.
  39. *
  40. * @return [ Void ]
  41. */
  42. exports.requestPermission = function (callback, scope) {
  43. var fn = this.createCallbackFn(callback, scope);
  44. if (device.platform != 'iOS') {
  45. fn(true);
  46. return;
  47. }
  48. exec(fn, null, 'LocalNotification', 'request', []);
  49. };
  50. /**
  51. * Schedule notifications.
  52. *
  53. * @param [ Array ] notifications The notifications to schedule.
  54. * @param [ Function ] callback The function to be exec as the callback.
  55. * @param [ Object ] scope The callback function's scope.
  56. * @param [ Object ] args Optional flags how to schedule.
  57. *
  58. * @return [ Void ]
  59. */
  60. exports.schedule = function (msgs, callback, scope, args) {
  61. var fn = function (granted) {
  62. if (!granted) return;
  63. var notifications = Array.isArray(msgs) ? msgs : [msgs];
  64. for (var i = 0; i < notifications.length; i++) {
  65. var notification = notifications[i];
  66. this.mergeWithDefaults(notification);
  67. this.convertProperties(notification);
  68. }
  69. this.exec('schedule', notifications, callback, scope);
  70. };
  71. if (args && args.skipPermission) {
  72. fn.call(this, true);
  73. } else {
  74. this.requestPermission(fn, this);
  75. }
  76. };
  77. // /**
  78. // * Update existing notifications specified by IDs in options.
  79. // *
  80. // * @param {Object} notifications
  81. // * The notification properties to update
  82. // * @param {Function} callback
  83. // * A function to be called after the notification has been updated
  84. // * @param {Object?} scope
  85. // * The scope for the callback function
  86. // * @param {Object?} args
  87. // * skipPermission:true schedules the notifications immediatly without
  88. // * registering or checking for permission
  89. // */
  90. // exports.update = function (msgs, callback, scope, args) {
  91. // var fn = function(granted) {
  92. // if (!granted) return;
  93. // var notifications = Array.isArray(msgs) ? msgs : [msgs];
  94. // for (var i = 0; i < notifications.length; i++) {
  95. // var notification = notifications[i];
  96. // this.convertProperties(notification);
  97. // }
  98. // this.exec('update', notifications, callback, scope);
  99. // };
  100. // if (args && args.skipPermission) {
  101. // fn.call(this, true);
  102. // } else {
  103. // this.registerPermission(fn, this);
  104. // }
  105. // };
  106. /**
  107. * Clear the specified notifications by id.
  108. *
  109. * @param [ Array<Int> ] ids The IDs of the notifications.
  110. * @param [ Function ] callback The function to be exec as the callback.
  111. * @param [ Object ] scope The callback function's scope.
  112. *
  113. * @return [ Void ]
  114. */
  115. exports.clear = function (ids, callback, scope) {
  116. ids = Array.isArray(ids) ? ids : [ids];
  117. ids = this.convertIds(ids);
  118. this.exec('clear', ids, callback, scope);
  119. };
  120. /**
  121. * Clear all triggered notifications.
  122. *
  123. * @param [ Function ] callback The function to be exec as the callback.
  124. * @param [ Object ] scope The callback function's scope.
  125. *
  126. * @return [ Void ]
  127. */
  128. exports.clearAll = function (callback, scope) {
  129. this.exec('clearAll', null, callback, scope);
  130. };
  131. /**
  132. * Clear the specified notifications by id.
  133. *
  134. * @param [ Array<Int> ] ids The IDs of the notifications.
  135. * @param [ Function ] callback The function to be exec as the callback.
  136. * @param [ Object ] scope The callback function's scope.
  137. *
  138. * @return [ Void ]
  139. */
  140. exports.cancel = function (ids, callback, scope) {
  141. ids = Array.isArray(ids) ? ids : [ids];
  142. ids = this.convertIds(ids);
  143. this.exec('cancel', ids, callback, scope);
  144. };
  145. /**
  146. * Cancel all scheduled notifications.
  147. *
  148. * @param [ Function ] callback The function to be exec as the callback.
  149. * @param [ Object ] scope The callback function's scope.
  150. *
  151. * @return [ Void ]
  152. */
  153. exports.cancelAll = function (callback, scope) {
  154. this.exec('cancelAll', null, callback, scope);
  155. };
  156. /**
  157. * Check if a notification is present.
  158. *
  159. * @param [ Int ] id The ID of the notification.
  160. * @param [ Function ] callback The function to be exec as the callback.
  161. * @param [ Object ] scope The callback function's scope.
  162. *
  163. * @return [ Void ]
  164. */
  165. exports.isPresent = function (id, callback, scope) {
  166. this.exec('isPresent', id, callback, scope);
  167. };
  168. /**
  169. * Check if a notification is scheduled.
  170. *
  171. * @param [ Int ] id The ID of the notification.
  172. * @param [ Function ] callback The function to be exec as the callback.
  173. * @param [ Object ] scope The callback function's scope.
  174. *
  175. * @return [ Void ]
  176. */
  177. exports.isScheduled = function (id, callback, scope) {
  178. this.exec('isScheduled', id, callback, scope);
  179. };
  180. /**
  181. * Check if a notification was triggered.
  182. *
  183. * @param [ Int ] id The ID of the notification.
  184. * @param [ Function ] callback The function to be exec as the callback.
  185. * @param [ Object ] scope The callback function's scope.
  186. *
  187. * @return [ Void ]
  188. */
  189. exports.isTriggered = function (id, callback, scope) {
  190. this.exec('isTriggered', id, callback, scope);
  191. };
  192. /**
  193. * List of all notification ids.
  194. *
  195. * @param [ Function ] callback The function to be exec as the callback.
  196. * @param [ Object ] scope The callback function's scope.
  197. *
  198. * @return [ Void ]
  199. */
  200. exports.getAllIds = function (callback, scope) {
  201. this.exec('ids', null, callback, scope);
  202. };
  203. /**
  204. * Alias for `getAllIds`.
  205. */
  206. exports.getIds = function () {
  207. this.getAllIds.apply(this, arguments);
  208. };
  209. /**
  210. * List of all scheduled notification IDs.
  211. *
  212. * @param [ Function ] callback The function to be exec as the callback.
  213. * @param [ Object ] scope The callback function's scope.
  214. *
  215. * @return [ Void ]
  216. */
  217. exports.getScheduledIds = function (callback, scope) {
  218. this.exec('scheduledIds', null, callback, scope);
  219. };
  220. /**
  221. * List of all triggered notification IDs.
  222. *
  223. * @param [ Function ] callback The function to be exec as the callback.
  224. * @param [ Object ] scope The callback function's scope.
  225. *
  226. * @return [ Void ]
  227. */
  228. exports.getTriggeredIds = function (callback, scope) {
  229. this.exec('triggeredIds', null, callback, scope);
  230. };
  231. /**
  232. * List of local notifications specified by id.
  233. * If called without IDs, all notification will be returned.
  234. *
  235. * @param [ Array<Int> ] ids The IDs of the notifications.
  236. * @param [ Function ] callback The function to be exec as the callback.
  237. * @param [ Object ] scope The callback function's scope.
  238. *
  239. * @return [ Void ]
  240. */
  241. exports.get = function () {
  242. var args = Array.apply(null, arguments);
  243. if (typeof args[0] == 'function') {
  244. args.unshift([]);
  245. }
  246. var ids = args[0],
  247. callback = args[1],
  248. scope = args[2];
  249. if (!Array.isArray(ids)) {
  250. this.exec('notification', Number(ids), callback, scope);
  251. return;
  252. }
  253. ids = this.convertIds(ids);
  254. this.exec('notifications', ids, callback, scope);
  255. };
  256. /**
  257. * List for all notifications.
  258. *
  259. * @param [ Function ] callback The function to be exec as the callback.
  260. * @param [ Object ] scope The callback function's scope.
  261. *
  262. * @return [ Void ]
  263. */
  264. exports.getAll = function (callback, scope) {
  265. this.exec('notifications', null, callback, scope);
  266. };
  267. /**
  268. * List of scheduled notifications specified by id.
  269. * If called without IDs, all notification will be returned.
  270. *
  271. * @param [ Array<Int> ] ids The IDs of the notifications.
  272. * @param [ Function ] callback The function to be exec as the callback.
  273. * @param [ Object ] scope The callback function's scope.
  274. *
  275. * @return [ Void ]
  276. */
  277. exports.getScheduled = function () {
  278. var args = Array.apply(null, arguments);
  279. if (typeof args[0] == 'function') {
  280. args.unshift([]);
  281. }
  282. var ids = args[0],
  283. callback = args[1],
  284. scope = args[2];
  285. if (!Array.isArray(ids)) {
  286. ids = [ids];
  287. }
  288. if (!Array.isArray(ids)) {
  289. this.exec('scheduledNotification', Number(ids), callback, scope);
  290. return;
  291. }
  292. ids = this.convertIds(ids);
  293. this.exec('scheduledNotifications', ids, callback, scope);
  294. };
  295. /**
  296. * List of all scheduled notifications.
  297. *
  298. * @param [ Function ] callback The function to be exec as the callback.
  299. * @param [ Object ] scope The callback function's scope.
  300. */
  301. exports.getAllScheduled = function (callback, scope) {
  302. this.exec('scheduledNotifications', null, callback, scope);
  303. };
  304. /**
  305. * List of triggered notifications specified by id.
  306. * If called without IDs, all notification will be returned.
  307. *
  308. * @param [ Array<Int> ] ids The IDs of the notifications.
  309. * @param [ Function ] callback The function to be exec as the callback.
  310. * @param [ Object ] scope The callback function's scope.
  311. *
  312. * @return [ Void ]
  313. */
  314. exports.getTriggered = function () {
  315. var args = Array.apply(null, arguments);
  316. if (typeof args[0] == 'function') {
  317. args.unshift([]);
  318. }
  319. var ids = args[0],
  320. callback = args[1],
  321. scope = args[2];
  322. if (!Array.isArray(ids)) {
  323. ids = [ids];
  324. }
  325. if (!Array.isArray(ids)) {
  326. this.exec('triggeredNotification', Number(ids), callback, scope);
  327. return;
  328. }
  329. ids = this.convertIds(ids);
  330. this.exec('triggeredNotifications', ids, callback, scope);
  331. };
  332. /**
  333. * List of all triggered notifications.
  334. *
  335. * @param [ Function ] callback The function to be exec as the callback.
  336. * @param [ Object ] scope The callback function's scope.
  337. */
  338. exports.getAllTriggered = function (callback, scope) {
  339. this.exec('triggeredNotifications', null, callback, scope);
  340. };
  341. /**
  342. * The (platform specific) default settings.
  343. *
  344. * @return [ Object ]
  345. */
  346. exports.getDefaults = function () {
  347. return this._defaults;
  348. };
  349. /**
  350. * Overwrite default settings.
  351. *
  352. * @param [ Object ] newDefaults New default values.
  353. *
  354. * @return [ Void ]
  355. */
  356. exports.setDefaults = function (newDefaults) {
  357. var defaults = this.getDefaults();
  358. for (var key in defaults) {
  359. if (newDefaults.hasOwnProperty(key)) {
  360. defaults[key] = newDefaults[key];
  361. }
  362. }
  363. };
  364. /**
  365. * Register callback for given event.
  366. *
  367. * @param [ String ] event The name of the event.
  368. * @param [ Function ] callback The function to be exec as callback.
  369. * @param [ Object ] scope The callback function's scope.
  370. *
  371. * @return [ Void ]
  372. */
  373. exports.on = function (event, callback, scope) {
  374. if (typeof callback !== "function")
  375. return;
  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 The name of the event.
  386. * @param [ Function ] callback The function to be exec as callback.
  387. *
  388. * @return [ Void ]
  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. };