local-notification-core.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. * Schedule notifications.
  75. *
  76. * @param [ Array ] notifications The notifications to schedule.
  77. * @param [ Function ] callback The function to be exec as the callback.
  78. * @param [ Object ] scope The callback function's scope.
  79. * @param [ Object ] args Optional flags how to schedule.
  80. *
  81. * @return [ Void ]
  82. */
  83. exports.update = function (msgs, callback, scope, args) {
  84. var fn = function(granted) {
  85. if (!granted) return;
  86. var notifications = Array.isArray(msgs) ? msgs : [msgs];
  87. for (var i = 0; i < notifications.length; i++) {
  88. var notification = notifications[i];
  89. this.convertProperties(notification);
  90. }
  91. this.exec('update', notifications, callback, scope);
  92. };
  93. if (args && args.skipPermission) {
  94. fn.call(this, true);
  95. } else {
  96. this.requestPermission(fn, this);
  97. }
  98. };
  99. /**
  100. * Clear the specified notifications by id.
  101. *
  102. * @param [ Array<Int> ] ids The IDs of the notifications.
  103. * @param [ Function ] callback The function to be exec as the callback.
  104. * @param [ Object ] scope The callback function's scope.
  105. *
  106. * @return [ Void ]
  107. */
  108. exports.clear = function (ids, callback, scope) {
  109. ids = Array.isArray(ids) ? ids : [ids];
  110. ids = this.convertIds(ids);
  111. this.exec('clear', ids, callback, scope);
  112. };
  113. /**
  114. * Clear all triggered notifications.
  115. *
  116. * @param [ Function ] callback The function to be exec as the callback.
  117. * @param [ Object ] scope The callback function's scope.
  118. *
  119. * @return [ Void ]
  120. */
  121. exports.clearAll = function (callback, scope) {
  122. this.exec('clearAll', null, callback, scope);
  123. };
  124. /**
  125. * Clear the specified notifications by id.
  126. *
  127. * @param [ Array<Int> ] ids The IDs of the notifications.
  128. * @param [ Function ] callback The function to be exec as the callback.
  129. * @param [ Object ] scope The callback function's scope.
  130. *
  131. * @return [ Void ]
  132. */
  133. exports.cancel = function (ids, callback, scope) {
  134. ids = Array.isArray(ids) ? ids : [ids];
  135. ids = this.convertIds(ids);
  136. this.exec('cancel', ids, callback, scope);
  137. };
  138. /**
  139. * Cancel all scheduled notifications.
  140. *
  141. * @param [ Function ] callback The function to be exec as the callback.
  142. * @param [ Object ] scope The callback function's scope.
  143. *
  144. * @return [ Void ]
  145. */
  146. exports.cancelAll = function (callback, scope) {
  147. this.exec('cancelAll', null, callback, scope);
  148. };
  149. /**
  150. * Check if a notification is present.
  151. *
  152. * @param [ Int ] id The ID of the notification.
  153. * @param [ Function ] callback The function to be exec as the callback.
  154. * @param [ Object ] scope The callback function's scope.
  155. *
  156. * @return [ Void ]
  157. */
  158. exports.isPresent = function (id, callback, scope) {
  159. var fn = this.createCallbackFn(callback, scope);
  160. this.getType(id, function (type) {
  161. fn(type != 'unknown');
  162. });
  163. };
  164. /**
  165. * Check if a notification has a given type.
  166. *
  167. * @param [ Int ] id The ID of the notification.
  168. * @param [ String ] type The type of the notification.
  169. * @param [ Function ] callback The function to be exec as the callback.
  170. * @param [ Object ] scope The callback function's scope.
  171. *
  172. * @return [ Void ]
  173. */
  174. exports.hasType = function (id, type, callback, scope) {
  175. var fn = this.createCallbackFn(callback, scope);
  176. this.getType(id, function (type2) {
  177. fn(type == type2);
  178. });
  179. };
  180. /**
  181. * Get the type (triggered, scheduled) for the notification.
  182. *
  183. * @param [ Int ] id The ID of the notification.
  184. * @param [ Function ] callback The function to be exec as the callback.
  185. * @param [ Object ] scope The callback function's scope.
  186. *
  187. * @return [ Void ]
  188. */
  189. exports.getType = function (id, callback, scope) {
  190. this.exec('type', id, callback, scope);
  191. };
  192. /**
  193. * List of all notification ids.
  194. *
  195. * @param [ Function ] callback The function to be exec as the callback.
  196. * @param [ Object ] scope The callback function's scope.
  197. *
  198. * @return [ Void ]
  199. */
  200. exports.getIds = function (callback, scope) {
  201. this.exec('ids', null, callback, scope);
  202. };
  203. /**
  204. * List of all scheduled notification IDs.
  205. *
  206. * @param [ Function ] callback The function to be exec as the callback.
  207. * @param [ Object ] scope The callback function's scope.
  208. *
  209. * @return [ Void ]
  210. */
  211. exports.getScheduledIds = function (callback, scope) {
  212. this.exec('scheduledIds', null, callback, scope);
  213. };
  214. /**
  215. * List of all triggered notification IDs.
  216. *
  217. * @param [ Function ] callback The function to be exec as the callback.
  218. * @param [ Object ] scope The callback function's scope.
  219. *
  220. * @return [ Void ]
  221. */
  222. exports.getTriggeredIds = function (callback, scope) {
  223. this.exec('triggeredIds', null, callback, scope);
  224. };
  225. /**
  226. * List of local notifications specified by id.
  227. * If called without IDs, all notification will be returned.
  228. *
  229. * @param [ Array<Int> ] ids The IDs of the notifications.
  230. * @param [ Function ] callback The function to be exec as the callback.
  231. * @param [ Object ] scope The callback function's scope.
  232. *
  233. * @return [ Void ]
  234. */
  235. exports.get = function () {
  236. var args = Array.apply(null, arguments);
  237. if (typeof args[0] == 'function') {
  238. args.unshift([]);
  239. }
  240. var ids = args[0],
  241. callback = args[1],
  242. scope = args[2];
  243. if (!Array.isArray(ids)) {
  244. this.exec('notification', Number(ids), callback, scope);
  245. return;
  246. }
  247. ids = this.convertIds(ids);
  248. this.exec('notifications', ids, callback, scope);
  249. };
  250. /**
  251. * List for all notifications.
  252. *
  253. * @param [ Function ] callback The function to be exec as the callback.
  254. * @param [ Object ] scope The callback function's scope.
  255. *
  256. * @return [ Void ]
  257. */
  258. exports.getAll = function (callback, scope) {
  259. this.exec('notifications', null, callback, scope);
  260. };
  261. /**
  262. * List of all scheduled notifications.
  263. *
  264. * @param [ Function ] callback The function to be exec as the callback.
  265. * @param [ Object ] scope The callback function's scope.
  266. */
  267. exports.getScheduled = function (callback, scope) {
  268. this.exec('scheduledNotifications', null, callback, scope);
  269. };
  270. /**
  271. * List of all triggered notifications.
  272. *
  273. * @param [ Function ] callback The function to be exec as the callback.
  274. * @param [ Object ] scope The callback function's scope.
  275. */
  276. exports.getTriggered = function (callback, scope) {
  277. this.exec('triggeredNotifications', null, callback, scope);
  278. };
  279. /**
  280. * Register an group of actions by id.
  281. *
  282. * @param [ String ] id The Id of the group.
  283. * @param [ Array] actions The action config settings.
  284. * @param [ Function ] callback The function to be exec as the callback.
  285. * @param [ Object ] scope The callback function's scope.
  286. *
  287. * @return [ Void ]
  288. */
  289. exports.addActionGroup = function (id, actions, callback, scope) {
  290. var config = { actionGroupId: id, actions: actions };
  291. this.exec('registerCategory', config, callback, scope);
  292. };
  293. /**
  294. * The (platform specific) default settings.
  295. *
  296. * @return [ Object ]
  297. */
  298. exports.getDefaults = function () {
  299. return this._defaults;
  300. };
  301. /**
  302. * Overwrite default settings.
  303. *
  304. * @param [ Object ] newDefaults New default values.
  305. *
  306. * @return [ Void ]
  307. */
  308. exports.setDefaults = function (newDefaults) {
  309. var defaults = this.getDefaults();
  310. for (var key in defaults) {
  311. if (newDefaults.hasOwnProperty(key)) {
  312. defaults[key] = newDefaults[key];
  313. }
  314. }
  315. };
  316. /**
  317. * Register callback for given event.
  318. *
  319. * @param [ String ] event The name of the event.
  320. * @param [ Function ] callback The function to be exec as callback.
  321. * @param [ Object ] scope The callback function's scope.
  322. *
  323. * @return [ Void ]
  324. */
  325. exports.on = function (event, callback, scope) {
  326. if (typeof callback !== "function")
  327. return;
  328. if (!this._listener[event]) {
  329. this._listener[event] = [];
  330. }
  331. var item = [callback, scope || window];
  332. this._listener[event].push(item);
  333. };
  334. /**
  335. * Unregister callback for given event.
  336. *
  337. * @param [ String ] event The name of the event.
  338. * @param [ Function ] callback The function to be exec as callback.
  339. *
  340. * @return [ Void ]
  341. */
  342. exports.un = function (event, callback) {
  343. var listener = this._listener[event];
  344. if (!listener)
  345. return;
  346. for (var i = 0; i < listener.length; i++) {
  347. var fn = listener[i][0];
  348. if (fn == callback) {
  349. listener.splice(i, 1);
  350. break;
  351. }
  352. }
  353. };