local-notification-core.js 11 KB

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