local-notification-core.js 13 KB

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