local-notification-core.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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.getIds = function (callback, scope) {
  197. this.exec('ids', null, callback, scope);
  198. };
  199. /**
  200. * List of all scheduled notification IDs.
  201. *
  202. * @param [ Function ] callback The function to be exec as the callback.
  203. * @param [ Object ] scope The callback function's scope.
  204. *
  205. * @return [ Void ]
  206. */
  207. exports.getScheduledIds = function (callback, scope) {
  208. this.exec('scheduledIds', null, callback, scope);
  209. };
  210. /**
  211. * List of all triggered notification IDs.
  212. *
  213. * @param [ Function ] callback The function to be exec as the callback.
  214. * @param [ Object ] scope The callback function's scope.
  215. *
  216. * @return [ Void ]
  217. */
  218. exports.getTriggeredIds = function (callback, scope) {
  219. this.exec('triggeredIds', null, callback, scope);
  220. };
  221. /**
  222. * List of local notifications specified by id.
  223. * If called without IDs, all notification will be returned.
  224. *
  225. * @param [ Array<Int> ] ids The IDs of the notifications.
  226. * @param [ Function ] callback The function to be exec as the callback.
  227. * @param [ Object ] scope The callback function's scope.
  228. *
  229. * @return [ Void ]
  230. */
  231. exports.get = function () {
  232. var args = Array.apply(null, arguments);
  233. if (typeof args[0] == 'function') {
  234. args.unshift([]);
  235. }
  236. var ids = args[0],
  237. callback = args[1],
  238. scope = args[2];
  239. if (!Array.isArray(ids)) {
  240. this.exec('notification', Number(ids), callback, scope);
  241. return;
  242. }
  243. ids = this.convertIds(ids);
  244. this.exec('notifications', ids, callback, scope);
  245. };
  246. /**
  247. * List for all notifications.
  248. *
  249. * @param [ Function ] callback The function to be exec as the callback.
  250. * @param [ Object ] scope The callback function's scope.
  251. *
  252. * @return [ Void ]
  253. */
  254. exports.getAll = function (callback, scope) {
  255. this.exec('notifications', null, callback, scope);
  256. };
  257. /**
  258. * List of all scheduled notifications.
  259. *
  260. * @param [ Function ] callback The function to be exec as the callback.
  261. * @param [ Object ] scope The callback function's scope.
  262. */
  263. exports.getScheduled = function (callback, scope) {
  264. this.exec('scheduledNotifications', null, callback, scope);
  265. };
  266. /**
  267. * List of all triggered notifications.
  268. *
  269. * @param [ Function ] callback The function to be exec as the callback.
  270. * @param [ Object ] scope The callback function's scope.
  271. */
  272. exports.getTriggered = function (callback, scope) {
  273. this.exec('triggeredNotifications', null, callback, scope);
  274. };
  275. /**
  276. * Register an group of actions by id.
  277. *
  278. * @param [ String ] id The Id of the group.
  279. * @param [ Array] actions The action config settings.
  280. * @param [ Function ] callback The function to be exec as the callback.
  281. * @param [ Object ] scope The callback function's scope.
  282. *
  283. * @return [ Void ]
  284. */
  285. exports.addActionGroup = function (id, actions, callback, scope) {
  286. var config = { actionGroupId: id, actions: actions };
  287. this.exec('registerCategory', config, callback, scope);
  288. };
  289. /**
  290. * The (platform specific) default settings.
  291. *
  292. * @return [ Object ]
  293. */
  294. exports.getDefaults = function () {
  295. return this._defaults;
  296. };
  297. /**
  298. * Overwrite default settings.
  299. *
  300. * @param [ Object ] newDefaults New default values.
  301. *
  302. * @return [ Void ]
  303. */
  304. exports.setDefaults = function (newDefaults) {
  305. var defaults = this.getDefaults();
  306. for (var key in defaults) {
  307. if (newDefaults.hasOwnProperty(key)) {
  308. defaults[key] = newDefaults[key];
  309. }
  310. }
  311. };
  312. /**
  313. * Register callback for given event.
  314. *
  315. * @param [ String ] event The name of the event.
  316. * @param [ Function ] callback The function to be exec as callback.
  317. * @param [ Object ] scope The callback function's scope.
  318. *
  319. * @return [ Void ]
  320. */
  321. exports.on = function (event, callback, scope) {
  322. if (typeof callback !== "function")
  323. return;
  324. if (!this._listener[event]) {
  325. this._listener[event] = [];
  326. }
  327. var item = [callback, scope || window];
  328. this._listener[event].push(item);
  329. };
  330. /**
  331. * Unregister callback for given event.
  332. *
  333. * @param [ String ] event The name of the event.
  334. * @param [ Function ] callback The function to be exec as callback.
  335. *
  336. * @return [ Void ]
  337. */
  338. exports.un = function (event, callback) {
  339. var listener = this._listener[event];
  340. if (!listener)
  341. return;
  342. for (var i = 0; i < listener.length; i++) {
  343. var fn = listener[i][0];
  344. if (fn == callback) {
  345. listener.splice(i, 1);
  346. break;
  347. }
  348. }
  349. };