local-notification-core.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 notification.
  108. // *
  109. // * @param {String} id
  110. // * The ID of the notification
  111. // * @param {Function} callback
  112. // * A function to be called after the notification has been cleared
  113. // * @param {Object?} scope
  114. // * The scope for the callback function
  115. // */
  116. // exports.clear = function (ids, callback, scope) {
  117. // ids = Array.isArray(ids) ? ids : [ids];
  118. // ids = this.convertIds(ids);
  119. // this.exec('clear', ids, callback, scope);
  120. // };
  121. // /**
  122. // * Clear all previously sheduled notifications.
  123. // *
  124. // * @param {Function} callback
  125. // * A function to be called after all notifications have been cleared
  126. // * @param {Object?} scope
  127. // * The scope for the callback function
  128. // */
  129. // exports.clearAll = function (callback, scope) {
  130. // this.exec('clearAll', null, callback, scope);
  131. // };
  132. // /**
  133. // * Cancel the specified notifications.
  134. // *
  135. // * @param {String[]} ids
  136. // * The IDs of the notifications
  137. // * @param {Function} callback
  138. // * A function to be called after the notifications has been canceled
  139. // * @param {Object?} scope
  140. // * The scope for the callback function
  141. // */
  142. // exports.cancel = function (ids, callback, scope) {
  143. // ids = Array.isArray(ids) ? ids : [ids];
  144. // ids = this.convertIds(ids);
  145. // this.exec('cancel', ids, callback, scope);
  146. // };
  147. // /**
  148. // * Remove all previously registered notifications.
  149. // *
  150. // * @param {Function} callback
  151. // * A function to be called after all notifications have been canceled
  152. // * @param {Object?} scope
  153. // * The scope for the callback function
  154. // */
  155. // exports.cancelAll = function (callback, scope) {
  156. // this.exec('cancelAll', null, callback, scope);
  157. // };
  158. // /**
  159. // * Check if a notification with an ID is present.
  160. // *
  161. // * @param {String} id
  162. // * The ID of the notification
  163. // * @param {Function} callback
  164. // * A callback function to be called with the list
  165. // * @param {Object?} scope
  166. // * The scope for the callback function
  167. // */
  168. // exports.isPresent = function (id, callback, scope) {
  169. // this.exec('isPresent', id || 0, callback, scope);
  170. // };
  171. // /**
  172. // * Check if a notification with an ID is scheduled.
  173. // *
  174. // * @param {String} id
  175. // * The ID of the notification
  176. // * @param {Function} callback
  177. // * A callback function to be called with the list
  178. // * @param {Object?} scope
  179. // * The scope for the callback function
  180. // */
  181. // exports.isScheduled = function (id, callback, scope) {
  182. // this.exec('isScheduled', id || 0, callback, scope);
  183. // };
  184. // /**
  185. // * Check if a notification with an ID was triggered.
  186. // *
  187. // * @param {String} id
  188. // * The ID of the notification
  189. // * @param {Function} callback
  190. // * A callback function to be called with the list
  191. // * @param {Object?} scope
  192. // * The scope for the callback function
  193. // */
  194. // exports.isTriggered = function (id, callback, scope) {
  195. // this.exec('isTriggered', id || 0, callback, scope);
  196. // };
  197. // *
  198. // * List all local 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. // exports.getAllIds = function (callback, scope) {
  205. // this.exec('getAllIds', null, callback, scope);
  206. // };
  207. // /**
  208. // * Alias for `getAllIds`.
  209. // */
  210. // exports.getIds = function () {
  211. // this.getAllIds.apply(this, arguments);
  212. // };
  213. // /**
  214. // * List all scheduled notification IDs.
  215. // *
  216. // * @param {Function} callback
  217. // * A callback function to be called with the list
  218. // * @param {Object?} scope
  219. // * The scope for the callback function
  220. // */
  221. // exports.getScheduledIds = function (callback, scope) {
  222. // this.exec('getScheduledIds', null, callback, scope);
  223. // };
  224. // /**
  225. // * List all triggered notification IDs.
  226. // *
  227. // * @param {Function} callback
  228. // * A callback function to be called with the list
  229. // * @param {Object?} scope
  230. // * The scope for the callback function
  231. // */
  232. // exports.getTriggeredIds = function (callback, scope) {
  233. // this.exec('getTriggeredIds', null, callback, scope);
  234. // };
  235. // /**
  236. // * Property list for given local notifications.
  237. // * If called without IDs, all notification will be returned.
  238. // *
  239. // * @param {Number[]?} ids
  240. // * Set of notification IDs
  241. // * @param {Function} callback
  242. // * A callback function to be called with the list
  243. // * @param {Object?} scope
  244. // * The scope for the callback function
  245. // */
  246. // exports.get = function () {
  247. // var args = Array.apply(null, arguments);
  248. // if (typeof args[0] == 'function') {
  249. // args.unshift([]);
  250. // }
  251. // var ids = args[0],
  252. // callback = args[1],
  253. // scope = args[2];
  254. // if (!Array.isArray(ids)) {
  255. // this.exec('getSingle', Number(ids), callback, scope);
  256. // return;
  257. // }
  258. // ids = this.convertIds(ids);
  259. // this.exec('getAll', ids, callback, scope);
  260. // };
  261. // /**
  262. // * Property list for all local notifications.
  263. // *
  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.getAll = function (callback, scope) {
  270. // this.exec('getAll', null, callback, scope);
  271. // };
  272. // /**
  273. // * Property list for given scheduled notifications.
  274. // * If called without IDs, all notification will be returned.
  275. // *
  276. // * @param {Number[]?} ids
  277. // * Set of notification IDs
  278. // * @param {Function} callback
  279. // * A callback function to be called with the list
  280. // * @param {Object?} scope
  281. // * The scope for the callback function
  282. // */
  283. // exports.getScheduled = function () {
  284. // var args = Array.apply(null, arguments);
  285. // if (typeof args[0] == 'function') {
  286. // args.unshift([]);
  287. // }
  288. // var ids = args[0],
  289. // callback = args[1],
  290. // scope = args[2];
  291. // if (!Array.isArray(ids)) {
  292. // ids = [ids];
  293. // }
  294. // if (!Array.isArray(ids)) {
  295. // this.exec('getSingleScheduled', Number(ids), callback, scope);
  296. // return;
  297. // }
  298. // ids = this.convertIds(ids);
  299. // this.exec('getScheduled', ids, callback, scope);
  300. // };
  301. // /**
  302. // * Property list for all scheduled notifications.
  303. // *
  304. // * @param {Function} callback
  305. // * A callback function to be called with the list
  306. // * @param {Object?} scope
  307. // * The scope for the callback function
  308. // */
  309. // exports.getAllScheduled = function (callback, scope) {
  310. // this.exec('getScheduled', null, callback, scope);
  311. // };
  312. // /**
  313. // * Property list for given triggered notifications.
  314. // * If called without IDs, all notification will be returned.
  315. // *
  316. // * @param {Number[]?} ids
  317. // * Set of notification IDs
  318. // * @param {Function} callback
  319. // * A callback function to be called with the list
  320. // * @param {Object?} scope
  321. // * The scope for the callback function
  322. // */
  323. // exports.getTriggered = function () {
  324. // var args = Array.apply(null, arguments);
  325. // if (typeof args[0] == 'function') {
  326. // args.unshift([]);
  327. // }
  328. // var ids = args[0],
  329. // callback = args[1],
  330. // scope = args[2];
  331. // if (!Array.isArray(ids)) {
  332. // ids = [ids];
  333. // }
  334. // if (!Array.isArray(ids)) {
  335. // this.exec('getSingleTriggered', Number(ids), callback, scope);
  336. // return;
  337. // }
  338. // ids = this.convertIds(ids);
  339. // this.exec('getTriggered', ids, callback, scope);
  340. // };
  341. // /**
  342. // * Property list for all triggered notifications.
  343. // *
  344. // * @param {Function} callback
  345. // * A callback function to be called with the list
  346. // * @param {Object?} scope
  347. // * The scope for the callback function
  348. // */
  349. // exports.getAllTriggered = function (callback, scope) {
  350. // this.exec('getTriggered', null, callback, scope);
  351. // };
  352. /**
  353. * The (platform specific) default settings.
  354. *
  355. * @return [ Object ]
  356. */
  357. exports.getDefaults = function () {
  358. return this._defaults;
  359. };
  360. /**
  361. * Overwrite default settings.
  362. *
  363. * @param [ Object ] newDefaults New default values.
  364. *
  365. * @return [ Void ]
  366. */
  367. exports.setDefaults = function (newDefaults) {
  368. var defaults = this.getDefaults();
  369. for (var key in defaults) {
  370. if (newDefaults.hasOwnProperty(key)) {
  371. defaults[key] = newDefaults[key];
  372. }
  373. }
  374. };
  375. /**
  376. * Register callback for given event.
  377. *
  378. * @param [ String ] event The name of the event.
  379. * @param [ Function ] callback The function to be exec as callback.
  380. * @param [ Object ] scope The callback function's scope.
  381. *
  382. * @return [ Void ]
  383. */
  384. exports.on = function (event, callback, scope) {
  385. if (typeof callback !== "function")
  386. return;
  387. if (!this._listener[event]) {
  388. this._listener[event] = [];
  389. }
  390. var item = [callback, scope || window];
  391. this._listener[event].push(item);
  392. };
  393. /**
  394. * Unregister callback for given event.
  395. *
  396. * @param [ String ] event The name of the event.
  397. * @param [ Function ] callback The function to be exec as callback.
  398. *
  399. * @return [ Void ]
  400. */
  401. exports.un = function (event, callback) {
  402. var listener = this._listener[event];
  403. if (!listener)
  404. return;
  405. for (var i = 0; i < listener.length; i++) {
  406. var fn = listener[i][0];
  407. if (fn == callback) {
  408. listener.splice(i, 1);
  409. break;
  410. }
  411. }
  412. };