local-notification-core.js 11 KB

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