local-notification-core.js 13 KB

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